Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions apps/web/pages/api/book/recurring-event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import {
TestData,
Timezones,
} from "@calcom/testing/lib/bookingScenario/bookingScenario";
import process from "node:process";
import { WEBAPP_URL, WEBSITE_URL } from "@calcom/lib/constants";
import { ErrorCode } from "@calcom/lib/errorCodes";
import logger from "@calcom/lib/logger";
import { BookingStatus, SchedulingType } from "@calcom/prisma/enums";
import { createMockNextJsRequest } from "@calcom/testing/lib/bookingScenario/createMockNextJsRequest";
import {
expectBookingCreatedWebhookToHaveBeenFired,
Expand All @@ -19,16 +24,10 @@ import {
} from "@calcom/testing/lib/bookingScenario/expects";
import { getMockRequestDataForBooking } from "@calcom/testing/lib/bookingScenario/getMockRequestDataForBooking";
import { setupAndTeardown } from "@calcom/testing/lib/bookingScenario/setupAndTeardown";

import { test } from "@calcom/testing/lib/fixtures/fixtures";
import { v4 as uuidv4 } from "uuid";
import { describe, expect } from "vitest";

import { WEBAPP_URL, WEBSITE_URL } from "@calcom/lib/constants";
import { ErrorCode } from "@calcom/lib/errorCodes";
import logger from "@calcom/lib/logger";
import { BookingStatus, SchedulingType } from "@calcom/prisma/enums";
import { test } from "@calcom/testing/lib/fixtures/fixtures";

const DAY_IN_MS = 1000 * 60 * 60 * 24;

function getPlusDayDate(date: string, days: number) {
Expand Down Expand Up @@ -202,8 +201,8 @@ describe("handleNewBooking", () => {
organizer,
location: "integrations:daily",
subscriberUrl: "http://my-webhook.example.com",
//FIXME: All recurring bookings seem to have the same URL. https://github.com/calcom/cal.diy/issues/11955
videoCallUrl: `${WEBAPP_URL}/video/${createdBookings[0].uid}`,

videoCallUrl: "http://mock-dailyvideo.example.com/meeting-1",
});
}

Expand Down Expand Up @@ -549,8 +548,8 @@ describe("handleNewBooking", () => {
organizer,
location: "integrations:daily",
subscriberUrl: "http://my-webhook.example.com",
//FIXME: File a bug - All recurring bookings seem to have the same URL. They should have same CalVideo URL which could mean that future recurring meetings would have already expired by the time they are needed.
videoCallUrl: `${WEBAPP_URL}/video/${createdBookings[0].uid}`,

videoCallUrl: "http://mock-dailyvideo.example.com/meeting-1",
});
}

Expand Down Expand Up @@ -765,8 +764,8 @@ describe("handleNewBooking", () => {
organizer,
location: "integrations:daily",
subscriberUrl: "http://my-webhook.example.com",
//FIXME: File a bug - All recurring bookings seem to have the same URL. They should have same CalVideo URL which could mean that future recurring meetings would have already expired by the time they are needed.
videoCallUrl: `${WEBAPP_URL}/video/${createdBookings[0].uid}`,

videoCallUrl: "http://mock-dailyvideo.example.com/meeting-1",
});
}

Expand Down
85 changes: 55 additions & 30 deletions packages/features/bookings/lib/handleConfirmation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ export async function handleConfirmation(args: {
const apps = eventTypeAppMetadataOptionalSchema.parse(eventTypeMetadata?.apps);
const eventManager = new EventManager(user, apps);
const areCalendarEventsEnabled = platformClientParams?.areCalendarEventsEnabled ?? true;
const scheduleResult = await eventManager.create(evt, { skipCalendarEvent: !areCalendarEventsEnabled });
const scheduleResult = await eventManager.create(evt, {
skipCalendarEvent: !areCalendarEventsEnabled,
});
const results = scheduleResult.results;
const metadata: AdditionalInformation = {};
const spanContext = distributedTracing.createSpan(traceContext, "handle_confirmation");
Expand Down Expand Up @@ -154,10 +156,11 @@ export async function handleConfirmation(args: {
} | null;
}[] = [];

// meetingUrl for non-recurring bookings only (recurring occurrences each get their own below)
const videoCallUrl = metadata.hangoutLink ? metadata.hangoutLink : evt.videoCallData?.url || "";
const meetingUrl = getVideoCallUrlFromCalEvent(evt) || videoCallUrl;

let acceptedBookings: {
let _acceptedBookings: {
oldStatus: BookingStatus;
uid: string;
}[];
Expand All @@ -170,11 +173,17 @@ export async function handleConfirmation(args: {
status: BookingStatus.PENDING,
},
});

acceptedBookings = unconfirmedRecurringBookings.map((booking) => ({
_acceptedBookings = unconfirmedRecurringBookings.map((booking) => ({
oldStatus: booking.status,
uid: booking.uid,
}));
// Derive the video call URL from the video reference created for this confirmation.
// Cal Video recurring series intentionally share one room URL across occurrences.
const videoReference = scheduleResult.referencesToCreate.find(
(ref) => ref.meetingUrl && (ref.type === evt.videoCallData?.type || ref.type.endsWith("_video"))
);

const occurrenceMeetingUrl = videoReference?.meetingUrl || meetingUrl;

const updateBookingsPromise = unconfirmedRecurringBookings.map((recurringBooking) =>
prisma.booking.update({
Expand All @@ -189,9 +198,10 @@ export async function handleConfirmation(args: {
paid,
metadata: {
...(typeof recurringBooking.metadata === "object" ? recurringBooking.metadata : {}),
videoCallUrl: meetingUrl,
videoCallUrl: occurrenceMeetingUrl,
},
},

select: {
eventType: {
select: {
Expand Down Expand Up @@ -300,7 +310,7 @@ export async function handleConfirmation(args: {
},
});
updatedBookings.push(updatedBooking);
acceptedBookings = [
_acceptedBookings = [
{
oldStatus: booking.status,
uid: booking.uid,
Expand Down Expand Up @@ -361,6 +371,7 @@ export async function handleConfirmation(args: {
);
});
});

subscribersMeetingEnded.forEach((subscriber) => {
updatedBookingsWithCalEventResponses.forEach((booking) => {
scheduleTriggerPromises.push(
Expand Down Expand Up @@ -400,31 +411,45 @@ export async function handleConfirmation(args: {
length: eventType?.length,
};

const payload: EventPayloadType = {
...evt,
...eventTypeInfo,
bookingId,
eventTypeId: eventType?.id,
status: "ACCEPTED",
smsReminderNumber: booking.smsReminderNumber || undefined,
metadata: meetingUrl ? { videoCallUrl: meetingUrl } : {},
...(platformClientParams ? platformClientParams : {}),
};
const { assignmentReason: _, ...evtWithoutAssignmentReason } = evt;

const promises = subscribersBookingCreated.map((sub) =>
sendPayload(
sub.secret,
WebhookTriggerEvents.BOOKING_CREATED,
new Date().toISOString(),
sub,
payload
).catch((e) => {
tracingLogger.error(
`Error executing webhook for event: ${WebhookTriggerEvents.BOOKING_CREATED}, URL: ${sub.subscriberUrl}, bookingId: ${evt.bookingId}, bookingUid: ${evt.uid}, platformClientId: ${platformClientParams?.platformClientId}`,
safeStringify(e)
);
})
);
const promises = updatedBookings.flatMap((updatedBooking) => {
const bookingMeetingUrl =
updatedBooking.metadata &&
typeof updatedBooking.metadata === "object" &&
"videoCallUrl" in updatedBooking.metadata
? (updatedBooking.metadata as { videoCallUrl: string }).videoCallUrl
: meetingUrl;

const payload: EventPayloadType = {
...evtWithoutAssignmentReason,
...eventTypeInfo,
uid: updatedBooking.uid,
startTime: updatedBooking.startTime.toISOString(),
endTime: updatedBooking.endTime.toISOString(),
bookingId: updatedBooking.id,
eventTypeId: eventType?.id,
status: "ACCEPTED",
smsReminderNumber: booking.smsReminderNumber || undefined,
metadata: bookingMeetingUrl ? { videoCallUrl: bookingMeetingUrl } : {},
...(platformClientParams ? platformClientParams : {}),
};

return subscribersBookingCreated.map((sub) =>
sendPayload(
sub.secret,
WebhookTriggerEvents.BOOKING_CREATED,
new Date().toISOString(),
sub,
payload
).catch((e) => {
tracingLogger.error(
`Error executing webhook for event: ${WebhookTriggerEvents.BOOKING_CREATED}, URL: ${sub.subscriberUrl}, bookingId: ${updatedBooking.id}, bookingUid: ${updatedBooking.uid}, platformClientId: ${platformClientParams?.platformClientId}`,
safeStringify(e)
);
})
);
});

await Promise.all(promises);
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2207,10 +2207,10 @@ async function handler(
}

const metadata = videoCallUrl
? {
videoCallUrl: getVideoCallUrlFromCalEvent(evt) || videoCallUrl,
}
: undefined;
? {
videoCallUrl,
}
: undefined;

const isBookingEmailSmsTaskerEnabled = false;

Expand Down
Loading