fix: api v2 logic for slots reservation endpoint (#23222)

* init logic for handling reservation for round robin event

* remove unused comments

* add function to get already reserved slots

* fixup: add correct logic for round robin slots reservation

* fixup: implement PR feedback

* fixup: update logic to handle fixed and non fixed round robin hosts

* update slots repository

* fixup: implement PR feedback

* fixup: implement PR feedback

* add tests for round robin slot reservation with non fixed hosts

* add tests for round robin slot reservation with fixed and non fixed hosts

* fix: implement PR feedback

* fix: e2e test

* fix: move validateRoundRobinSlotAvailability to core libraries

* fix: merge conflicts

* fix: merge conflicts

* update slots.ts for platform libraries

* fix: import logic from platform libraries

* cleanup

* fix: import path

* fix: missing import

* fix: test code and handle error thrown

---------

Co-authored-by: supalarry <[email protected]>
Co-authored-by: Alex van Andel <[email protected]>
This commit is contained in:
authored and GitHub committed 2025-10-10 12:35:21 +02:00
1 parent c46f824e3f
commit b286244aab
4 files changed
+315 -4

No files matched your search

@@ -0,0 +1,90 @@
import { DateTime } from "luxon";
import { HttpError } from "@calcom/lib/http-error";
import { prisma } from "@calcom/prisma";
import type { Host } from "@calcom/prisma/client";
export async function validateRoundRobinSlotAvailability(
eventTypeId: number,
startDate: DateTime,
endDate: DateTime,
hosts: Host[]
) {
const fixedHosts = hosts.filter((host) => host.isFixed === true);
const nonFixedHosts = hosts.filter((host) => host.isFixed === false);
if (fixedHosts.length > 0) {
await validateFixedHostsAvailability(eventTypeId, startDate, endDate, fixedHosts);
} else {
await validateNonFixedHostsAvailability(eventTypeId, startDate, endDate, nonFixedHosts);
}
}
async function validateFixedHostsAvailability(
eventTypeId: number,
startDate: DateTime,
endDate: DateTime,
hosts: Host[]
) {
const existingBooking = await prisma.booking.findFirst({
where: {
eventTypeId,
startTime: startDate.toJSDate(),
endTime: endDate.toJSDate(),
},
select: { attendees: true, userId: true, status: true },
});
const existingSlotReservation = await prisma.selectedSlots.count({
where: {
eventTypeId,
slotUtcStartDate: startDate.toISO() ?? "",
slotUtcEndDate: endDate.toISO() ?? "",
// Only consider non-expired reservations
releaseAt: { gt: DateTime.utc().toJSDate() },
},
});
const hasHostAsAttendee = hosts.some(
(host) =>
existingBooking?.attendees.some((attendee) => attendee.id === host.userId) ||
existingBooking?.userId === host.userId
);
if (existingSlotReservation > 0) {
throw new HttpError({
statusCode: 422,
message: `Can't reserve the slot because the round robin event type has no available hosts left at this time slot.`,
});
}
if (hasHostAsAttendee) {
throw new HttpError({
statusCode: 422,
message: `Can't reserve a slot if the event is already booked.`,
});
}
}
async function validateNonFixedHostsAvailability(
eventTypeId: number,
startDate: DateTime,
endDate: DateTime,
hosts: Host[]
) {
const existingSlotReservations = await prisma.selectedSlots.count({
where: {
eventTypeId,
slotUtcStartDate: startDate.toISO() ?? "",
slotUtcEndDate: endDate.toISO() ?? "",
// Only consider non-expired reservations
releaseAt: { gt: DateTime.utc().toJSDate() },
},
});
if (existingSlotReservations === hosts.length) {
throw new HttpError({
statusCode: 422,
message: `Can't reserve the slot because the round robin event type has no available hosts left at this time slot.`,
});
}
}
+2
View File
@@ -1,3 +1,4 @@
import { validateRoundRobinSlotAvailability } from "@calcom/features/ee/round-robin/utils/validateRoundRobinSlotAvailability";
import { FilterHostsService } from "@calcom/features/bookings/lib/host-filtering/filterHostsBySameRoundRobinHost";
import { QualifiedHostsService } from "@calcom/features/bookings/lib/host-filtering/findQualifiedHostsWithDelegationCredentials";
import { BusyTimesService } from "@calcom/lib/getBusyTimes";
@@ -12,3 +13,4 @@ export { QualifiedHostsService };
export { FilterHostsService };
export { NoSlotsNotificationService };
export { validateRoundRobinSlotAvailability };