take into existing orders when computing times

This commit is contained in:
vchikalkin 2025-05-21 17:26:10 +03:00
parent f0b63a5e7e
commit 0698242257

View File

@ -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');
}
}