Files
calendar/packages/app-store/_utils/oauth/updateTokenObject.ts
T
Benny JooandGitHub e91bf53d80 refactor: Remove circular deps between @calcom/lib and @calcom/features [2] (#24438)
* move SystemField to features

* migrate workflow service

* merge two tests for team repository

* update imports and migrate team repository

* migrate delegation credential repository

* migrate credential repository

* migrate entityPermissionUtils

* migrate hashedLink service and repository

* migrate membership service

* update imports

* remove file

* migrate buildEventUrlFromBooking

* migrate getAllUserBookings to features

* update imports

* update organizationMock

* migrate slots

* migrate date-ranges to schedules dir

* migrate getAggregatedAvailability

* fix

* refactor

* migrate useCreateEventType hook to features

* migrate assignValueToUser

* migrate validateUsername to auth features

* migrate system field back to lib

* migrate getLabelValueMapFromResponses back to lib

* update imports

* use relative path

* fix type checks

* fix

* fix

* fix tests

* update gh codeowners

* fix

* fix
2025-10-17 06:48:08 -03:00

94 lines
2.6 KiB
TypeScript

import type z from "zod";
import logger from "@calcom/lib/logger";
import { CredentialRepository } from "@calcom/features/credentials/repositories/CredentialRepository";
import { prisma } from "@calcom/prisma";
import type { Prisma } from "@calcom/prisma/client";
import type { OAuth2UniversalSchemaWithCalcomBackwardCompatibility } from "./universalSchema";
const log = logger.getSubLogger({ prefix: ["_utils", "oauth", "updateTokenObject"] });
/**
* @deprecated Use updateTokenObjectInDb instead
*/
export const updateTokenObject = async ({
tokenObject,
credentialId,
}: {
tokenObject: z.infer<typeof OAuth2UniversalSchemaWithCalcomBackwardCompatibility>;
credentialId: number;
}) => {
await prisma.credential.update({
where: {
id: credentialId,
},
data: {
key: tokenObject as unknown as Prisma.InputJsonValue,
},
});
};
/**
* OAuthManager helper to update the token object in db.
*
* It ensures that the token goes in DB. For JWT flow, it also creates a delegation user credential if not present
*/
export const updateTokenObjectInDb = async (
args: {
tokenObject: z.infer<typeof OAuth2UniversalSchemaWithCalcomBackwardCompatibility>;
} & (
| {
authStrategy: "jwt";
userId: number | null;
credentialType: string;
appId: string;
delegatedToId: string | null;
}
| {
authStrategy: "oauth";
credentialId: number;
}
)
) => {
const { tokenObject } = args;
if (args.authStrategy === "jwt") {
const { userId, delegatedToId, credentialType, appId } = args;
if (!userId) {
log.error("Cannot update token object in DB for Delegation as userId is not present");
return;
}
if (!delegatedToId) {
log.error("Cannot update token object in DB for Delegation as delegatedToId is not present");
return;
}
const updated = await CredentialRepository.updateWhereUserIdAndDelegationCredentialId({
userId,
delegationCredentialId: delegatedToId,
data: {
key: tokenObject as Prisma.InputJsonValue,
},
});
// If no delegation-credential is updated, create one
if (updated.count === 0) {
log.debug("No delegation-credential found. Creating one");
await CredentialRepository.createDelegationCredential({
userId,
delegationCredentialId: delegatedToId,
type: credentialType,
key: tokenObject as Prisma.InputJsonValue,
appId,
});
}
} else {
const { credentialId } = args;
await CredentialRepository.updateWhereId({
id: credentialId,
data: {
key: tokenObject as Prisma.InputJsonValue,
},
});
}
};