From ff3aaadbaac7b0f133c5a83c1b6eee28f5bd063d Mon Sep 17 00:00:00 2001 From: Morgan <33722304+ThyMinimalDev@users.noreply.github.com> Date: Mon, 9 Jun 2025 14:46:38 +0300 Subject: [PATCH] fix: update delegation credentials api-v2 (#21739) * fix: update delegation credentials api-v2 * fixup! fix: update delegation credentials api-v2 --- ...nizations-delegation-credential.repository.ts | 5 +++-- ...rganizations-delegation-credential.service.ts | 16 +++++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/apps/api/v2/src/modules/organizations/delegation-credentials/organizations-delegation-credential.repository.ts b/apps/api/v2/src/modules/organizations/delegation-credentials/organizations-delegation-credential.repository.ts index a9cb1a297d..064a30c318 100644 --- a/apps/api/v2/src/modules/organizations/delegation-credentials/organizations-delegation-credential.repository.ts +++ b/apps/api/v2/src/modules/organizations/delegation-credentials/organizations-delegation-credential.repository.ts @@ -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 }, diff --git a/apps/api/v2/src/modules/organizations/delegation-credentials/services/organizations-delegation-credential.service.ts b/apps/api/v2/src/modules/organizations/delegation-credentials/services/organizations-delegation-credential.service.ts index eadaaad4af..ac533eb189 100644 --- a/apps/api/v2/src/modules/organizations/delegation-credentials/services/organizations-delegation-credential.service.ts +++ b/apps/api/v2/src/modules/organizations/delegation-credentials/services/organizations-delegation-credential.service.ts @@ -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;