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>
This commit is contained in:
Joe Au-Yeung 2022-05-16 16:20:09 -04:00 committed by GitHub
parent 4e97d3ed72
commit ad126efee8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 30 additions and 14 deletions

View File

@ -51,6 +51,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
select: {
uid: true,
type: true,
externalCalendarId: true,
},
},
payment: true,
@ -163,11 +164,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const apiDeletes = async.mapLimit(bookingToDelete.user.credentials, 5, async (credential: Credential) => {
const bookingRefUid = bookingToDelete.references.filter((ref) => ref.type === credential.type)[0]?.uid;
const bookingExternalCalendarId = bookingToDelete.references.filter(
(ref) => ref.type === credential.type
)[0]?.externalCalendarId;
if (bookingRefUid) {
if (credential.type.endsWith("_calendar")) {
const calendar = getCalendar(credential);
return calendar?.deleteEvent(bookingRefUid, evt);
return calendar?.deleteEvent(bookingRefUid, evt, bookingExternalCalendarId);
} else if (credential.type.endsWith("_video")) {
return deleteMeeting(credential, bookingRefUid);
}

View File

@ -14,6 +14,7 @@ import type {
IntegrationCalendar,
NewCalendarEventType,
} from "@calcom/types/Calendar";
import type { PartialReference } from "@calcom/types/EventManager";
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
@ -162,7 +163,7 @@ export default class GoogleCalendarService implements Calendar {
});
}
async updateEvent(uid: string, event: CalendarEvent): Promise<any> {
async updateEvent(uid: string, event: CalendarEvent, externalCalendarId: string): Promise<any> {
return new Promise(async (resolve, reject) => {
const auth = await this.auth;
const myGoogleAuth = await auth.getToken();
@ -194,9 +195,7 @@ export default class GoogleCalendarService implements Calendar {
calendar.events.update(
{
auth: myGoogleAuth,
calendarId: event.destinationCalendar?.externalId
? event.destinationCalendar.externalId
: "primary",
calendarId: externalCalendarId ? externalCalendarId : event.destinationCalendar?.externalId,
eventId: uid,
sendNotifications: true,
sendUpdates: "all",
@ -214,7 +213,7 @@ export default class GoogleCalendarService implements Calendar {
});
}
async deleteEvent(uid: string, event: CalendarEvent): Promise<void> {
async deleteEvent(uid: string, event: CalendarEvent, externalCalendarId: string): Promise<void> {
return new Promise(async (resolve, reject) => {
const auth = await this.auth;
const myGoogleAuth = await auth.getToken();
@ -225,9 +224,7 @@ export default class GoogleCalendarService implements Calendar {
calendar.events.delete(
{
auth: myGoogleAuth,
calendarId: event.destinationCalendar?.externalId
? event.destinationCalendar.externalId
: "primary",
calendarId: externalCalendarId ? externalCalendarId : event.destinationCalendar?.externalId,
eventId: uid,
sendNotifications: true,
sendUpdates: "all",

View File

@ -128,7 +128,8 @@ export const createEvent = async (credential: Credential, calEvent: CalendarEven
export const updateEvent = async (
credential: Credential,
calEvent: CalendarEvent,
bookingRefUid: string | null
bookingRefUid: string | null,
externalCalendarId: string | null
): Promise<EventResult> => {
const uid = getUid(calEvent);
const calendar = getCalendar(credential);
@ -139,7 +140,7 @@ export const updateEvent = async (
const updatedResult =
calendar && bookingRefUid
? await calendar
.updateEvent(bookingRefUid, calEvent)
.updateEvent(bookingRefUid, calEvent, externalCalendarId)
.then(() => (success = true))
.catch((e) => {
log.error("updateEvent failed", e, calEvent);

View File

@ -146,6 +146,7 @@ export default class EventManager {
meetingId: result.createdEvent?.id.toString(),
meetingPassword: result.createdEvent?.password,
meetingUrl: result.createdEvent?.url,
externalCalendarId: evt.destinationCalendar?.externalId,
};
});
@ -188,6 +189,7 @@ export default class EventManager {
meetingId: true,
meetingPassword: true,
meetingUrl: true,
externalCalendarId: true,
},
},
destinationCalendar: true,
@ -354,7 +356,11 @@ export default class EventManager {
? booking.references.filter((ref) => ref.type === credential.type && !!ref.uid)[0]?.uid
: null;
return updateEvent(credential, event, bookingRefUid);
const bookingExternalCalendarId = booking.references
? booking.references.filter((ref) => ref.type === credential.type)[0].externalCalendarId
: null;
return updateEvent(credential, event, bookingRefUid, bookingExternalCalendarId!);
});
}

View File

@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "BookingReference" ADD COLUMN "externalCalendarId" TEXT;

View File

@ -223,6 +223,7 @@ model BookingReference {
meetingUrl String?
booking Booking? @relation(fields: [bookingId], references: [id], onDelete: Cascade)
bookingId Int?
externalCalendarId String?
deleted Boolean?
}

View File

@ -140,9 +140,13 @@ export interface IntegrationCalendar extends Ensure<Partial<SelectedCalendar>, "
export interface Calendar {
createEvent(event: CalendarEvent): Promise<NewCalendarEventType>;
updateEvent(uid: string, event: CalendarEvent): Promise<Event | Event[]>;
updateEvent(
uid: string,
event: CalendarEvent,
externalCalendarId?: string | null
): Promise<Event | Event[]>;
deleteEvent(uid: string, event: CalendarEvent): Promise<unknown>;
deleteEvent(uid: string, event: CalendarEvent, externalCalendarId?: string | null): Promise<unknown>;
getAvailability(
dateFrom: string,

View File

@ -8,6 +8,7 @@ export interface PartialReference {
meetingId?: string | null;
meetingPassword?: string | null;
meetingUrl?: string | null;
externalCalendarId?: string | null;
}
export interface EventResult {