chore: ensure default calendars with trigger.dev apiv2 (#27058)
* chore: ensure default calendars with trigger.dev apiv2 * test: add unit and e2e tests for CalendarsTasker integration - Add unit tests for CalendarsTasker.dispatch when enableAsyncTasker is true - Add e2e test to verify ensureDefaultCalendarsForUser is called when creating membership - Mock CalendarsTasker and ConfigService in unit tests - Test both async (Trigger.dev) and sync (Bull queue) paths - Fix missing return types on helper functions in e2e tests - Add *.spec.ts to biome test file exceptions for noExcessiveLinesPerFunction rule Co-Authored-By: [email protected] <[email protected]> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
11 files changed
+462
-289
No files matched your search
+10
-5
@@ -1,5 +1,9 @@
|
|||||||
|
import { BullModule } from "@nestjs/bull";
|
||||||
|
import { Module } from "@nestjs/common";
|
||||||
|
import { OrganizationsDelegationCredentialService } from "./services/organizations-delegation-credential.service";
|
||||||
import { CalendarsModule } from "@/ee/calendars/calendars.module";
|
import { CalendarsModule } from "@/ee/calendars/calendars.module";
|
||||||
import { CALENDARS_QUEUE, CalendarsProcessor } from "@/ee/calendars/processors/calendars.processor";
|
import { CALENDARS_QUEUE, CalendarsProcessor } from "@/ee/calendars/processors/calendars.processor";
|
||||||
|
import { CalendarsTaskerModule } from "@/lib/modules/calendars-tasker.module";
|
||||||
import { MembershipsModule } from "@/modules/memberships/memberships.module";
|
import { MembershipsModule } from "@/modules/memberships/memberships.module";
|
||||||
import { OrganizationsDelegationCredentialController } from "@/modules/organizations/delegation-credentials/organizations-delegation-credential.controller";
|
import { OrganizationsDelegationCredentialController } from "@/modules/organizations/delegation-credentials/organizations-delegation-credential.controller";
|
||||||
import { OrganizationsDelegationCredentialRepository } from "@/modules/organizations/delegation-credentials/organizations-delegation-credential.repository";
|
import { OrganizationsDelegationCredentialRepository } from "@/modules/organizations/delegation-credentials/organizations-delegation-credential.repository";
|
||||||
@@ -7,10 +11,6 @@ import { OrganizationsRepository } from "@/modules/organizations/index/organizat
|
|||||||
import { PrismaModule } from "@/modules/prisma/prisma.module";
|
import { PrismaModule } from "@/modules/prisma/prisma.module";
|
||||||
import { RedisModule } from "@/modules/redis/redis.module";
|
import { RedisModule } from "@/modules/redis/redis.module";
|
||||||
import { StripeModule } from "@/modules/stripe/stripe.module";
|
import { StripeModule } from "@/modules/stripe/stripe.module";
|
||||||
import { BullModule } from "@nestjs/bull";
|
|
||||||
import { Module } from "@nestjs/common";
|
|
||||||
|
|
||||||
import { OrganizationsDelegationCredentialService } from "./services/organizations-delegation-credential.service";
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@@ -26,6 +26,7 @@ import { OrganizationsDelegationCredentialService } from "./services/organizatio
|
|||||||
duration: 1000,
|
duration: 1000,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
CalendarsTaskerModule,
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
OrganizationsDelegationCredentialService,
|
OrganizationsDelegationCredentialService,
|
||||||
@@ -34,6 +35,10 @@ import { OrganizationsDelegationCredentialService } from "./services/organizatio
|
|||||||
CalendarsProcessor,
|
CalendarsProcessor,
|
||||||
],
|
],
|
||||||
controllers: [OrganizationsDelegationCredentialController],
|
controllers: [OrganizationsDelegationCredentialController],
|
||||||
exports: [OrganizationsDelegationCredentialRepository, OrganizationsDelegationCredentialService, BullModule],
|
exports: [
|
||||||
|
OrganizationsDelegationCredentialRepository,
|
||||||
|
OrganizationsDelegationCredentialService,
|
||||||
|
BullModule,
|
||||||
|
],
|
||||||
})
|
})
|
||||||
export class OrganizationsDelegationCredentialModule {}
|
export class OrganizationsDelegationCredentialModule {}
|
||||||
+69
@@ -2,9 +2,11 @@ import {
|
|||||||
CALENDARS_QUEUE,
|
CALENDARS_QUEUE,
|
||||||
DEFAULT_CALENDARS_JOB,
|
DEFAULT_CALENDARS_JOB,
|
||||||
} from "@/ee/calendars/processors/calendars.processor";
|
} from "@/ee/calendars/processors/calendars.processor";
|
||||||
|
import { CalendarsTasker } from "@/lib/services/tasker/calendars-tasker.service";
|
||||||
import { OrganizationsDelegationCredentialRepository } from "@/modules/organizations/delegation-credentials/organizations-delegation-credential.repository";
|
import { OrganizationsDelegationCredentialRepository } from "@/modules/organizations/delegation-credentials/organizations-delegation-credential.repository";
|
||||||
import { OrganizationsDelegationCredentialService } from "@/modules/organizations/delegation-credentials/services/organizations-delegation-credential.service";
|
import { OrganizationsDelegationCredentialService } from "@/modules/organizations/delegation-credentials/services/organizations-delegation-credential.service";
|
||||||
import { Logger } from "@nestjs/common";
|
import { Logger } from "@nestjs/common";
|
||||||
|
import { ConfigService } from "@nestjs/config";
|
||||||
import { getQueueToken } from "@nestjs/bull";
|
import { getQueueToken } from "@nestjs/bull";
|
||||||
import { Test, TestingModule } from "@nestjs/testing";
|
import { Test, TestingModule } from "@nestjs/testing";
|
||||||
|
|
||||||
@@ -12,6 +14,8 @@ describe("OrganizationsDelegationCredentialService", () => {
|
|||||||
let service: OrganizationsDelegationCredentialService;
|
let service: OrganizationsDelegationCredentialService;
|
||||||
let mockRepository: OrganizationsDelegationCredentialRepository;
|
let mockRepository: OrganizationsDelegationCredentialRepository;
|
||||||
let mockQueue: { getJob: jest.Mock; add: jest.Mock };
|
let mockQueue: { getJob: jest.Mock; add: jest.Mock };
|
||||||
|
let mockCalendarsTasker: { dispatch: jest.Mock };
|
||||||
|
let mockConfigService: { get: jest.Mock };
|
||||||
|
|
||||||
const orgId = 1;
|
const orgId = 1;
|
||||||
const domain = "example.com";
|
const domain = "example.com";
|
||||||
@@ -22,6 +26,14 @@ describe("OrganizationsDelegationCredentialService", () => {
|
|||||||
add: jest.fn().mockResolvedValue(undefined),
|
add: jest.fn().mockResolvedValue(undefined),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
mockCalendarsTasker = {
|
||||||
|
dispatch: jest.fn().mockResolvedValue({ runId: "test-run-id" }),
|
||||||
|
};
|
||||||
|
|
||||||
|
mockConfigService = {
|
||||||
|
get: jest.fn().mockReturnValue(false),
|
||||||
|
};
|
||||||
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
providers: [
|
providers: [
|
||||||
OrganizationsDelegationCredentialService,
|
OrganizationsDelegationCredentialService,
|
||||||
@@ -35,6 +47,14 @@ describe("OrganizationsDelegationCredentialService", () => {
|
|||||||
provide: getQueueToken(CALENDARS_QUEUE),
|
provide: getQueueToken(CALENDARS_QUEUE),
|
||||||
useValue: mockQueue,
|
useValue: mockQueue,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
provide: CalendarsTasker,
|
||||||
|
useValue: mockCalendarsTasker,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provide: ConfigService,
|
||||||
|
useValue: mockConfigService,
|
||||||
|
},
|
||||||
],
|
],
|
||||||
}).compile();
|
}).compile();
|
||||||
|
|
||||||
@@ -215,5 +235,54 @@ describe("OrganizationsDelegationCredentialService", () => {
|
|||||||
|
|
||||||
await expect(service.ensureDefaultCalendarsForUser(orgId, userId, userEmail)).resolves.toBeUndefined();
|
await expect(service.ensureDefaultCalendarsForUser(orgId, userId, userEmail)).resolves.toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("when enableAsyncTasker is true", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
mockConfigService.get.mockReturnValue(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("uses CalendarsTasker.dispatch instead of Bull queue", async () => {
|
||||||
|
(mockRepository.findEnabledByOrgIdAndDomain as jest.Mock).mockResolvedValue({
|
||||||
|
id: "cred-1",
|
||||||
|
});
|
||||||
|
|
||||||
|
await service.ensureDefaultCalendarsForUser(orgId, userId, userEmail);
|
||||||
|
|
||||||
|
expect(mockConfigService.get).toHaveBeenCalledWith("enableAsyncTasker");
|
||||||
|
expect(mockCalendarsTasker.dispatch).toHaveBeenCalledWith(
|
||||||
|
"ensureDefaultCalendars",
|
||||||
|
{ userId },
|
||||||
|
{ idempotencyKey: `${DEFAULT_CALENDARS_JOB}_${userId}`, idempotencyKeyTTL: "1h" }
|
||||||
|
);
|
||||||
|
expect(mockQueue.add).not.toHaveBeenCalled();
|
||||||
|
expect(mockQueue.getJob).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not call CalendarsTasker when no delegation credential exists", async () => {
|
||||||
|
(mockRepository.findEnabledByOrgIdAndDomain as jest.Mock).mockResolvedValue(null);
|
||||||
|
|
||||||
|
await service.ensureDefaultCalendarsForUser(orgId, userId, userEmail);
|
||||||
|
|
||||||
|
expect(mockCalendarsTasker.dispatch).not.toHaveBeenCalled();
|
||||||
|
expect(mockQueue.add).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not throw when CalendarsTasker.dispatch fails", async () => {
|
||||||
|
(mockRepository.findEnabledByOrgIdAndDomain as jest.Mock).mockResolvedValue({
|
||||||
|
id: "cred-1",
|
||||||
|
});
|
||||||
|
mockCalendarsTasker.dispatch.mockRejectedValue(new Error("Tasker error"));
|
||||||
|
|
||||||
|
await expect(service.ensureDefaultCalendarsForUser(orgId, userId, userEmail)).resolves.toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns early for invalid email without calling tasker", async () => {
|
||||||
|
await service.ensureDefaultCalendarsForUser(orgId, userId, "invalidemail");
|
||||||
|
|
||||||
|
expect(Logger.prototype.warn).toHaveBeenCalledWith(`Invalid email format for user ${userId}: missing domain`);
|
||||||
|
expect(mockCalendarsTasker.dispatch).not.toHaveBeenCalled();
|
||||||
|
expect(mockRepository.findEnabledByOrgIdAndDomain).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
+64
-22
@@ -1,8 +1,21 @@
|
|||||||
|
import { encryptServiceAccountKey } from "@calcom/platform-libraries";
|
||||||
|
import {
|
||||||
|
addDelegationCredential,
|
||||||
|
type TServiceAccountKeySchema,
|
||||||
|
toggleDelegationCredentialEnabled,
|
||||||
|
} from "@calcom/platform-libraries/app-store";
|
||||||
|
import type { DelegationCredential, Prisma, User } from "@calcom/prisma/client";
|
||||||
|
import { InjectQueue } from "@nestjs/bull";
|
||||||
|
import { Injectable, Logger, NotFoundException } from "@nestjs/common";
|
||||||
|
import { ConfigService } from "@nestjs/config";
|
||||||
|
import { Queue } from "bull";
|
||||||
|
import { AppConfig } from "@/config/type";
|
||||||
import {
|
import {
|
||||||
CALENDARS_QUEUE,
|
CALENDARS_QUEUE,
|
||||||
DEFAULT_CALENDARS_JOB,
|
DEFAULT_CALENDARS_JOB,
|
||||||
DefaultCalendarsJobDataType,
|
DefaultCalendarsJobDataType,
|
||||||
} from "@/ee/calendars/processors/calendars.processor";
|
} from "@/ee/calendars/processors/calendars.processor";
|
||||||
|
import { CalendarsTasker } from "@/lib/services/tasker/calendars-tasker.service";
|
||||||
import { CreateDelegationCredentialInput } from "@/modules/organizations/delegation-credentials/inputs/create-delegation-credential.input";
|
import { CreateDelegationCredentialInput } from "@/modules/organizations/delegation-credentials/inputs/create-delegation-credential.input";
|
||||||
import {
|
import {
|
||||||
GoogleServiceAccountKeyInput,
|
GoogleServiceAccountKeyInput,
|
||||||
@@ -10,17 +23,30 @@ import {
|
|||||||
} from "@/modules/organizations/delegation-credentials/inputs/service-account-key.input";
|
} from "@/modules/organizations/delegation-credentials/inputs/service-account-key.input";
|
||||||
import { UpdateDelegationCredentialInput } from "@/modules/organizations/delegation-credentials/inputs/update-delegation-credential.input";
|
import { UpdateDelegationCredentialInput } from "@/modules/organizations/delegation-credentials/inputs/update-delegation-credential.input";
|
||||||
import { OrganizationsDelegationCredentialRepository } from "@/modules/organizations/delegation-credentials/organizations-delegation-credential.repository";
|
import { OrganizationsDelegationCredentialRepository } from "@/modules/organizations/delegation-credentials/organizations-delegation-credential.repository";
|
||||||
import { InjectQueue } from "@nestjs/bull";
|
|
||||||
import { Injectable, NotFoundException, Logger } from "@nestjs/common";
|
|
||||||
import { Queue } from "bull";
|
|
||||||
|
|
||||||
import { encryptServiceAccountKey } from "@calcom/platform-libraries";
|
type DelegationCredentialWithWorkspacePlatform = {
|
||||||
import {
|
workspacePlatform: {
|
||||||
addDelegationCredential,
|
name: string;
|
||||||
toggleDelegationCredentialEnabled,
|
id: number;
|
||||||
type TServiceAccountKeySchema,
|
enabled: boolean;
|
||||||
} from "@calcom/platform-libraries/app-store";
|
createdAt: Date;
|
||||||
import type { User } from "@calcom/prisma/client";
|
updatedAt: Date;
|
||||||
|
slug: string;
|
||||||
|
description: string;
|
||||||
|
defaultServiceAccountKey: Prisma.JsonValue;
|
||||||
|
};
|
||||||
|
} & {
|
||||||
|
id: string;
|
||||||
|
organizationId: number;
|
||||||
|
serviceAccountKey: Prisma.JsonValue;
|
||||||
|
enabled: boolean;
|
||||||
|
lastEnabledAt: Date | null;
|
||||||
|
lastDisabledAt: Date | null;
|
||||||
|
domain: string;
|
||||||
|
createdAt: Date;
|
||||||
|
updatedAt: Date;
|
||||||
|
workspacePlatformId: number;
|
||||||
|
};
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class OrganizationsDelegationCredentialService {
|
export class OrganizationsDelegationCredentialService {
|
||||||
@@ -28,13 +54,15 @@ export class OrganizationsDelegationCredentialService {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly organizationsDelegationCredentialRepository: OrganizationsDelegationCredentialRepository,
|
private readonly organizationsDelegationCredentialRepository: OrganizationsDelegationCredentialRepository,
|
||||||
|
private readonly calendarsTasker: CalendarsTasker,
|
||||||
|
private readonly configService: ConfigService<AppConfig>,
|
||||||
@InjectQueue(CALENDARS_QUEUE) private readonly calendarsQueue: Queue
|
@InjectQueue(CALENDARS_QUEUE) private readonly calendarsQueue: Queue
|
||||||
) {}
|
) {}
|
||||||
async createDelegationCredential(
|
async createDelegationCredential(
|
||||||
orgId: number,
|
orgId: number,
|
||||||
delegatedServiceAccountUser: User,
|
delegatedServiceAccountUser: User,
|
||||||
body: CreateDelegationCredentialInput
|
body: CreateDelegationCredentialInput
|
||||||
) {
|
): Promise<Partial<Omit<DelegationCredential, "serviceAccountKey">> | null | undefined> {
|
||||||
const delegationCredential = await addDelegationCredential({
|
const delegationCredential = await addDelegationCredential({
|
||||||
input: body,
|
input: body,
|
||||||
ctx: { user: { id: delegatedServiceAccountUser.id, organizationId: orgId } },
|
ctx: { user: { id: delegatedServiceAccountUser.id, organizationId: orgId } },
|
||||||
@@ -47,7 +75,7 @@ export class OrganizationsDelegationCredentialService {
|
|||||||
delegationCredentialId: string,
|
delegationCredentialId: string,
|
||||||
delegatedServiceAccountUser: User,
|
delegatedServiceAccountUser: User,
|
||||||
body: UpdateDelegationCredentialInput
|
body: UpdateDelegationCredentialInput
|
||||||
) {
|
): Promise<DelegationCredentialWithWorkspacePlatform> {
|
||||||
let delegationCredential =
|
let delegationCredential =
|
||||||
await this.organizationsDelegationCredentialRepository.findByIdWithWorkspacePlatform(
|
await this.organizationsDelegationCredentialRepository.findByIdWithWorkspacePlatform(
|
||||||
delegationCredentialId
|
delegationCredentialId
|
||||||
@@ -82,7 +110,7 @@ export class OrganizationsDelegationCredentialService {
|
|||||||
return { ...delegationCredential, enabled: body?.enabled ?? delegationCredential.enabled };
|
return { ...delegationCredential, enabled: body?.enabled ?? delegationCredential.enabled };
|
||||||
}
|
}
|
||||||
|
|
||||||
async ensureDefaultCalendars(orgId: number, domain: string) {
|
async ensureDefaultCalendars(orgId: number, domain: string): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const delegatedUserProfiles =
|
const delegatedUserProfiles =
|
||||||
await this.organizationsDelegationCredentialRepository.findDelegatedUserProfiles(orgId, domain);
|
await this.organizationsDelegationCredentialRepository.findDelegatedUserProfiles(orgId, domain);
|
||||||
@@ -123,7 +151,7 @@ export class OrganizationsDelegationCredentialService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async ensureDefaultCalendarsForUser(orgId: number, userId: number, userEmail: string) {
|
async ensureDefaultCalendarsForUser(orgId: number, userId: number, userEmail: string): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const emailParts = userEmail.split("@");
|
const emailParts = userEmail.split("@");
|
||||||
if (emailParts.length < 2 || !emailParts[1]) {
|
if (emailParts.length < 2 || !emailParts[1]) {
|
||||||
@@ -133,23 +161,37 @@ export class OrganizationsDelegationCredentialService {
|
|||||||
const emailDomain = `@${emailParts[1]}`;
|
const emailDomain = `@${emailParts[1]}`;
|
||||||
|
|
||||||
const delegationCredential =
|
const delegationCredential =
|
||||||
await this.organizationsDelegationCredentialRepository.findEnabledByOrgIdAndDomain(orgId, emailDomain);
|
await this.organizationsDelegationCredentialRepository.findEnabledByOrgIdAndDomain(
|
||||||
|
orgId,
|
||||||
|
emailDomain
|
||||||
|
);
|
||||||
|
|
||||||
if (!delegationCredential) {
|
if (!delegationCredential) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.configService.get("enableAsyncTasker")) {
|
||||||
|
this.logger.log(`Adding default calendar job for user with id: ${userId}`);
|
||||||
|
await this.calendarsTasker.dispatch(
|
||||||
|
"ensureDefaultCalendars",
|
||||||
|
{
|
||||||
|
userId,
|
||||||
|
},
|
||||||
|
{ idempotencyKey: `${DEFAULT_CALENDARS_JOB}_${userId}`, idempotencyKeyTTL: "1h" }
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const existingJob = await this.calendarsQueue.getJob(`${DEFAULT_CALENDARS_JOB}_${userId}`);
|
const existingJob = await this.calendarsQueue.getJob(`${DEFAULT_CALENDARS_JOB}_${userId}`);
|
||||||
if (existingJob) {
|
if (existingJob) {
|
||||||
await existingJob.remove();
|
await existingJob.remove();
|
||||||
this.logger.log(`Removed existing default calendar job for user with id: ${userId}`);
|
this.logger.log(`Removed existing default calendar job for user with id: ${userId}`);
|
||||||
}
|
}
|
||||||
this.logger.log(`Adding default calendar job for user with id: ${userId}`);
|
this.logger.log(`Adding default calendar job for user with id: ${userId}`);
|
||||||
await this.calendarsQueue.add(
|
await this.calendarsQueue.add(DEFAULT_CALENDARS_JOB, { userId } satisfies DefaultCalendarsJobDataType, {
|
||||||
DEFAULT_CALENDARS_JOB,
|
jobId: `${DEFAULT_CALENDARS_JOB}_${userId}`,
|
||||||
{ userId } satisfies DefaultCalendarsJobDataType,
|
removeOnComplete: true,
|
||||||
{ jobId: `${DEFAULT_CALENDARS_JOB}_${userId}`, removeOnComplete: true }
|
});
|
||||||
);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.error(
|
this.logger.error(
|
||||||
err,
|
err,
|
||||||
@@ -163,7 +205,7 @@ export class OrganizationsDelegationCredentialService {
|
|||||||
delegationCredentialId: string,
|
delegationCredentialId: string,
|
||||||
delegatedServiceAccountUser: User,
|
delegatedServiceAccountUser: User,
|
||||||
enabled: boolean
|
enabled: boolean
|
||||||
) {
|
): Promise<Partial<Omit<DelegationCredential, "serviceAccountKey">> | null | undefined> {
|
||||||
const handlerUser = {
|
const handlerUser = {
|
||||||
id: delegatedServiceAccountUser.id,
|
id: delegatedServiceAccountUser.id,
|
||||||
email: delegatedServiceAccountUser.email,
|
email: delegatedServiceAccountUser.email,
|
||||||
@@ -177,7 +219,7 @@ export class OrganizationsDelegationCredentialService {
|
|||||||
async updateDelegationCredentialServiceAccountKey(
|
async updateDelegationCredentialServiceAccountKey(
|
||||||
delegationCredentialId: string,
|
delegationCredentialId: string,
|
||||||
serviceAccountKey: GoogleServiceAccountKeyInput | MicrosoftServiceAccountKeyInput
|
serviceAccountKey: GoogleServiceAccountKeyInput | MicrosoftServiceAccountKeyInput
|
||||||
) {
|
): Promise<DelegationCredentialWithWorkspacePlatform> {
|
||||||
// First encrypt the service account key
|
// First encrypt the service account key
|
||||||
const encryptedServiceAccountKey = encryptServiceAccountKey(
|
const encryptedServiceAccountKey = encryptServiceAccountKey(
|
||||||
serviceAccountKey as TServiceAccountKeySchema
|
serviceAccountKey as TServiceAccountKeySchema
|
||||||
|
|||||||
+36
-2
@@ -20,6 +20,7 @@ import { GetAllOrgMemberships } from "@/modules/organizations/memberships/output
|
|||||||
import { GetOrgMembership } from "@/modules/organizations/memberships/outputs/get-membership.output";
|
import { GetOrgMembership } from "@/modules/organizations/memberships/outputs/get-membership.output";
|
||||||
import { OrgUserAttribute } from "@/modules/organizations/memberships/outputs/organization-membership.output";
|
import { OrgUserAttribute } from "@/modules/organizations/memberships/outputs/organization-membership.output";
|
||||||
import { UpdateOrgMembership } from "@/modules/organizations/memberships/outputs/update-membership.output";
|
import { UpdateOrgMembership } from "@/modules/organizations/memberships/outputs/update-membership.output";
|
||||||
|
import { OrganizationsDelegationCredentialService } from "@/modules/organizations/delegation-credentials/services/organizations-delegation-credential.service";
|
||||||
import { PrismaModule } from "@/modules/prisma/prisma.module";
|
import { PrismaModule } from "@/modules/prisma/prisma.module";
|
||||||
import { TeamMembershipOutput } from "@/modules/teams/memberships/outputs/team-membership.output";
|
import { TeamMembershipOutput } from "@/modules/teams/memberships/outputs/team-membership.output";
|
||||||
import { TokensModule } from "@/modules/tokens/tokens.module";
|
import { TokensModule } from "@/modules/tokens/tokens.module";
|
||||||
@@ -146,7 +147,7 @@ describe("Organizations Memberships Endpoints", () => {
|
|||||||
await app.init();
|
await app.init();
|
||||||
});
|
});
|
||||||
|
|
||||||
async function setupAttributes() {
|
async function setupAttributes(): Promise<void> {
|
||||||
textAttribute = await attributesRepositoryFixture.create({
|
textAttribute = await attributesRepositoryFixture.create({
|
||||||
team: { connect: { id: org.id } },
|
team: { connect: { id: org.id } },
|
||||||
type: "TEXT",
|
type: "TEXT",
|
||||||
@@ -261,7 +262,7 @@ describe("Organizations Memberships Endpoints", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function userHasCorrectAttributes(attributes: OrgUserAttribute[]) {
|
function userHasCorrectAttributes(attributes: OrgUserAttribute[]): void {
|
||||||
expect(attributes.length).toEqual(4);
|
expect(attributes.length).toEqual(4);
|
||||||
const responseNumberAttribute = attributes.find((attr) => attr.type === "number");
|
const responseNumberAttribute = attributes.find((attr) => attr.type === "number");
|
||||||
const responseSingleSelectAttribute = attributes.find((attr) => attr.type === "singleSelect");
|
const responseSingleSelectAttribute = attributes.find((attr) => attr.type === "singleSelect");
|
||||||
@@ -387,6 +388,39 @@ describe("Organizations Memberships Endpoints", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should call ensureDefaultCalendarsForUser when creating membership", async () => {
|
||||||
|
const newUserEmail = `organizations-memberships-calendars-${randomString()}@api.com`;
|
||||||
|
const newUser = await userRepositoryFixture.create({
|
||||||
|
email: newUserEmail,
|
||||||
|
username: newUserEmail,
|
||||||
|
bio,
|
||||||
|
metadata,
|
||||||
|
});
|
||||||
|
|
||||||
|
const ensureDefaultCalendarsSpy = jest
|
||||||
|
.spyOn(OrganizationsDelegationCredentialService.prototype, "ensureDefaultCalendarsForUser")
|
||||||
|
.mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await request(app.getHttpServer())
|
||||||
|
.post(`/v2/organizations/${org.id}/memberships`)
|
||||||
|
.send({
|
||||||
|
userId: newUser.id,
|
||||||
|
accepted: true,
|
||||||
|
role: "MEMBER",
|
||||||
|
} satisfies CreateOrgMembershipDto)
|
||||||
|
.expect(201);
|
||||||
|
|
||||||
|
expect(ensureDefaultCalendarsSpy).toHaveBeenCalledWith(org.id, newUser.id, newUserEmail);
|
||||||
|
} finally {
|
||||||
|
ensureDefaultCalendarsSpy.mockRestore();
|
||||||
|
await membershipRepositoryFixture.delete(
|
||||||
|
(await membershipRepositoryFixture.getUserMembershipByTeamId(newUser.id, org.id))?.id ?? 0
|
||||||
|
);
|
||||||
|
await userRepositoryFixture.deleteByEmail(newUserEmail);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
it("should update the membership of the org", async () => {
|
it("should update the membership of the org", async () => {
|
||||||
return request(app.getHttpServer())
|
return request(app.getHttpServer())
|
||||||
.patch(`/v2/organizations/${org.id}/memberships/${membershipCreatedViaApi.id}`)
|
.patch(`/v2/organizations/${org.id}/memberships/${membershipCreatedViaApi.id}`)
|
||||||
|
|||||||
+2
@@ -1,6 +1,7 @@
|
|||||||
import { BullModule } from "@nestjs/bull";
|
import { BullModule } from "@nestjs/bull";
|
||||||
import { Module } from "@nestjs/common";
|
import { Module } from "@nestjs/common";
|
||||||
import { CALENDARS_QUEUE } from "@/ee/calendars/processors/calendars.processor";
|
import { CALENDARS_QUEUE } from "@/ee/calendars/processors/calendars.processor";
|
||||||
|
import { CalendarsTaskerModule } from "@/lib/modules/calendars-tasker.module";
|
||||||
import { ApiKeysModule } from "@/modules/api-keys/api-keys.module";
|
import { ApiKeysModule } from "@/modules/api-keys/api-keys.module";
|
||||||
import { ManagedOrganizationsBillingService } from "@/modules/billing/services/managed-organizations.billing.service";
|
import { ManagedOrganizationsBillingService } from "@/modules/billing/services/managed-organizations.billing.service";
|
||||||
import { MembershipsModule } from "@/modules/memberships/memberships.module";
|
import { MembershipsModule } from "@/modules/memberships/memberships.module";
|
||||||
@@ -35,6 +36,7 @@ import { UsersRepository } from "@/modules/users/users.repository";
|
|||||||
duration: 1000,
|
duration: 1000,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
CalendarsTaskerModule,
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
ManagedOrganizationsService,
|
ManagedOrganizationsService,
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { CalendarsRepository } from "@/ee/calendars/calendars.repository";
|
|||||||
import { CALENDARS_QUEUE } from "@/ee/calendars/processors/calendars.processor";
|
import { CALENDARS_QUEUE } from "@/ee/calendars/processors/calendars.processor";
|
||||||
import { CalendarsService } from "@/ee/calendars/services/calendars.service";
|
import { CalendarsService } from "@/ee/calendars/services/calendars.service";
|
||||||
import { CalendarsCacheService } from "@/ee/calendars/services/calendars-cache.service";
|
import { CalendarsCacheService } from "@/ee/calendars/services/calendars-cache.service";
|
||||||
|
import { CalendarsTaskerModule } from "@/lib/modules/calendars-tasker.module";
|
||||||
import { AppsRepository } from "@/modules/apps/apps.repository";
|
import { AppsRepository } from "@/modules/apps/apps.repository";
|
||||||
import { CredentialsRepository } from "@/modules/credentials/credentials.repository";
|
import { CredentialsRepository } from "@/modules/credentials/credentials.repository";
|
||||||
import { OrganizationsDelegationCredentialRepository } from "@/modules/organizations/delegation-credentials/organizations-delegation-credential.repository";
|
import { OrganizationsDelegationCredentialRepository } from "@/modules/organizations/delegation-credentials/organizations-delegation-credential.repository";
|
||||||
@@ -29,6 +30,7 @@ import { UsersRepository } from "@/modules/users/users.repository";
|
|||||||
duration: 1000,
|
duration: 1000,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
CalendarsTaskerModule,
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
SelectedCalendarsRepository,
|
SelectedCalendarsRepository,
|
||||||
|
|||||||
@@ -32,7 +32,8 @@
|
|||||||
"@calcom/platform-libraries/bookings": ["../../../packages/platform/libraries/bookings.ts"],
|
"@calcom/platform-libraries/bookings": ["../../../packages/platform/libraries/bookings.ts"],
|
||||||
"@calcom/platform-libraries/private-links": ["../../../packages/platform/libraries/private-links.ts"],
|
"@calcom/platform-libraries/private-links": ["../../../packages/platform/libraries/private-links.ts"],
|
||||||
"@calcom/platform-libraries/organizations": ["../../../packages/platform/libraries/organizations.ts"],
|
"@calcom/platform-libraries/organizations": ["../../../packages/platform/libraries/organizations.ts"],
|
||||||
"@calcom/platform-libraries/errors": ["../../../packages/platform/libraries/errors.ts"]
|
"@calcom/platform-libraries/errors": ["../../../packages/platform/libraries/errors.ts"],
|
||||||
|
"@calcom/platform-libraries/calendars": ["../../../packages/platform/libraries/calendars.ts"]
|
||||||
},
|
},
|
||||||
"incremental": true,
|
"incremental": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
|
|||||||
+1
-1
@@ -212,7 +212,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"includes": ["**/*.test.ts", "**/*.test.tsx", "**/*.e2e-spec.ts", "**/*.integration-test.ts"],
|
"includes": ["**/*.test.ts", "**/*.test.tsx", "**/*.spec.ts", "**/*.e2e-spec.ts", "**/*.integration-test.ts"],
|
||||||
"linter": {
|
"linter": {
|
||||||
"rules": {
|
"rules": {
|
||||||
"complexity": {
|
"complexity": {
|
||||||
|
|||||||
+267
-258
File diff suppressed because it is too large.
Load diff
@@ -104,6 +104,11 @@
|
|||||||
"import": "./dist/errors.js",
|
"import": "./dist/errors.js",
|
||||||
"require": "./dist/errors.cjs",
|
"require": "./dist/errors.cjs",
|
||||||
"types": "./dist/errors.d.ts"
|
"types": "./dist/errors.d.ts"
|
||||||
|
},
|
||||||
|
"./calendars": {
|
||||||
|
"import": "./dist/calendars.js",
|
||||||
|
"require": "./dist/calendars.cjs",
|
||||||
|
"types": "./dist/calendars.d.ts"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"typesVersions": {
|
"typesVersions": {
|
||||||
@@ -146,6 +151,9 @@
|
|||||||
],
|
],
|
||||||
"errors": [
|
"errors": [
|
||||||
"dist/errors.d.ts"
|
"dist/errors.d.ts"
|
||||||
|
],
|
||||||
|
"calendars": [
|
||||||
|
"dist/calendars.d.ts"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ export default defineConfig({
|
|||||||
"private-links": resolve(__dirname, "./private-links.ts"),
|
"private-links": resolve(__dirname, "./private-links.ts"),
|
||||||
pbac: resolve(__dirname, "./pbac.ts"),
|
pbac: resolve(__dirname, "./pbac.ts"),
|
||||||
errors: resolve(__dirname, "./errors.ts"),
|
errors: resolve(__dirname, "./errors.ts"),
|
||||||
|
calendars: resolve(__dirname, "./calendars.ts"),
|
||||||
},
|
},
|
||||||
name: "calcom-lib",
|
name: "calcom-lib",
|
||||||
fileName: "calcom-lib",
|
fileName: "calcom-lib",
|
||||||
|
|||||||
Reference in new issue
Block a user