/* eslint-disable canonical/id-match */ import { getClientWithToken } from '../apollo/client'; import * as GQL from '../types'; import { Enum_Customer_Role, Enum_Slot_State } from '../types'; import { formatDate, formatTime, sumTime } from '../utils/datetime-format'; import { BaseService } from './base'; import { CustomersService } from './customers'; import { ServicesService } from './services'; import { SlotsService } from './slots'; import { type VariablesOf } from '@graphql-typed-document-node/core'; const ERRORS = { INVALID_CLIENT: 'Invalid client', INVALID_MASTER: 'Invalid master', INVALID_SERVICE_DURATION: 'Invalid service duration', MISSING_CLIENT: 'Missing client id', MISSING_SERVICE_ID: 'Missing service id', MISSING_SERVICES: 'Missing services', MISSING_SLOT: 'Missing slot id', MISSING_START_TIME: 'Missing time start', SLOT_CLOSED: 'Slot is closed', }; export class OrdersService extends BaseService { async createOrder(variables: { input: Omit['input'], 'time_end'>; }) { if (!variables.input.slot) throw new Error(ERRORS.MISSING_SLOT); if (!variables.input.client) throw new Error(ERRORS.MISSING_CLIENT); if (!variables.input.services?.length) throw new Error(ERRORS.MISSING_SERVICES); if (!variables.input.services[0]) throw new Error(ERRORS.MISSING_SERVICE_ID); if (!variables.input.time_start) throw new Error(ERRORS.MISSING_START_TIME); const customersService = new CustomersService(this.customer); const slotsService = new SlotsService(this.customer); const servicesService = new ServicesService(this.customer); const { customer } = await customersService.getCustomer(this.customer); const { slot } = await slotsService.getSlot({ documentId: variables.input.slot }); if (slot?.state === Enum_Slot_State.Closed) { throw new Error(ERRORS.SLOT_CLOSED); } if (customer?.role === Enum_Customer_Role.Client) { if (customer.documentId !== variables.input.client) { throw new Error(ERRORS.INVALID_CLIENT); } const masters = await customersService.getMasters(this.customer); const masterId = slot?.master?.documentId; if (!masters.customers.some((master) => master?.documentId === masterId)) { throw new Error(ERRORS.INVALID_MASTER); } } if ( customer?.role === Enum_Customer_Role.Master && slot?.master?.documentId !== customer.documentId ) { throw new Error(ERRORS.INVALID_MASTER); } const { service } = await servicesService.getService({ documentId: variables.input.services[0], }); if (!service?.duration) throw new Error(ERRORS.INVALID_SERVICE_DURATION); const endTime = sumTime(variables.input.time_start, service?.duration); const { mutate } = await getClientWithToken(); const mutationResult = await mutate({ mutation: GQL.CreateOrderDocument, variables: { input: { client: variables.input.client, date: formatDate(variables.input.date).db(), services: variables.input.services, slot: variables.input.slot, time_end: formatTime(endTime).db(), time_start: formatTime(variables.input.time_start).db(), }, }, }); const error = mutationResult.errors?.at(0); if (error) throw new Error(error.message); return mutationResult.data; } async getOrder(variables: VariablesOf) { const { query } = await getClientWithToken(); const result = await query({ query: GQL.GetOrderDocument, variables, }); return result.data; } async getOrders(variables: VariablesOf) { const { query } = await getClientWithToken(); const result = await query({ query: GQL.GetOrdersDocument, variables: { filters: { ...variables.filters, date: { eq: formatDate(variables?.filters?.date?.eq).db() }, slot: { ...variables.filters?.slot, date: { eq: formatDate(variables?.filters?.slot?.date?.eq).db() }, }, }, }, }); return result.data; } }