fix: #10789 Prevent Double Booking (for requests that require confirmation) (#10882)

Co-authored-by: Peer Richelsen <[email protected]>
Co-authored-by: alannnc <[email protected]>
Co-authored-by: zomars <[email protected]>
This commit is contained in:
authored and GitHub committed 2023-09-27 20:54:22 +00:00
1 parent a2df323cf8
commit d7c3132fa5
3 files changed
+71 -4

No files matched your search

@@ -7,6 +7,7 @@ import { handleConfirmation } from "@calcom/features/bookings/lib/handleConfirma
import { handleWebhookTrigger } from "@calcom/features/bookings/lib/handleWebhookTrigger";
import type { EventTypeInfo } from "@calcom/features/webhooks/lib/sendPayload";
import { isPrismaObjOrUndefined, parseRecurringEvent } from "@calcom/lib";
import { getUsernameList } from "@calcom/lib/defaultEvents";
import { getTeamIdFromEventType } from "@calcom/lib/getTeamIdFromEventType";
import { getTranslation } from "@calcom/lib/server";
import { getUsersCredentials } from "@calcom/lib/server/getUsersCredentials";
@@ -19,6 +20,7 @@ import type { IAbstractPaymentService, PaymentApp } from "@calcom/types/PaymentS
import { TRPCError } from "@trpc/server";
import type { TrpcSessionUser } from "../../../trpc";
import { getAvailableSlots } from "../slots/util";
import type { TConfirmInputSchema } from "./confirm.schema";
import type { BookingsProcedureContext } from "./util";
@@ -55,6 +57,7 @@ export const confirmHandler = async ({ ctx, input }: ConfirmOptions) => {
teamId: true,
recurringEvent: true,
title: true,
slug: true,
requiresConfirmation: true,
currency: true,
length: true,
@@ -113,6 +116,27 @@ export const confirmHandler = async ({ ctx, input }: ConfirmOptions) => {
throw new TRPCError({ code: "BAD_REQUEST", message: "Booking already confirmed" });
}
// Check for user's slot availability for current booking start and end time
if (confirmed) {
const slotsAvailable = await getAvailableSlots({
input: {
startTime: booking?.startTime.toISOString(),
endTime: booking?.endTime.toISOString(),
eventTypeId: booking?.eventType?.id,
eventTypeSlug: booking?.eventType?.slug,
timeZone: user?.timeZone,
usernameList: getUsernameList(user?.username ?? ""),
isTeamEvent: !!booking?.eventType?.teamId || false,
},
});
// If no free slot available with current booking request's start time and
// end time, then the slot is booked for other booking ,throw error
if (slotsAvailable?.slots && Object.keys(slotsAvailable.slots).length === 0) {
throw new TRPCError({ code: "BAD_REQUEST", message: "Slot already confirmed for other booking" });
}
}
// If booking requires payment and is not paid, we don't allow confirmation
if (confirmed && booking.payment.length > 0 && !booking.paid) {
await prisma.booking.update({