fix: Connect selected calendar to credential (#11283)
release.yml / release (push) Canceled after 0s

* Reduce the unhandled promise

* Connect selected calendar to credential

* Write credential to selected calendar

* Type fix

* Handle catching promises in separate threads

* Only call to list calendars if there are no credentialIds

* Changed type of IntegrationCalendar from undef to null

* Adding missing property to getting started

* Also add calendars.tsx

---------

Co-authored-by: Alex van Andel <[email protected]>
This commit is contained in:
Joe Au-YeungandAlex van Andel authored and GitHub committed 2023-09-12 18:09:05 +01:00
1 parent 1c1625d3fc
commit 3d4a3c7a93
9 files changed
+64 -40

No files matched your search

@@ -15,9 +15,10 @@ interface ICalendarSwitchProps {
name: string;
isLastItemInList?: boolean;
destination?: boolean;
credentialId: number;
}
const CalendarSwitch = (props: ICalendarSwitchProps) => {
const { title, externalId, type, isChecked, name, isLastItemInList = false } = props;
const { title, externalId, type, isChecked, name, isLastItemInList = false, credentialId } = props;
const [checkedInternal, setCheckedInternal] = useState(isChecked);
const utils = trpc.useContext();
const { t } = useLocale();
@@ -40,7 +41,7 @@ const CalendarSwitch = (props: ICalendarSwitchProps) => {
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
body: JSON.stringify({ ...body, credentialId }),
});
if (!res.ok) {
@@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE "SelectedCalendar" ADD COLUMN "credentialId" INTEGER;
-- AddForeignKey
ALTER TABLE "SelectedCalendar" ADD CONSTRAINT "SelectedCalendar_credentialId_fkey" FOREIGN KEY ("credentialId") REFERENCES "Credential"("id") ON DELETE CASCADE ON UPDATE CASCADE;
+7 -4
View File
@@ -137,6 +137,7 @@ model Credential {
// How to make it a required column?
appId String?
destinationCalendars DestinationCalendar[]
selectedCalendars SelectedCalendar[]
invalid Boolean? @default(false)
@@index([userId])
@@ -448,10 +449,12 @@ model Availability {
}
model SelectedCalendar {
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId Int
integration String
externalId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId Int
integration String
externalId String
credential Credential? @relation(fields: [credentialId], references: [id], onDelete: Cascade)
credentialId Int?
@@id([userId, integration, externalId])
@@index([userId])
@@ -307,38 +307,6 @@ export const deleteCredentialHandler = async ({ ctx, input }: DeleteCredentialOp
}
}
// If it's a calendar remove it from the SelectedCalendars
if (credential.app?.categories.includes(AppCategories.calendar)) {
const calendar = await getCalendar(credential);
const calendars = await calendar?.listCalendars();
if (calendars && calendars.length > 0) {
calendars.map(async (cal) => {
prisma.selectedCalendar
.delete({
where: {
userId_integration_externalId: {
userId: user.id,
externalId: cal.externalId,
integration: cal.integration as string,
},
},
})
.catch((error) => {
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2025") {
console.log(
`Error deleting selected calendars for user ${user.id} and calendar ${credential.appId}. Could not find selected calendar.`
);
}
console.log(
`Error deleting selected calendars for user ${user.id} and calendar ${credential.appId} with error: ${error}`
);
});
});
}
}
// if zapier get disconnected, delete zapier apiKey, delete zapier webhooks and cancel all scheduled jobs from zapier
if (credential.app?.slug === "zapier") {
await prisma.apiKey.deleteMany({
@@ -372,4 +340,46 @@ export const deleteCredentialHandler = async ({ ctx, input }: DeleteCredentialOp
id: id,
},
});
// Backwards compatibility. Selected calendars cascade on delete when deleting a credential
// If it's a calendar remove it from the SelectedCalendars
if (credential.app?.categories.includes(AppCategories.calendar)) {
const selectedCalendars = await prisma.selectedCalendar.findMany({
where: {
userId: user.id,
integration: credential.type as string,
},
});
if (selectedCalendars.length) {
const calendar = await getCalendar(credential);
const calendars = await calendar?.listCalendars();
if (calendars && calendars.length > 0) {
calendars.map(async (cal) => {
prisma.selectedCalendar
.delete({
where: {
userId_integration_externalId: {
userId: user.id,
externalId: cal.externalId,
integration: cal.integration as string,
},
},
})
.catch((error) => {
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2025") {
console.log(
`Error deleting selected calendars for user ${user.id} and calendar ${credential.appId}. Could not find selected calendar.`
);
}
console.log(
`Error deleting selected calendars for user ${user.id} and calendar ${credential.appId} with error: ${error}`
);
});
});
}
}
}
};
+1 -1
View File
@@ -214,7 +214,7 @@ export interface IntegrationCalendar extends Ensure<Partial<SelectedCalendar>, "
// For displaying the connected email address
email?: string;
primaryEmail?: string;
credentialId?: number;
credentialId?: number | null;
integrationTitle?: string;
}