Scheduling infrastructure for absolutely everyone.
fix: Connect selected calendar to credential (#11283)
Joe Au-Yeung3d4a3c7a9391
diff --git a/apps/web/components/apps/CalendarListContainer.tsx b/apps/web/components/apps/CalendarListContainer.tsxindex 8afd92845ee..87bce77cdce 100644--- a/apps/web/components/apps/CalendarListContainer.tsx+++ b/apps/web/components/apps/CalendarListContainer.tsx@@ -116,6 +116,7 @@ function ConnectedCalendarsList(props: Props) {type={item.integration.type}isChecked={cal.isSelected}destination={cal.externalId === props.destinationCalendarId}+ credentialId={cal.credentialId}/>))}</ul>diff --git a/apps/web/components/getting-started/components/ConnectedCalendarItem.tsx b/apps/web/components/getting-started/components/ConnectedCalendarItem.tsxindex f859274a5ea..a611fe69b94 100644--- a/apps/web/components/getting-started/components/ConnectedCalendarItem.tsx+++ b/apps/web/components/getting-started/components/ConnectedCalendarItem.tsx@@ -53,6 +53,7 @@ const ConnectedCalendarItem = (prop: IConnectedCalendarItem) => {<ul className="p-4">{calendars?.map((calendar, i) => (<CalendarSwitch+ credentialId={calendar.credentialId}key={calendar.externalId}externalId={calendar.externalId}title={calendar.name || "Nameless Calendar"}diff --git a/apps/web/pages/api/availability/calendar.ts b/apps/web/pages/api/availability/calendar.tsindex c8ebfe3d5e4..5482c58e2e8 100644--- a/apps/web/pages/api/availability/calendar.ts+++ b/apps/web/pages/api/availability/calendar.ts@@ -9,6 +9,7 @@ import prisma from "@calcom/prisma";const selectedCalendarSelectSchema = z.object({integration: z.string(),externalId: z.string(),+ credentialId: z.number().optional(),});export default async function handler(req: NextApiRequest, res: NextApiResponse) {@@ -37,7 +38,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)const { credentials, ...user } = userWithCredentials;if (req.method === "POST") {- const { integration, externalId } = selectedCalendarSelectSchema.parse(req.body);+ const { integration, externalId, credentialId } = selectedCalendarSelectSchema.parse(req.body);await prisma.selectedCalendar.upsert({where: {userId_integration_externalId: {@@ -50,6 +51,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)userId: user.id,integration,externalId,+ credentialId,},// already existsupdate: {},diff --git a/apps/web/pages/settings/my-account/calendars.tsx b/apps/web/pages/settings/my-account/calendars.tsxindex 60154fa7e79..f9c630d4738 100644--- a/apps/web/pages/settings/my-account/calendars.tsx+++ b/apps/web/pages/settings/my-account/calendars.tsx@@ -182,6 +182,7 @@ const CalendarsView = () => {{item.calendars.map((cal) => (<CalendarSwitchkey={cal.externalId}+ credentialId={cal.credentialId}externalId={cal.externalId}title={cal.name || "Nameless calendar"}name={cal.name || "Nameless calendar"}diff --git a/packages/features/calendars/CalendarSwitch.tsx b/packages/features/calendars/CalendarSwitch.tsxindex d1fa6cd23fc..ba91da1aff9 100644--- a/packages/features/calendars/CalendarSwitch.tsx+++ b/packages/features/calendars/CalendarSwitch.tsx@@ -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) {diff --git a/packages/prisma/migrations/20230911172537_connect_selected_calendar_to_credential/migration.sql b/packages/prisma/migrations/20230911172537_connect_selected_calendar_to_credential/migration.sqlnew file mode 100644index 00000000000..5d16cc6bad8--- /dev/null+++ b/packages/prisma/migrations/20230911172537_connect_selected_calendar_to_credential/migration.sql@@ -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;diff --git a/packages/prisma/schema.prisma b/packages/prisma/schema.prismaindex 199c2ea2af3..ffcf80e83b9 100644--- a/packages/prisma/schema.prisma+++ b/packages/prisma/schema.prisma@@ -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])diff --git a/packages/trpc/server/routers/loggedInViewer/deleteCredential.handler.ts b/packages/trpc/server/routers/loggedInViewer/deleteCredential.handler.tsindex 2b6e4446337..fa6de6b98b0 100644--- a/packages/trpc/server/routers/loggedInViewer/deleteCredential.handler.ts+++ b/packages/trpc/server/routers/loggedInViewer/deleteCredential.handler.ts@@ -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 zapierif (credential.app?.slug === "zapier") {await prisma.apiKey.deleteMany({@@ -372,4 +340,46 @@ export const deleteCredentialHandler = async ({ ctx, input }: DeleteCredentialOpid: 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}`+ );+ });+ });+ }+ }+ }};diff --git a/packages/types/Calendar.d.ts b/packages/types/Calendar.d.tsindex 0196cf6cb90..6cdd12f8818 100644--- a/packages/types/Calendar.d.ts+++ b/packages/types/Calendar.d.ts@@ -214,7 +214,7 @@ export interface IntegrationCalendar extends Ensure<Partial<SelectedCalendar>, "// For displaying the connected email addressemail?: string;primaryEmail?: string;- credentialId?: number;+ credentialId?: number | null;integrationTitle?: string;}