Attach credential id to destinationCalendar records (#3197)

This commit is contained in:
Joe Au-Yeung
2022-07-01 21:55:27 +01:00
committed by GitHub
parent 2cbf46a323
commit 201d17264c
8 changed files with 31 additions and 1 deletions
+7 -1
View File
@@ -550,7 +550,11 @@ const loggedInViewerRouter = createProtectedRouter()
const connectedCalendars = await getConnectedCalendars(calendarCredentials, user.selectedCalendars);
const allCals = connectedCalendars.map((cal) => cal.calendars ?? []).flat();
if (!allCals.find((cal) => cal.externalId === externalId && cal.integration === integration)) {
const credentialId = allCals.find(
(cal) => cal.externalId === externalId && cal.integration === integration && cal.readOnly === false
)?.credentialId;
if (!credentialId) {
throw new TRPCError({ code: "BAD_REQUEST", message: `Could not find calendar ${input.externalId}` });
}
@@ -565,11 +569,13 @@ const loggedInViewerRouter = createProtectedRouter()
update: {
integration,
externalId,
credentialId,
},
create: {
...where,
integration,
externalId,
credentialId,
},
});
},
@@ -337,6 +337,7 @@ export default class GoogleCalendarService implements Calendar {
integration: this.integrationName,
name: cal.summary ?? "No name",
primary: cal.primary ?? false,
readOnly: !(cal.accessRole === "reader" || cal.accessRole === "owner") && true,
};
return calendar;
}) || []
@@ -177,6 +177,7 @@ export default class Office365CalendarService implements Calendar {
integration: this.integrationName,
name: cal.name ?? "No calendar name",
primary: cal.isDefaultCalendar ?? false,
readOnly: !cal.canEdit && true,
};
return calendar;
});
+1
View File
@@ -46,6 +46,7 @@ export const getConnectedCalendars = async (
...cal,
primary: cal.primary || null,
isSelected: selectedCalendars.some((selected) => selected.externalId === cal.externalId),
credentialId,
}))
.sortBy(["primary"])
.value();
+12
View File
@@ -282,6 +282,18 @@ export default class EventManager {
/** Can I use destinationCalendar here? */
/* How can I link a DC to a cred? */
if (event.destinationCalendar) {
if (event.destinationCalendar.credentialId) {
const credential = await prisma.credential.findFirst({
where: {
id: event.destinationCalendar.credentialId,
},
});
if (credential) {
return [await createEvent(credential, event)];
}
}
const destinationCalendarCredentials = this.calendarCredentials.filter(
(c) => c.type === event.destinationCalendar?.integration
);
@@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE "DestinationCalendar" ADD COLUMN "credentialId" INTEGER;
-- AddForeignKey
ALTER TABLE "DestinationCalendar" ADD CONSTRAINT "DestinationCalendar_credentialId_fkey" FOREIGN KEY ("credentialId") REFERENCES "Credential"("id") ON DELETE SET NULL ON UPDATE CASCADE;
+3
View File
@@ -89,6 +89,7 @@ model Credential {
app App? @relation(fields: [appId], references: [slug], onDelete: Cascade)
// How to make it a required column?
appId String?
destinationCalendars DestinationCalendar[]
}
enum UserPlan {
@@ -113,6 +114,8 @@ model DestinationCalendar {
bookingId Int? @unique
eventType EventType? @relation(fields: [eventTypeId], references: [id])
eventTypeId Int? @unique
credentialId Int?
credential Credential? @relation(fields: [credentialId], references: [id])
}
enum UserPermissionRole {
+1
View File
@@ -146,6 +146,7 @@ export interface AdditionalInformation {
export interface IntegrationCalendar extends Ensure<Partial<SelectedCalendar>, "externalId"> {
primary?: boolean;
name?: string;
readOnly?: boolean;
}
export interface Calendar {