Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Minimum fix for N Calendar Invites #14617

Merged
merged 3 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/app-store/_utils/calendars/processExternalId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { DestinationCalendar } from "@prisma/client";

import { metadata as OutlookMetadata } from "../../office365calendar";

/**
* When inviting attendees to a calendar event, sometimes the external ID is only used for internal purposes
* Need to process the correct external ID for the calendar service
*/
const processExternalId = (destinationCalendar: DestinationCalendar) => {
if (destinationCalendar.integration === OutlookMetadata.type) {
// Primary email should always be present for Outlook
return destinationCalendar.primaryEmail || destinationCalendar.externalId;
}

return destinationCalendar.externalId;
};

export default processExternalId;
4 changes: 4 additions & 0 deletions packages/core/EventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ export default class EventManager {
};

if (event.destinationCalendar && event.destinationCalendar.length > 0) {
let eventCreated = false;
// Since GCal pushes events to multiple calendars we only want to create one event per booking
let gCalAdded = false;
const destinationCalendars: DestinationCalendar[] = event.destinationCalendar.reduce(
Expand All @@ -554,6 +555,7 @@ export default class EventManager {
[] as DestinationCalendar[]
);
for (const destination of destinationCalendars) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This for ... of loop was the cause of creating multiple calendar events.

if (eventCreated) break;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a calendar event was created then set eventCreated to true. If the event was created on the first destination calendar then break out of the loop.

log.silly("Creating Calendar event", JSON.stringify({ destination }));
if (destination.credentialId) {
let credential = this.calendarCredentials.find((c) => c.id === destination.credentialId);
Expand Down Expand Up @@ -582,6 +584,7 @@ export default class EventManager {
const createdEvent = await createEvent(credential, event, destination.externalId);
if (createdEvent) {
createdEvents.push(createdEvent);
eventCreated = true;
}
}
} else {
Expand All @@ -607,6 +610,7 @@ export default class EventManager {
})
);
createdEvents.push(await createEvent(firstCalendarCredential, event));
eventCreated = true;
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion packages/features/bookings/lib/handleNewBooking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { Logger } from "tslog";
import { v5 as uuidv5 } from "uuid";
import z from "zod";

import processExternalId from "@calcom/app-store/_utils/calendars/processExternalId";
import { metadata as GoogleMeetMetadata } from "@calcom/app-store/googlevideo/_metadata";
import type { LocationObject } from "@calcom/app-store/locations";
import {
Expand Down Expand Up @@ -1405,9 +1406,13 @@ async function handler(

// Organizer or user owner of this event type it's not listed as a team member.
const teamMemberPromises = users.slice(1).map(async (user) => {
// TODO: Add back once EventManager tests are ready https://github.com/calcom/cal.com/pull/14610#discussion_r1567817120
// push to teamDestinationCalendars if it's a team event but collective only
if (isTeamEventType && eventType.schedulingType === "COLLECTIVE" && user.destinationCalendar) {
teamDestinationCalendars.push(user.destinationCalendar);
teamDestinationCalendars.push({
...user.destinationCalendar,
externalId: processExternalId(user.destinationCalendar),
});
}

return {
Expand Down