// import { Service } from './../../generated/prisma-client/index' import { schema } from 'nexus' import { ServiceWhereUniqueInput, ServiceSelect } from 'nexus-plugin-prisma/client' import { logger } from '../../utils/logger' export const Service = schema.objectType({ name: 'Service', definition(t) { t.model.id() t.model.type() t.model.createdAt() t.model.updatedAt() t.model.businessName() t.model.GST() t.model.PST() t.model.identificationNumber() t.model.walterPourcentage() t.model.homeScreenPosition() t.model.canAddMultipleOptions() t.model.durationMS() t.model.maxDateBookingDaysFromNow() t.model.isOffline() t.model.maximumUsageTime() t.model.email() t.model.minimumSpendPerOrder() t.model.bookingInAdvanceMinSeconds() t.model.bookingInAdvanceMaxSeconds() t.model.hasSpecificBooking() t.model.shippingFees() t.model.timeRequiredBetweenEachBookingSeconds() t.model.allowMultipleBookings() t.model.squareLogoId() t.model.bannerLogoId() t.model.stripeAccountId() t.model.stripeAccountAccessToken() t.model.isApproved() t.model.businessBio() t.model.businessDescription() t.model.businessBioFr() t.model.businessDescriptionFr() t.model.deliveryFees() t.model.timeRequiredBetweenEachBookingMS() t.model.comingSoon() t.model.appPosition() t.model.hasPickup() t.model.bookingInAdvanceMinSecondsForPickup() t.model.bookingInAdvanceMaxSecondsForPickup() t.model.allowMultipleBookingsForPickup() t.model.hasSpecificBookingForPickup() t.model.timeRequiredBetweenEachBookingMSForPickup() t.model.hasDelivery() t.model.chargeTaxes() t.model.address() t.model.bannerLogo() t.model.phone() t.model.squareLogo() t.model.invoices() t.model.options() t.model.categories() t.model.picturesHeroImageOfServiceToService() t.model.picturesIconOfServiceToService() t.model.serviceProjects() t.model.users() t.model.businessHours() t.model.acceptedTermsAndConditions() // t.model.contactPersons() // We're not using contactPersons anymore t.model.owner() t.model.pickupHours() t.model.frequentlyAskedQuestions() t.model.shippingLines() t.model.taxLines() t.model.specialDates() }, }) // frequentlyAskedQuestions export const ServiceQueries = schema.extendType({ type: 'Query', definition(t) { t.crud.service() t.crud.services({ filtering: true, ordering: true }) }, }) export const ServiceMutation = schema.extendType({ type: 'Mutation', definition: (t) => { t.crud.deleteOneService({ alias: 'deleteService' }) t.crud.updateOneService({ alias: 'updateService' }) t.crud.createOneService({ alias: 'createService' }) t.field('updateService', { type: 'Service', args: { data: schema.arg({ type: 'ServiceUpdateInput' }), where: schema.arg({ type: 'ServiceWhereUniqueInput' }), }, resolve: async (parent, { where, data }, ctx: NexusContext) => { return ctx.prisma.service.update({ where, data }) }, }) t.field('syncAllBusinessHoursWithFirstServiceProject', { type: 'Boolean', args: { serviceId: schema.stringArg(), }, resolve: async (parent, { serviceId }, ctx: NexusContext) => { const service = await ctx.prisma.service.findOne({ where: { id: serviceId }, select: { serviceProjects: { select: { id: true, businessHours: { select: { id: true, dayOfWeek: true, openAtMS: true, closeAtMS: true, openAt: true, closeAt: true, isClosed: true, }, }, }, }, }, }) const firstServiceProject = service.serviceProjects[0] // Update every serviceProject business hours await Promise.all( service.serviceProjects.map((serviceProject) => ctx.prisma.serviceProject.update({ where: { id: serviceProject.id, }, data: { businessHours: { update: serviceProject.businessHours.map((businessHour, index) => ({ where: { id: businessHour.id, }, data: { dayOfWeek: firstServiceProject.businessHours[index].dayOfWeek, openAtMS: firstServiceProject.businessHours[index].openAtMS, closeAtMS: firstServiceProject.businessHours[index].closeAtMS, openAt: firstServiceProject.businessHours[index].openAt, closeAt: firstServiceProject.businessHours[index].closeAt, isClosed: firstServiceProject.businessHours[index].isClosed, }, })), }, }, }) ) ) return true }, }) // t.field('syncAllMenuForService', { // type: 'Boolean', // args: { serviceId: schema.stringArg() }, // resolve: async (parent, { serviceId }, ctx: NexusContext) => { // const service = await ctx.prisma.service({ id: serviceId }).$fragment( // `{ // serviceProjects { // id // options { // id // title // titleFr // description // descriptionFr // price // position // isDeleted // isRecurring // image { // url // name // cloudinaryId // format // } // } // } // }` // ) // const [firstServiceProject, ...restOfServiceProjects] = service.serviceProjects // await Promise.all( // restOfServiceProjects.map(serviceProject => { // const hasMoreOptionsThenCurrent = // firstServiceProject.options.length > serviceProject.options.length // const hasLessOptionsThenCurrent = // firstServiceProject.options.length < serviceProject.options.length // return ctx.prisma.updateServiceProject({ // where: { // id: serviceProject.id // }, // data: { // options: { // ...(hasMoreOptionsThenCurrent // ? { // create: firstServiceProject.options // .slice(serviceProject.options.length) // .filter(option => !option.isDeleted) // .map(({ ...restOfOptionData }) => ({ // ...restOfOptionData, // ...(option.image && // option.image.url && { // image: { // create: { // name: option.image.name, // url: option.image.url, // cloudinaryId: option.image.cloudinaryId, // format: option.image.format // } // } // }) // })) // } // : null), // ...(hasLessOptionsThenCurrent // ? { // delete: serviceProject.options // .slice(firstServiceProject.options.length) // .map(option => ({ // id: option.id // })) // } // : null), // update: serviceProject.options.map((option, index) => ({ // where: { // id: option.id // }, // data: { // title: firstServiceProject.options[index].title, // titleFr: firstServiceProject.options[index].titleFr, // description: firstServiceProject.options[index].description, // descriptionFr: firstServiceProject.options[index].descriptionFr, // price: firstServiceProject.options[index].price, // durationMS: firstServiceProject.options[index].durationMS, // image: // firstServiceProject.options[index].image && // firstServiceProject.options[index].image.url // ? option.image // ? { // update: { // name: firstServiceProject.options[index].image.name, // url: firstServiceProject.options[index].image.url, // cloudinaryId: // firstServiceProject.options[index].image.cloudinaryId, // format: firstServiceProject.options[index].image.format // } // } // : { // create: { // name: firstServiceProject.options[index].image.name, // url: firstServiceProject.options[index].image.url, // cloudinaryId: // firstServiceProject.options[index].image.cloudinaryId, // format: firstServiceProject.options[index].image.format // } // } // : option.image // ? { // delete: true // } // : undefined, // isRecurring: firstServiceProject.options[index].isRecurring, // position: firstServiceProject.options[index].position, // isDeleted: firstServiceProject.options[index].isDeleted // } // })) // } // } // }) // }) // ) // return true // } // }) }, })