Skip to content

Commit ff3aaad

Browse files
fix: update delegation credentials api-v2 (calcom#21739)
* fix: update delegation credentials api-v2 * fixup! fix: update delegation credentials api-v2
1 parent 11bddbb commit ff3aaad

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

apps/api/v2/src/modules/organizations/delegation-credentials/organizations-delegation-credential.repository.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { PrismaReadService } from "@/modules/prisma/prisma-read.service";
2+
import { PrismaWriteService } from "@/modules/prisma/prisma-write.service";
23
import { Injectable } from "@nestjs/common";
34

45
import { Prisma } from "@calcom/prisma/client";
56

67
@Injectable()
78
export class OrganizationsDelegationCredentialRepository {
8-
constructor(private readonly dbRead: PrismaReadService) {}
9+
constructor(private readonly dbRead: PrismaReadService, private readonly dbWrite: PrismaWriteService) {}
910

1011
async findById(delegationCredentialId: string) {
1112
return this.dbRead.prisma.delegationCredential.findUnique({ where: { id: delegationCredentialId } });
@@ -22,7 +23,7 @@ export class OrganizationsDelegationCredentialRepository {
2223
delegationCredentialId: string,
2324
data: Prisma.DelegationCredentialUncheckedUpdateInput
2425
) {
25-
return this.dbRead.prisma.delegationCredential.update({
26+
return this.dbWrite.prisma.delegationCredential.update({
2627
where: { id: delegationCredentialId },
2728
data,
2829
include: { workspacePlatform: true },

apps/api/v2/src/modules/organizations/delegation-credentials/services/organizations-delegation-credential.service.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class OrganizationsDelegationCredentialService {
4747
delegatedServiceAccountUser: User,
4848
body: UpdateDelegationCredentialInput
4949
) {
50-
const delegationCredential =
50+
let delegationCredential =
5151
await this.organizationsDelegationCredentialRepository.findByIdWithWorkspacePlatform(
5252
delegationCredentialId
5353
);
@@ -56,6 +56,14 @@ export class OrganizationsDelegationCredentialService {
5656
throw new NotFoundException(`DelegationCredential with id ${delegationCredentialId} not found`);
5757
}
5858

59+
if (body.serviceAccountKey !== undefined) {
60+
const updatedDelegationCredential = await this.updateDelegationCredentialServiceAccountKey(
61+
delegationCredential.id,
62+
body.serviceAccountKey
63+
);
64+
delegationCredential = updatedDelegationCredential ?? delegationCredential;
65+
}
66+
5967
if (body.enabled !== undefined) {
6068
await this.updateDelegationCredentialEnabled(
6169
orgId,
@@ -64,16 +72,13 @@ export class OrganizationsDelegationCredentialService {
6472
body.enabled
6573
);
6674
}
67-
if (body.serviceAccountKey !== undefined) {
68-
await this.updateDelegationCredentialServiceAccountKey(delegationCredentialId, body.serviceAccountKey);
69-
}
7075

7176
// once delegation credentials are enabled, slowly set all the destination calendars of delegated users
7277
if (body.enabled === true && delegationCredential.enabled === false) {
7378
await this.ensureDefaultCalendars(orgId, delegationCredential.domain);
7479
}
7580

76-
return { ...delegationCredential, enabled: body?.enabled ?? delegationCredential?.enabled };
81+
return { ...delegationCredential, enabled: body?.enabled ?? delegationCredential.enabled };
7782
}
7883

7984
async ensureDefaultCalendars(orgId: number, domain: string) {
@@ -132,6 +137,7 @@ export class OrganizationsDelegationCredentialService {
132137
delegationCredentialId,
133138
{
134139
serviceAccountKey: encryptedServiceAccountKey,
140+
enabled: false,
135141
}
136142
);
137143
return delegationCredential;

0 commit comments

Comments
 (0)