fix: update delegation credentials api-v2 (#21739)

* fix: update delegation credentials api-v2

* fixup! fix: update delegation credentials api-v2
This commit is contained in:
Morgan
2025-06-09 11:46:38 +00:00
committed by GitHub
parent 11bddbb71d
commit ff3aaadbaa
2 changed files with 14 additions and 7 deletions
@@ -1,11 +1,12 @@
import { PrismaReadService } from "@/modules/prisma/prisma-read.service";
import { PrismaWriteService } from "@/modules/prisma/prisma-write.service";
import { Injectable } from "@nestjs/common";
import { Prisma } from "@calcom/prisma/client";
@Injectable()
export class OrganizationsDelegationCredentialRepository {
constructor(private readonly dbRead: PrismaReadService) {}
constructor(private readonly dbRead: PrismaReadService, private readonly dbWrite: PrismaWriteService) {}
async findById(delegationCredentialId: string) {
return this.dbRead.prisma.delegationCredential.findUnique({ where: { id: delegationCredentialId } });
@@ -22,7 +23,7 @@ export class OrganizationsDelegationCredentialRepository {
delegationCredentialId: string,
data: Prisma.DelegationCredentialUncheckedUpdateInput
) {
return this.dbRead.prisma.delegationCredential.update({
return this.dbWrite.prisma.delegationCredential.update({
where: { id: delegationCredentialId },
data,
include: { workspacePlatform: true },
@@ -47,7 +47,7 @@ export class OrganizationsDelegationCredentialService {
delegatedServiceAccountUser: User,
body: UpdateDelegationCredentialInput
) {
const delegationCredential =
let delegationCredential =
await this.organizationsDelegationCredentialRepository.findByIdWithWorkspacePlatform(
delegationCredentialId
);
@@ -56,6 +56,14 @@ export class OrganizationsDelegationCredentialService {
throw new NotFoundException(`DelegationCredential with id ${delegationCredentialId} not found`);
}
if (body.serviceAccountKey !== undefined) {
const updatedDelegationCredential = await this.updateDelegationCredentialServiceAccountKey(
delegationCredential.id,
body.serviceAccountKey
);
delegationCredential = updatedDelegationCredential ?? delegationCredential;
}
if (body.enabled !== undefined) {
await this.updateDelegationCredentialEnabled(
orgId,
@@ -64,16 +72,13 @@ export class OrganizationsDelegationCredentialService {
body.enabled
);
}
if (body.serviceAccountKey !== undefined) {
await this.updateDelegationCredentialServiceAccountKey(delegationCredentialId, body.serviceAccountKey);
}
// once delegation credentials are enabled, slowly set all the destination calendars of delegated users
if (body.enabled === true && delegationCredential.enabled === false) {
await this.ensureDefaultCalendars(orgId, delegationCredential.domain);
}
return { ...delegationCredential, enabled: body?.enabled ?? delegationCredential?.enabled };
return { ...delegationCredential, enabled: body?.enabled ?? delegationCredential.enabled };
}
async ensureDefaultCalendars(orgId: number, domain: string) {
@@ -132,6 +137,7 @@ export class OrganizationsDelegationCredentialService {
delegationCredentialId,
{
serviceAccountKey: encryptedServiceAccountKey,
enabled: false,
}
);
return delegationCredential;