Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent duplicate calendar account linking #13310

Merged
merged 15 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/app-store/googlecalendar/api/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
const primaryCal = cals.data.items?.find((cal) => cal.primary);

if (primaryCal?.id) {
const existingCalendar = await prisma.selectedCalendar.findUnique({
where: {
userId_integration_externalId: {
userId: req.session.user.id,
externalId: primaryCal.id,
integration: "google_calendar",
},
},
});

if (existingCalendar) {
await prisma.credential.delete({
where: {
id: credential.id,
},
});
throw new HttpError({ statusCode: 409, message: "Account is already linked." });
}

await prisma.selectedCalendar.create({
data: {
userId: req.session.user.id,
Expand Down
20 changes: 20 additions & 0 deletions packages/app-store/office365calendar/api/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { NextApiRequest, NextApiResponse } from "next";
import { WEBAPP_URL } from "@calcom/lib/constants";
import { handleErrorsJson } from "@calcom/lib/errors";
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
import { HttpError } from "@calcom/lib/http-error";
import prisma from "@calcom/prisma";

import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
Expand Down Expand Up @@ -110,6 +111,25 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}

if (defaultCalendar?.id && req.session?.user?.id) {
const existingCalendar = await prisma.selectedCalendar.findUnique({
where: {
userId_integration_externalId: {
userId: req.session?.user.id,
integration: "office365_calendar",
externalId: defaultCalendar.id,
},
},
});

if (existingCalendar) {
await prisma.credential.delete({
where: {
id: credential.id,
},
});

throw new HttpError({ statusCode: 409, message: "Account is already linked." });
}
await prisma.selectedCalendar.create({
data: {
userId: req.session?.user.id,
Expand Down
20 changes: 20 additions & 0 deletions packages/app-store/zohocalendar/api/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { WEBAPP_URL } from "@calcom/lib/constants";
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
import { HttpError } from "@calcom/lib/http-error";
import logger from "@calcom/lib/logger";
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
import prisma from "@calcom/prisma";
Expand Down Expand Up @@ -82,9 +83,28 @@
});
const data = await calendarResponse.json();

const primaryCalendar = data.calendars.find((calendar: any) => calendar.isdefault);

Check warning on line 86 in packages/app-store/zohocalendar/api/callback.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app-store/zohocalendar/api/callback.ts#L86

[@typescript-eslint/no-explicit-any] Unexpected any. Specify a different type.

if (primaryCalendar.uid) {
const existingCalendar = await prisma.selectedCalendar.findUnique({
where: {
userId_integration_externalId: {
userId: req.session.user.id,
integration: config.type,
externalId: primaryCalendar.uid,
},
},
});

if (existingCalendar) {
await prisma.credential.delete({
where: {
id: credential.id,
},
});

throw new HttpError({ statusCode: 409, message: "Account is already linked." });
}
await prisma.selectedCalendar.create({
data: {
userId: req.session.user.id,
Expand Down
Loading