Files
calendar/packages/trpc/server/routers/loggedInViewer/unlinkConnectedAccount.handler.ts
T
3c1297aa72 fix: calcom trpc sessio circle dep (#19893)
* fix trpc session circle dep

* fixes trpc session cirlce dep

* fix relative imports

* fix more imports to use types and not trpc

* fix exports

* Fixed types

---------

Co-authored-by: Keith Williams <keithwillcode@gmail.com>
2025-03-19 05:50:22 -03:00

45 lines
1.4 KiB
TypeScript

import type { GetServerSidePropsContext, NextApiResponse } from "next";
import { prisma } from "@calcom/prisma";
import { IdentityProvider } from "@calcom/prisma/enums";
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
type UpdateProfileOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
res?: NextApiResponse | GetServerSidePropsContext["res"];
};
};
const unlinkConnectedAccount = async ({ ctx }: UpdateProfileOptions) => {
const { user } = ctx;
// Unlink the account
const CalComAdapter = (await import("@calcom/features/auth/lib/next-auth-custom-adapter")).default;
const calcomAdapter = CalComAdapter(prisma);
// If it fails to delete, don't stop because the users login data might not be present
try {
await calcomAdapter.unlinkAccount({
provider: user.identityProvider.toLocaleLowerCase(),
providerAccountId: user.identityProviderId || "",
});
} catch {
// Fail silently if we don't have a record in the account table
}
// Fall back to the default identity provider
const _user = await prisma.user.update({
where: {
id: user.id,
identityProvider: IdentityProvider.GOOGLE,
identityProviderId: { not: null },
},
data: {
identityProvider: IdentityProvider.CAL,
identityProviderId: null,
},
});
if (!_user) return { message: "account_unlinked_error" };
return { message: "account_unlinked_success" };
};
export default unlinkConnectedAccount;