import { getClientWithToken } from '../apollo/client'; import * as GQL from '../types'; import { formatDate, formatTime } from '../utils/datetime-format'; import { BaseService } from './base'; import { CustomersService } from './customers'; import { type VariablesOf } from '@graphql-typed-document-node/core'; 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, date: formatDate(variables.input.date).db(), master: customer?.documentId, time_end: formatTime(variables.input.time_end).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 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 getSlot(variables: VariablesOf) { const { query } = await getClientWithToken(); const result = await query({ query: GQL.GetSlotDocument, variables, }); if (result.error) throw new Error(result.error.message); return result.data; } async getSlots(variables: VariablesOf) { const { query } = await getClientWithToken(); const result = await query({ query: GQL.GetSlotsDocument, variables: { filters: { ...variables.filters, date: { eq: formatDate(variables?.filters?.date?.eq).db() }, }, }, }); if (result.error) throw new Error(result.error.message); return result.data; } async updateSlot(variables: VariablesOf) { const { mutate } = await getClientWithToken(); const mutationResult = await mutate({ mutation: GQL.UpdateSlotDocument, variables: { ...variables, date: variables.data?.date ? formatDate(variables.data.date).db() : undefined, time_end: variables.data?.time_end ? formatTime(variables.data.time_end).db() : undefined, time_start: variables.data?.time_start ? formatTime(variables.data.time_start).db() : undefined, }, }); const error = mutationResult.errors?.at(0); if (error) throw new Error(error.message); return mutationResult.data; } }