Skip to content

Commit ad126ef

Browse files
joeauyeungkodiakhq[bot]zomars
authored
Add Google cal extneral calendar id to booking reference (#2671)
* Set google cal event id to use our uid * Save calendar external id to bookingRef * Pass external calendar ids to update and delete * Create migration * Fix type errors * Fix prisma url Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: Omar López <zomars@me.com>
1 parent 4e97d3e commit ad126ef

File tree

8 files changed

+30
-14
lines changed

8 files changed

+30
-14
lines changed

apps/web/pages/api/cancel.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
5151
select: {
5252
uid: true,
5353
type: true,
54+
externalCalendarId: true,
5455
},
5556
},
5657
payment: true,
@@ -163,11 +164,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
163164

164165
const apiDeletes = async.mapLimit(bookingToDelete.user.credentials, 5, async (credential: Credential) => {
165166
const bookingRefUid = bookingToDelete.references.filter((ref) => ref.type === credential.type)[0]?.uid;
167+
const bookingExternalCalendarId = bookingToDelete.references.filter(
168+
(ref) => ref.type === credential.type
169+
)[0]?.externalCalendarId;
166170
if (bookingRefUid) {
167171
if (credential.type.endsWith("_calendar")) {
168172
const calendar = getCalendar(credential);
169173

170-
return calendar?.deleteEvent(bookingRefUid, evt);
174+
return calendar?.deleteEvent(bookingRefUid, evt, bookingExternalCalendarId);
171175
} else if (credential.type.endsWith("_video")) {
172176
return deleteMeeting(credential, bookingRefUid);
173177
}

packages/app-store/googlecalendar/lib/CalendarService.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
IntegrationCalendar,
1515
NewCalendarEventType,
1616
} from "@calcom/types/Calendar";
17+
import type { PartialReference } from "@calcom/types/EventManager";
1718

1819
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
1920

@@ -162,7 +163,7 @@ export default class GoogleCalendarService implements Calendar {
162163
});
163164
}
164165

165-
async updateEvent(uid: string, event: CalendarEvent): Promise<any> {
166+
async updateEvent(uid: string, event: CalendarEvent, externalCalendarId: string): Promise<any> {
166167
return new Promise(async (resolve, reject) => {
167168
const auth = await this.auth;
168169
const myGoogleAuth = await auth.getToken();
@@ -194,9 +195,7 @@ export default class GoogleCalendarService implements Calendar {
194195
calendar.events.update(
195196
{
196197
auth: myGoogleAuth,
197-
calendarId: event.destinationCalendar?.externalId
198-
? event.destinationCalendar.externalId
199-
: "primary",
198+
calendarId: externalCalendarId ? externalCalendarId : event.destinationCalendar?.externalId,
200199
eventId: uid,
201200
sendNotifications: true,
202201
sendUpdates: "all",
@@ -214,7 +213,7 @@ export default class GoogleCalendarService implements Calendar {
214213
});
215214
}
216215

217-
async deleteEvent(uid: string, event: CalendarEvent): Promise<void> {
216+
async deleteEvent(uid: string, event: CalendarEvent, externalCalendarId: string): Promise<void> {
218217
return new Promise(async (resolve, reject) => {
219218
const auth = await this.auth;
220219
const myGoogleAuth = await auth.getToken();
@@ -225,9 +224,7 @@ export default class GoogleCalendarService implements Calendar {
225224
calendar.events.delete(
226225
{
227226
auth: myGoogleAuth,
228-
calendarId: event.destinationCalendar?.externalId
229-
? event.destinationCalendar.externalId
230-
: "primary",
227+
calendarId: externalCalendarId ? externalCalendarId : event.destinationCalendar?.externalId,
231228
eventId: uid,
232229
sendNotifications: true,
233230
sendUpdates: "all",

packages/core/CalendarManager.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ export const createEvent = async (credential: Credential, calEvent: CalendarEven
128128
export const updateEvent = async (
129129
credential: Credential,
130130
calEvent: CalendarEvent,
131-
bookingRefUid: string | null
131+
bookingRefUid: string | null,
132+
externalCalendarId: string | null
132133
): Promise<EventResult> => {
133134
const uid = getUid(calEvent);
134135
const calendar = getCalendar(credential);
@@ -139,7 +140,7 @@ export const updateEvent = async (
139140
const updatedResult =
140141
calendar && bookingRefUid
141142
? await calendar
142-
.updateEvent(bookingRefUid, calEvent)
143+
.updateEvent(bookingRefUid, calEvent, externalCalendarId)
143144
.then(() => (success = true))
144145
.catch((e) => {
145146
log.error("updateEvent failed", e, calEvent);

packages/core/EventManager.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ export default class EventManager {
146146
meetingId: result.createdEvent?.id.toString(),
147147
meetingPassword: result.createdEvent?.password,
148148
meetingUrl: result.createdEvent?.url,
149+
externalCalendarId: evt.destinationCalendar?.externalId,
149150
};
150151
});
151152

@@ -188,6 +189,7 @@ export default class EventManager {
188189
meetingId: true,
189190
meetingPassword: true,
190191
meetingUrl: true,
192+
externalCalendarId: true,
191193
},
192194
},
193195
destinationCalendar: true,
@@ -354,7 +356,11 @@ export default class EventManager {
354356
? booking.references.filter((ref) => ref.type === credential.type && !!ref.uid)[0]?.uid
355357
: null;
356358

357-
return updateEvent(credential, event, bookingRefUid);
359+
const bookingExternalCalendarId = booking.references
360+
? booking.references.filter((ref) => ref.type === credential.type)[0].externalCalendarId
361+
: null;
362+
363+
return updateEvent(credential, event, bookingRefUid, bookingExternalCalendarId!);
358364
});
359365
}
360366

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterTable
2+
ALTER TABLE "BookingReference" ADD COLUMN "externalCalendarId" TEXT;

packages/prisma/schema.prisma

+1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ model BookingReference {
223223
meetingUrl String?
224224
booking Booking? @relation(fields: [bookingId], references: [id], onDelete: Cascade)
225225
bookingId Int?
226+
externalCalendarId String?
226227
deleted Boolean?
227228
}
228229

packages/types/Calendar.d.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,13 @@ export interface IntegrationCalendar extends Ensure<Partial<SelectedCalendar>, "
140140
export interface Calendar {
141141
createEvent(event: CalendarEvent): Promise<NewCalendarEventType>;
142142

143-
updateEvent(uid: string, event: CalendarEvent): Promise<Event | Event[]>;
143+
updateEvent(
144+
uid: string,
145+
event: CalendarEvent,
146+
externalCalendarId?: string | null
147+
): Promise<Event | Event[]>;
144148

145-
deleteEvent(uid: string, event: CalendarEvent): Promise<unknown>;
149+
deleteEvent(uid: string, event: CalendarEvent, externalCalendarId?: string | null): Promise<unknown>;
146150

147151
getAvailability(
148152
dateFrom: string,

packages/types/EventManager.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface PartialReference {
88
meetingId?: string | null;
99
meetingPassword?: string | null;
1010
meetingUrl?: string | null;
11+
externalCalendarId?: string | null;
1112
}
1213

1314
export interface EventResult {

0 commit comments

Comments
 (0)