import { getClientWithToken } from '../apollo/client'; import * as GQL from '../types'; import { BaseService } from './base'; import { CustomersService } from './customers'; import { ServicesService } from './services'; import { type VariablesOf } from '@graphql-typed-document-node/core'; import { getMinutes } from '@repo/utils/datetime-format'; import dayjs from 'dayjs'; export class SlotsService extends BaseService { async createSlot(variables: VariablesOf) { const customerService = new CustomersService(this.customer); const { customer } = await customerService.getCustomer(this.customer); const { mutate } = await getClientWithToken(); const mutationResult = await mutate({ mutation: GQL.CreateSlotDocument, variables: { ...variables, input: { ...variables.input, master: customer?.documentId, }, }, }); const error = mutationResult.errors?.at(0); if (error) throw new Error(error.message); return mutationResult.data; } async deleteSlot(variables: VariablesOf) { const { mutate } = await getClientWithToken(); const mutationResult = await mutate({ mutation: GQL.DeleteSlotDocument, variables, }); const error = mutationResult.errors?.at(0); if (error) throw new Error(error.message); return mutationResult.data; } async getAvailableTimeSlots( variables: VariablesOf, context: { service: GQL.ServiceFiltersInput }, ) { const { query } = await getClientWithToken(); const getSlotsResult = await query({ query: GQL.GetSlotsOrdersDocument, variables: { filters: { ...variables.filters, state: { eq: GQL.Enum_Slot_State.Open }, }, }, }); if (getSlotsResult.error) throw new Error(getSlotsResult.error.message); const servicesService = new ServicesService(this.customer); if (!context?.service?.documentId?.eq) throw new Error('Missing service id'); const { service } = await servicesService.getService({ documentId: context.service.documentId.eq, }); if (!service) throw new Error('Service not found'); const serviceDuration = getMinutes(service.duration); const openedSlots = getSlotsResult.data.slots; const times: Array<{ slotId: string; time: string }> = []; for (const slot of openedSlots) { if (!slot?.time_start || !slot?.time_end) continue; let startTime = dayjs(`${slot.date} ${slot.time_start}`); const endTime = dayjs(`${slot.date} ${slot.time_end}`).subtract(serviceDuration, 'minutes'); while (startTime.valueOf() <= endTime.valueOf()) { const slotStartTime = startTime; const potentialEndTime = startTime.add(serviceDuration, 'minutes'); const hasConflict = slot.orders.some( (order) => order?.state && ![GQL.Enum_Order_State.Cancelled, GQL.Enum_Order_State.Completed].includes( order.state, ) && slotStartTime.isBefore(dayjs(`${slot.date} ${order.time_end}`)) && potentialEndTime.isAfter(dayjs(`${slot.date} ${order.time_start}`)), ); if (!hasConflict) { times.push({ slotId: slot.documentId, time: startTime.format('HH:mm') }); } startTime = startTime.add(15, 'minutes'); } } return { times }; } async getSlot(variables: VariablesOf) { const { query } = await getClientWithToken(); const result = await query({ query: GQL.GetSlotDocument, variables, }); return result.data; } async getSlots(variables: VariablesOf) { const { query } = await getClientWithToken(); const result = await query({ query: GQL.GetSlotsDocument, variables, }); return result.data; } async updateSlot(variables: VariablesOf) { const { mutate } = await getClientWithToken(); const mutationResult = await mutate({ mutation: GQL.UpdateSlotDocument, variables, }); const error = mutationResult.errors?.at(0); if (error) throw new Error(error.message); return mutationResult.data; } }