2025-05-20 19:53:51 +03:00

153 lines
4.4 KiB
TypeScript

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';
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: 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<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>) {
const { query } = await getClientWithToken();
const {
data: { slots: openedSlots },
error,
} = 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 (error) throw new Error(error.message);
const times: Array<{ slotId: string; time: string }> = [];
for (const slot of openedSlots) {
if (!slot?.time_start || !slot?.time_end) continue;
const orders = slot.orders ?? [];
let currentTime = dayjs(`${slot.date} ${slot.time_start}`);
const endTime = dayjs(`${slot.date} ${slot.time_end}`);
while (currentTime.isBefore(endTime)) {
const slotStart = currentTime;
const slotEnd = currentTime.add(30, 'minute');
const overlaps = orders.some((order) => {
if (!order?.time_start || !order?.time_end) return false;
const orderStart = dayjs(`${slot.date} ${order.time_start}`);
const orderEnd = dayjs(`${slot.date} ${order.time_end}`);
return slotStart.isBefore(orderEnd) && slotEnd.isAfter(orderStart);
});
if (!overlaps) {
times.push({ slotId: slot.documentId, time: slotStart.format('HH:mm') });
}
currentTime = slotEnd;
}
}
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;
}
}