* perf: move to disable prisma client extension inference * Prisma doesn't like it when you pass Record<string, unknown> * API v1 type fixes * Missed one * Fix unit test fail due to faulty expect * Assigning to prisma InputJsonValue/Array must be done as object, not interface * Fix @calcom/web ts error, teams not defined * Run eslint formatter * fixed the routingFormHelpers file causing a failing app store e2e test
94 lines
2.6 KiB
TypeScript
94 lines
2.6 KiB
TypeScript
import type z from "zod";
|
|
|
|
import logger from "@calcom/lib/logger";
|
|
import { CredentialRepository } from "@calcom/lib/server/repository/credential";
|
|
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,
|
|
},
|
|
});
|
|
}
|
|
};
|