From 0698242257724af15c701433a4d395ac7086ee6f Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Wed, 21 May 2025 17:26:10 +0300 Subject: [PATCH] take into existing orders when computing times --- packages/graphql/api/slots.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/graphql/api/slots.ts b/packages/graphql/api/slots.ts index 7ebcd3a..92b4587 100644 --- a/packages/graphql/api/slots.ts +++ b/packages/graphql/api/slots.ts @@ -75,20 +75,32 @@ export class SlotsService extends BaseService { if (!service) throw new Error('Service not found'); - const duration = getMinutes(service.duration); + 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(duration, 'minutes'); + const endTime = dayjs(`${slot.date} ${slot.time_end}`).subtract(serviceDuration, 'minutes'); while (startTime.valueOf() <= endTime.valueOf()) { - times.push({ slotId: slot.documentId, time: startTime.format('HH:mm') }); + 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'); } }