import { SERVICE_TYPE } from 'nexus-plugin-prisma/client' import { BusinessHourCreateWithoutServicesBusinessHoursInput, Enumerable, } from 'nexus-plugin-prisma/client' import { logger } from '../../utils/logger' import * as SlackUtils from '../../utils/slack' import { prismaClient as prisma } from '../../prismaClient' import { createServiceProjectsForService } from '../../queues' type CreateServiceInputType = { businessName: string type: SERVICE_TYPE ownersIds?: string[] email?: string } export async function createService({ businessName, ownersIds, type, email, }: CreateServiceInputType) { const businessHoursToCreate: Enumerable = new Array( 7 ) .fill(0, 6) .map((_, index) => ({ dayOfWeek: index, openAtMS: 1000 * 60 * 60 * 9, closeAtMS: 1000 * 60 * 60 * 17, isClosed: index === 0 || index === 6, })) const pickupHoursToCreate: Enumerable = new Array( 7 ) .fill(0, 6) .map((_, index) => ({ dayOfWeek: index, openAtMS: 1000 * 60 * 60 * 9, closeAtMS: 1000 * 60 * 60 * 17, isClosed: index === 0 || index === 6, })) const createdService = await prisma.service.create({ data: { type, email, businessName, businessHours: { create: businessHoursToCreate }, pickupHours: { create: pickupHoursToCreate }, ...(ownersIds?.length > 0 && { users: { connect: ownersIds.map((id) => ({ id })), }, }), }, }) createServiceProjectsForService(createdService.id).catch(logger.error) SlackUtils.send({ channel: 'newProjectCondoManager', subject: 'New service provider', text: `*Name*: ${createdService.businessName}\n*Category*: ${createdService.type}`, }).catch(logger.error) return createdService }