Skip to content

Scheduling infrastructure for absolutely everyone.

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

Morganff3aaadbaac7
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 a9cb1a297d0..064a30c3184 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 eadaaad4af0..ac533eb1890 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;