163 lines
5.0 KiB
TypeScript
163 lines
5.0 KiB
TypeScript
import { getClientWithToken } from '../apollo/client';
|
|
import * as GQL from '../types';
|
|
import { formatDate, formatTime, getMinutes } from '../utils/datetime-format';
|
|
import { BaseService } from './base';
|
|
import { CustomersService } from './customers';
|
|
import { ServicesService } from './services';
|
|
import { type VariablesOf } from '@graphql-typed-document-node/core';
|
|
import dayjs from 'dayjs';
|
|
|
|
export class SlotsService extends BaseService {
|
|
async createSlot(variables: VariablesOf<typeof GQL.CreateSlotDocument>) {
|
|
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: variables.input.time_end && formatTime(variables.input.time_end).db(),
|
|
time_start: variables.input.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<typeof GQL.DeleteSlotDocument>) {
|
|
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<typeof GQL.GetSlotsDocument>,
|
|
context: { service: GQL.ServiceFiltersInput },
|
|
) {
|
|
const { query } = await getClientWithToken();
|
|
|
|
const getSlotsResult = await query({
|
|
query: GQL.GetSlotsOrdersDocument,
|
|
variables: {
|
|
filters: {
|
|
...variables.filters,
|
|
date: { eq: formatDate(variables?.filters?.date?.eq).db() },
|
|
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 &&
|
|
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<typeof GQL.GetSlotDocument>) {
|
|
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<typeof GQL.GetSlotsDocument>) {
|
|
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<typeof GQL.UpdateSlotDocument>) {
|
|
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;
|
|
}
|
|
}
|