feat: Add consistent iCalUID (#12122)

Co-authored-by: Hariom <[email protected]>
Co-authored-by: Keith Williams <[email protected]>
This commit is contained in:
authored and GitHub committed 2023-12-15 10:28:32 -05:00
1 parent 0e8ac7e4ed
commit 9dfa596e3e
33 files changed
+568 -195

No files matched your search

@@ -115,6 +115,8 @@ async function getBookingToDelete(id: number | undefined, uid: string | undefine
scheduledJobs: true,
seatsReferences: true,
responses: true,
iCalUID: true,
iCalSequence: true,
},
});
}
@@ -264,6 +266,8 @@ async function handler(req: CustomRequest) {
}),
seatsPerTimeSlot: bookingToDelete.eventType?.seatsPerTimeSlot,
seatsShowAttendees: bookingToDelete.eventType?.seatsShowAttendees,
iCalUID: bookingToDelete.iCalUID,
iCalSequence: bookingToDelete.iCalSequence + 1,
};
const dataForWebhooks = { evt, webhooks, eventTypeInfo };
@@ -390,6 +394,8 @@ async function handler(req: CustomRequest) {
data: {
status: BookingStatus.CANCELLED,
cancellationReason: cancellationReason,
// Assume that canceling the booking is the last action
iCalSequence: evt.iCalSequence || 100,
},
select: {
startTime: true,
@@ -37,6 +37,7 @@ import {
sendScheduledEmails,
sendScheduledSeatsEmails,
} from "@calcom/emails";
import getICalUID from "@calcom/emails/lib/getICalUID";
import { getBookingFieldsWithSystemFields } from "@calcom/features/bookings/lib/getBookingFields";
import { getCalEventResponses } from "@calcom/features/bookings/lib/getCalEventResponses";
import { handleWebhookTrigger } from "@calcom/features/bookings/lib/handleWebhookTrigger";
@@ -717,6 +718,7 @@ async function createBooking({
},
dynamicEventSlugRef,
dynamicGroupSlugRef,
iCalUID: evt.iCalUID ?? "",
user: {
connect: {
id: organizerUser.id,
@@ -823,6 +825,21 @@ function getCustomInputsResponses(
return customInputsResponses;
}
function getICalSequence(originalRescheduledBooking: BookingType | null) {
// If new booking set the sequence to 0
if (!originalRescheduledBooking) {
return 0;
}
// If rescheduling and there is no sequence set, assume sequence should be 1
if (!originalRescheduledBooking.iCalSequence) {
return 1;
}
// If rescheduling then increment sequence by 1
return originalRescheduledBooking.iCalSequence + 1;
}
async function handler(
req: NextApiRequest & { userId?: number | undefined },
{
@@ -1247,6 +1264,13 @@ async function handler(
const calEventUserFieldsResponses =
"calEventUserFieldsResponses" in reqBody ? reqBody.calEventUserFieldsResponses : null;
const iCalUID = getICalUID({
event: { iCalUID: originalRescheduledBooking?.iCalUID, uid: originalRescheduledBooking?.uid },
uid,
});
// For bookings made before introducing iCalSequence, assume that the sequence should start at 1. For new bookings start at 0.
const iCalSequence = getICalSequence(originalRescheduledBooking);
let evt: CalendarEvent = {
bookerUrl: await getBookerUrl(organizerUser),
type: eventType.slug,
@@ -1283,6 +1307,8 @@ async function handler(
seatsPerTimeSlot: eventType.seatsPerTimeSlot,
seatsShowAvailabilityCount: eventType.seatsPerTimeSlot ? eventType.seatsShowAvailabilityCount : true,
schedulingType: eventType.schedulingType,
iCalUID,
iCalSequence,
};
if (isTeamEventType && eventType.schedulingType === "COLLECTIVE") {
@@ -2488,6 +2514,18 @@ async function handler(
evt.appsStatus = handleAppsStatus(results, booking);
videoCallUrl =
metadata.hangoutLink || organizerOrFirstDynamicGroupMemberDefaultLocationUrl || videoCallUrl;
if (evt.iCalUID !== booking.iCalUID) {
// The eventManager could change the iCalUID. At this point we can update the DB record
await prisma.booking.update({
where: {
id: booking.id,
},
data: {
iCalUID: evt.iCalUID || booking.iCalUID,
},
});
}
}
if (noEmail !== true) {
let isHostConfirmationEmailsDisabled = false;
@@ -51,6 +51,7 @@ import {
expectBookingPaymentIntiatedWebhookToHaveBeenFired,
expectBrokenIntegrationEmails,
expectSuccessfulCalendarEventCreationInCalendar,
expectICalUIDAsString,
} from "@calcom/web/test/utils/bookingScenario/expects";
import { getMockRequestDataForBooking } from "@calcom/web/test/utils/bookingScenario/getMockRequestDataForBooking";
import { setupAndTeardown } from "@calcom/web/test/utils/bookingScenario/setupAndTeardown";
@@ -141,7 +142,6 @@ describe("handleNewBooking", () => {
const calendarMock = mockCalendarToHaveNoBusySlots("googlecalendar", {
create: {
id: "MOCKED_GOOGLE_CALENDAR_EVENT_ID",
iCalUID: "MOCKED_GOOGLE_CALENDAR_ICS_ID",
},
});
@@ -162,6 +162,7 @@ describe("handleNewBooking", () => {
});
const createdBooking = await handleNewBooking(req);
expect(createdBooking.responses).toContain({
email: booker.email,
name: booker.name,
@@ -193,6 +194,7 @@ describe("handleNewBooking", () => {
meetingUrl: "https://UNUSED_URL",
},
],
iCalUID: createdBooking.iCalUID,
});
expectWorkflowToBeTriggered();
@@ -201,6 +203,8 @@ describe("handleNewBooking", () => {
videoCallUrl: "http://mock-dailyvideo.example.com/meeting-1",
});
const iCalUID = expectICalUIDAsString(createdBooking.iCalUID);
expectSuccessfulBookingCreationEmails({
booking: {
uid: createdBooking.uid!,
@@ -209,7 +213,7 @@ describe("handleNewBooking", () => {
booker,
organizer,
emails,
iCalUID: "MOCKED_GOOGLE_CALENDAR_ICS_ID",
iCalUID,
});
expectBookingCreatedWebhookToHaveBeenFired({
@@ -290,7 +294,6 @@ describe("handleNewBooking", () => {
create: {
id: "GOOGLE_CALENDAR_EVENT_ID",
uid: "MOCK_ID",
iCalUID: "MOCKED_GOOGLE_CALENDAR_ICS_ID",
},
});
@@ -342,6 +345,7 @@ describe("handleNewBooking", () => {
meetingUrl: "https://UNUSED_URL",
},
],
iCalUID: createdBooking.iCalUID,
});
expectWorkflowToBeTriggered();
@@ -353,6 +357,8 @@ describe("handleNewBooking", () => {
calendarId: null,
});
const iCalUID = expectICalUIDAsString(createdBooking.iCalUID);
expectSuccessfulBookingCreationEmails({
booking: {
uid: createdBooking.uid!,
@@ -360,7 +366,7 @@ describe("handleNewBooking", () => {
booker,
organizer,
emails,
iCalUID: "MOCKED_GOOGLE_CALENDAR_ICS_ID",
iCalUID,
});
expectBookingCreatedWebhookToHaveBeenFired({
booker,
@@ -441,7 +447,6 @@ describe("handleNewBooking", () => {
create: {
uid: "MOCK_ID",
id: "GOOGLE_CALENDAR_EVENT_ID",
iCalUID: "MOCKED_GOOGLE_CALENDAR_ICS_ID",
},
});
@@ -493,6 +498,7 @@ describe("handleNewBooking", () => {
meetingUrl: "https://UNUSED_URL",
},
],
iCalUID: createdBooking.iCalUID,
});
expectWorkflowToBeTriggered();
@@ -501,6 +507,8 @@ describe("handleNewBooking", () => {
videoCallUrl: "http://mock-dailyvideo.example.com/meeting-1",
});
const iCalUID = expectICalUIDAsString(createdBooking.iCalUID);
expectSuccessfulBookingCreationEmails({
booking: {
uid: createdBooking.uid!,
@@ -508,7 +516,7 @@ describe("handleNewBooking", () => {
booker,
organizer,
emails,
iCalUID: "MOCKED_GOOGLE_CALENDAR_ICS_ID",
iCalUID,
});
expectBookingCreatedWebhookToHaveBeenFired({
@@ -705,7 +713,6 @@ describe("handleNewBooking", () => {
create: {
uid: "MOCK_ID",
id: "GOOGLE_CALENDAR_EVENT_ID",
iCalUID: "MOCKED_GOOGLE_CALENDAR_ICS_ID",
},
});
@@ -757,6 +764,7 @@ describe("handleNewBooking", () => {
meetingUrl: "https://UNUSED_URL",
},
],
iCalUID: createdBooking.iCalUID,
});
expectWorkflowToBeTriggered();
@@ -765,6 +773,8 @@ describe("handleNewBooking", () => {
videoCallUrl: "http://mock-dailyvideo.example.com/meeting-1",
});
const iCalUID = expectICalUIDAsString(createdBooking.iCalUID);
expectSuccessfulBookingCreationEmails({
booking: {
uid: createdBooking.uid!,
@@ -772,7 +782,7 @@ describe("handleNewBooking", () => {
booker,
organizer,
emails,
iCalUID: "MOCKED_GOOGLE_CALENDAR_ICS_ID",
iCalUID,
});
expectBookingCreatedWebhookToHaveBeenFired({
@@ -998,6 +1008,8 @@ describe("handleNewBooking", () => {
});
const createdBooking = await handleNewBooking(req);
const iCalUID = expectICalUIDAsString(createdBooking.iCalUID);
expectSuccessfulBookingCreationEmails({
booking: {
uid: createdBooking.uid!,
@@ -1005,8 +1017,7 @@ describe("handleNewBooking", () => {
booker,
organizer,
emails,
// Because no calendar was involved, we don't have an ics UID
iCalUID: createdBooking.uid!,
iCalUID,
});
expectBookingCreatedWebhookToHaveBeenFired({
@@ -1561,11 +1572,7 @@ describe("handleNewBooking", () => {
metadataLookupKey: "dailyvideo",
});
mockCalendarToHaveNoBusySlots("googlecalendar", {
create: {
iCalUID: "MOCKED_GOOGLE_CALENDAR_ICS_ID",
},
});
mockCalendarToHaveNoBusySlots("googlecalendar", {});
const mockBookingData = getMockRequestDataForBooking({
data: {
@@ -1599,10 +1606,13 @@ describe("handleNewBooking", () => {
uid: createdBooking.uid!,
eventTypeId: mockBookingData.eventTypeId,
status: BookingStatus.ACCEPTED,
iCalUID: createdBooking.iCalUID,
});
expectWorkflowToBeTriggered();
const iCalUID = expectICalUIDAsString(createdBooking.iCalUID);
expectSuccessfulBookingCreationEmails({
booking: {
uid: createdBooking.uid!,
@@ -1610,7 +1620,7 @@ describe("handleNewBooking", () => {
booker,
organizer,
emails,
iCalUID: "MOCKED_GOOGLE_CALENDAR_ICS_ID",
iCalUID,
});
expectBookingCreatedWebhookToHaveBeenFired({
@@ -1686,11 +1696,7 @@ describe("handleNewBooking", () => {
metadataLookupKey: "dailyvideo",
});
mockCalendarToHaveNoBusySlots("googlecalendar", {
create: {
iCalUID: "MOCKED_GOOGLE_CALENDAR_ICS_ID",
},
});
mockCalendarToHaveNoBusySlots("googlecalendar", {});
const mockBookingData = getMockRequestDataForBooking({
data: {
@@ -1724,6 +1730,7 @@ describe("handleNewBooking", () => {
uid: createdBooking.uid!,
eventTypeId: mockBookingData.eventTypeId,
status: BookingStatus.PENDING,
iCalUID: createdBooking.iCalUID,
});
expectWorkflowToBeTriggered();
@@ -1873,11 +1880,7 @@ describe("handleNewBooking", () => {
apps: [TestData.apps["google-calendar"], TestData.apps["daily-video"]],
});
mockCalendarToHaveNoBusySlots("googlecalendar", {
create: {
iCalUID: "MOCKED_GOOGLE_CALENDAR_ICS_ID",
},
});
mockCalendarToHaveNoBusySlots("googlecalendar", {});
await createBookingScenario(scenarioData);
const createdBooking = await handleNewBooking(req);
@@ -1896,10 +1899,13 @@ describe("handleNewBooking", () => {
uid: createdBooking.uid!,
eventTypeId: mockBookingData.eventTypeId,
status: BookingStatus.ACCEPTED,
iCalUID: createdBooking.iCalUID,
});
expectWorkflowToBeTriggered();
const iCalUID = expectICalUIDAsString(createdBooking.iCalUID);
expectSuccessfulBookingCreationEmails({
booking: {
uid: createdBooking.uid!,
@@ -1907,7 +1913,7 @@ describe("handleNewBooking", () => {
booker,
organizer,
emails,
iCalUID: "MOCKED_GOOGLE_CALENDAR_ICS_ID",
iCalUID,
});
expectBookingCreatedWebhookToHaveBeenFired({
booker,
@@ -75,6 +75,7 @@ describe("handleNewBooking", () => {
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const uidOfBookingToBeRescheduled = "n5Wv3eHgconAED2j4gcVhP";
const iCalUID = `${uidOfBookingToBeRescheduled}@Cal.com`;
await createBookingScenario(
getScenarioData({
webhooks: [
@@ -128,6 +129,7 @@ describe("handleNewBooking", () => {
credentialId: undefined,
},
],
iCalUID,
},
],
organizer,
@@ -145,7 +147,7 @@ describe("handleNewBooking", () => {
},
update: {
uid: "UPDATED_MOCK_ID",
iCalUID: "MOCKED_GOOGLE_CALENDAR_ICS_ID",
iCalUID,
},
});
@@ -259,7 +261,7 @@ describe("handleNewBooking", () => {
booker,
organizer,
emails,
iCalUID: "MOCKED_GOOGLE_CALENDAR_ICS_ID",
iCalUID,
appsStatus: [
getMockPassingAppStatus({ slug: appStoreMetadata.dailyvideo.slug }),
getMockPassingAppStatus({ slug: appStoreMetadata.googlecalendar.slug }),
@@ -278,7 +280,7 @@ describe("handleNewBooking", () => {
);
test(
`should rechedule a booking successfully and update the event in the same externalCalendarId as was used in the booking earlier.
`should reschedule a booking successfully and update the event in the same externalCalendarId as was used in the booking earlier.
1. Should cancel the existing booking
2. Should create a new booking in the database
3. Should send emails to the booker as well as organizer
@@ -302,6 +304,7 @@ describe("handleNewBooking", () => {
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const uidOfBookingToBeRescheduled = "n5Wv3eHgconAED2j4gcVhP";
const iCalUID = `${uidOfBookingToBeRescheduled}@Cal.com`;
await createBookingScenario(
getScenarioData({
webhooks: [
@@ -355,6 +358,7 @@ describe("handleNewBooking", () => {
credentialId: undefined,
},
],
iCalUID,
},
],
organizer,
@@ -371,7 +375,7 @@ describe("handleNewBooking", () => {
uid: "MOCK_ID",
},
update: {
iCalUID: "MOCKED_GOOGLE_CALENDAR_ICS_ID",
iCalUID,
uid: "UPDATED_MOCK_ID",
},
});
@@ -467,7 +471,7 @@ describe("handleNewBooking", () => {
booker,
organizer,
emails,
iCalUID: "MOCKED_GOOGLE_CALENDAR_ICS_ID",
iCalUID,
});
expectBookingRescheduledWebhookToHaveBeenFired({
booker,
@@ -1117,6 +1121,7 @@ describe("handleNewBooking", () => {
});
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const uidOfBookingToBeRescheduled = "n5Wv3eHgconAED2j4gcVhP";
const iCalUID = `${uidOfBookingToBeRescheduled}@Cal.com`;
const scenarioData = getScenarioData({
webhooks: [
@@ -1168,6 +1173,7 @@ describe("handleNewBooking", () => {
credentialId: 1,
}),
],
iCalUID,
},
],
organizer,
@@ -202,10 +202,12 @@ export default function ApiKeyDialogForm({
);
}}
/>
{!watchNeverExpires && <span className="text-subtle mt-2 text-xs">
{t("api_key_expires_on")}
<span className="font-bold"> {dayjs(expiryDate).format("DD-MM-YYYY")}</span>
</span>}
{!watchNeverExpires && (
<span className="text-subtle mt-2 text-xs">
{t("api_key_expires_on")}
<span className="font-bold"> {dayjs(expiryDate).format("DD-MM-YYYY")}</span>
</span>
)}
</div>
)}