fix: APIV2 team membership - Member not getting added to event-type automatically (#24780)

* fix: APIV2 team membership addition

* feat: Add trimming for email domain and orgAutoAcceptEmail in auto-accept logic

- Trim whitespace from both user email domain and orgAutoAcceptEmail
- Ensures consistent matching even with accidental whitespace
- Addresses feedback from PR review

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>

* simplify

* feat: Use shared OrganizationMembershipService in TRPC for consistent auto-accept logic

- Create OrganizationMembershipService.container.ts for DI in TRPC
- Update getOrgConnectionInfo to apply trimming + case-insensitive comparison
- Precompute auto-accept decisions in createNewUsersConnectToOrgIfExists using the service
- Use service in handleNewUsersInvites for consistent auto-accept determination
- Ensures both API v2 and TRPC paths use identical trimming and normalization logic

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>

* Revert "feat: Use shared OrganizationMembershipService in TRPC for consistent auto-accept logic"

This reverts commit 0b2bd28b6e32e8c1d3dea139ca8ff9cbf402ac00.

* refactor: Unify OrganizationRepository and remove duplicate PrismaOrganizationRepository (#24869)

* refactor: Convert OrganizationRepository from static to instance methods

- Add constructor accepting deps object with prismaClient
- Convert all static methods to instance methods
- Add getOrganizationAutoAcceptSettings method
- Create singleton instance export in repository barrel file
- Update API v2 OrganizationsRepository to extend from OrganizationRepository
- Update all call sites to use singleton instance
- Add platform-libraries organizations.ts export
- Fix mock imports to use repository barrel
- Fix unsafe optional chaining in next-auth-options.ts
- Fix any types in test files with proper type inference

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>

* fix: Update all imports to use OrganizationRepository barrel export

- Update imports from direct OrganizationRepository file to barrel export
- This ensures mocks work correctly in tests
- Fixes 202 failing tests related to organizationRepository mock

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>

* fix: Update test mocks to use partial mock pattern

- Convert organizationMock to partial mock that preserves real class
- Add proper prisma mocks to failing test files
- Remove old OrganizationRepository mocks from test files
- This fixes test failures related to mock interception

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>

* fix: Export mocked singleton and update tests to use it directly

- Export mockedSingleton as organizationRepositoryMock from organizationMock
- Update delegationCredential.test.ts to import and use the exported mock
- This fixes 'vi.mocked(...).mockResolvedValue is not a function' errors

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>

* fix: Use platform-libraries import for API v2 OrganizationRepository

API v2 should import shared features through @calcom/platform-libraries
instead of directly from @calcom/features to maintain proper architectural
boundaries and packaging/licensing separation.

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>

* refactor: Implement DI pattern for OrganizationRepository

- Create OrganizationRepository.module.ts and .container.ts for DI
- Replace singleton pattern with getOrganizationRepository() across 20 files
- Update platform-libraries to export getOrganizationRepository
- Delete duplicate PrismaOrganizationRepository.ts
- Remove singleton export file (repositories/index.ts)
- Update test mocks to use DI container pattern
- All type checks and unit tests passing

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>

* fix: Implement read/write client separation in OrganizationRepository

- Updated OrganizationRepository constructor to accept optional prismaWriteClient parameter
- Routed all write operations (create, update) through prismaWrite client
- Routed all read operations (find, get) through prismaRead client
- Updated API v2 OrganizationsRepository to pass both dbRead.prisma and dbWrite.prisma to super()
- Optimized getOrganizationRepository() calls by storing in local variables to avoid repeated function calls
- This fixes the critical issue where API v2 was passing read-only client to base class with write methods

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>

* fix: Fix mock setup and optimize getOrganizationRepository() calls

- Fixed verify-email.test.ts mock to return mocked repository instance instead of scenario helper object
- Added mockReset to organizationMock.ts beforeEach to properly reset mock implementations between tests
- Added local variables in page.tsx to store getOrganizationRepository() result for consistency

This fixes the issue where getOrganizationRepository() was returning organizationScenarios.organizationRepository (scenario helper) instead of the actual mocked repository instance, causing findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail to be undefined.

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>

* refactor: Simplify OrganizationRepository to use single prismaClient

- Updated base OrganizationRepository constructor to accept only { prismaClient } instead of { prismaClient, prismaWriteClient? }
- Replaced this.prismaRead and this.prismaWrite with single this.prismaClient property
- Updated API v2 OrganizationsRepository to pass only dbWrite.prisma as prismaClient
- Removed unused PrismaReadService import from API v2
- All read and write operations now use the same client instance

This simplifies the architecture as requested - API v2 uses write client for all operations, and apps/web uses the client from DI container.

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix: Match OrganizationsRepository.findById signature with base class

The findById method in OrganizationsRepository was using a different signature
than the base OrganizationRepository class, causing type errors in CI.

Changed from: findById(organizationId: number)
Changed to: findById({ id }: { id: number })

This matches the base class signature and resolves the CI unit test failures.

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>

* fix: Update all findById call sites to use object parameter

Fixed 6 call sites in API v2 that were calling findById with a number
instead of the required { id: number } object parameter:

- is-org.guard.ts
- is-admin-api-enabled.guard.ts
- is-webhook-in-org.guard.ts
- organizations.service.ts
- managed-organizations.service.ts (2 call sites)

This resolves the API v2 build failure caused by the signature change
in OrganizationsRepository.findById to match the base class.

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>

* fix: Remove redundant findById override from OrganizationsRepository

The findById method was duplicating the base class OrganizationRepository
implementation. Both methods had identical logic (filtering by isOrganization: true),
so the override was unnecessary.

Since OrganizationsRepository extends OrganizationRepository and passes
dbWrite.prisma to the base constructor, the base class method already
provides the exact same functionality.

This resolves the API v2 build failure by eliminating the duplicate method
that was causing conflicts.

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>

* Remove unncessary changes

* Store in variable

* Revert "Remove unncessary changes"

This reverts commit af9351786a21616c9508c441191c17f2374fb2cc.

* Revert dbRead/dbWrite changes

* Add organizations library to tsconfig.json

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com>
This commit is contained in:
Hariom Balhara
2025-11-07 10:50:00 +00:00
committed by GitHub
co-authored by hariom@cal.com <hariombalhara@gmail.com> Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Morgan
parent 02b1393ff4
commit bc665cbeab
50 changed files with 583 additions and 159 deletions
@@ -0,0 +1,12 @@
import { OrganizationsRepository } from "@/modules/organizations/index/organizations.repository";
import { Injectable } from "@nestjs/common";
import { OrganizationMembershipService as BaseOrganizationMembershipService } from "@calcom/platform-libraries/organizations";
@Injectable()
export class OrganizationMembershipService extends BaseOrganizationMembershipService {
constructor(organizationsRepository: OrganizationsRepository) {
super({ organizationRepository: organizationsRepository });
}
}
@@ -54,7 +54,7 @@ export class IsAdminAPIEnabledGuard implements CanActivate {
}
}
const org = await this.organizationsRepository.findById(Number(organizationId));
const org = await this.organizationsRepository.findById({ id: Number(organizationId) });
if (org?.isOrganization && !org?.isPlatform) {
const adminAPIAccessIsEnabledInOrg = await this.organizationsRepository.fetchOrgAdminApiStatus(
@@ -55,7 +55,7 @@ export class IsOrgGuard implements CanActivate {
}
}
const org = await this.organizationsRepository.findById(Number(organizationId));
const org = await this.organizationsRepository.findById({ id: Number(organizationId) });
if (org?.isOrganization) {
canAccess = true;
@@ -43,7 +43,7 @@ export class IsWebhookInOrg implements CanActivate {
}
}
const org = await this.organizationsRepository.findById(Number(organizationId));
const org = await this.organizationsRepository.findById({ id: Number(organizationId) });
if (org?.isOrganization) {
const isWebhookInOrg = await this.organizationsWebhooksRepository.findWebhook(
@@ -4,23 +4,17 @@ import { PrismaWriteService } from "@/modules/prisma/prisma-write.service";
import { StripeService } from "@/modules/stripe/stripe.service";
import { Injectable } from "@nestjs/common";
import { OrganizationRepository } from "@calcom/platform-libraries/organizations";
import { Prisma } from "@calcom/prisma/client";
@Injectable()
export class OrganizationsRepository {
export class OrganizationsRepository extends OrganizationRepository {
constructor(
private readonly dbRead: PrismaReadService,
private readonly dbWrite: PrismaWriteService,
private readonly stripeService: StripeService
) {}
async findById(organizationId: number) {
return this.dbRead.prisma.team.findUnique({
where: {
id: organizationId,
isOrganization: true,
},
});
) {
super({ prismaClient: dbWrite.prisma });
}
async findByIds(organizationIds: number[]) {
@@ -6,7 +6,7 @@ export class OrganizationsService {
constructor(private readonly organizationsRepository: OrganizationsRepository) {}
async isPlatform(organizationId: number) {
const organization = await this.organizationsRepository.findById(organizationId);
const organization = await this.organizationsRepository.findById({ id: organizationId });
return organization?.isPlatform;
}
}
@@ -13,6 +13,7 @@ import { ZoomVideoService } from "@/modules/conferencing/services/zoom-video.ser
import { CredentialsRepository } from "@/modules/credentials/credentials.repository";
import { EmailModule } from "@/modules/email/email.module";
import { EmailService } from "@/modules/email/email.service";
import { OrganizationMembershipService } from "@/lib/services/organization-membership.service";
import { MembershipsModule } from "@/modules/memberships/memberships.module";
import { OAuthClientRepository } from "@/modules/oauth-clients/oauth-client.repository";
import { UserOOORepository } from "@/modules/ooo/repositories/ooo.repository";
@@ -108,6 +109,7 @@ import { Module } from "@nestjs/common";
],
providers: [
OrganizationsRepository,
OrganizationMembershipService,
OrganizationsTeamsRepository,
OrganizationsService,
OrganizationsTeamsService,
@@ -200,4 +202,4 @@ import { Module } from "@nestjs/common";
OrganizationsEventTypesPrivateLinksController,
],
})
export class OrganizationsModule {}
export class OrganizationsModule { }
@@ -103,12 +103,12 @@ export class ManagedOrganizationsService {
}
private async isManagerOrganizationPlatform(managerOrganizationId: number) {
const organization = await this.organizationsRepository.findById(managerOrganizationId);
const organization = await this.organizationsRepository.findById({ id: managerOrganizationId });
return !!organization?.isPlatform;
}
async getManagedOrganization(managedOrganizationId: number) {
const organization = await this.organizationsRepository.findById(managedOrganizationId);
const organization = await this.organizationsRepository.findById({ id: managedOrganizationId });
if (!organization) {
throw new NotFoundException(`Managed organization with id=${managedOrganizationId} does not exist.`);
}
@@ -418,6 +418,209 @@ describe("Organizations Teams Memberships Endpoints", () => {
.expect(404);
});
// Auto-accept tests
describe("auto-accept based on email domain", () => {
let orgWithAutoAccept: Team;
let subteamWithAutoAccept: Team;
let userWithMatchingEmail: User;
let userWithUppercaseEmail: User;
let userWithMatchingEmailForOverride: User;
let userWithNonMatchingEmail: User;
beforeAll(async () => {
// Create org with auto-accept settings
orgWithAutoAccept = await organizationsRepositoryFixture.create({
name: `auto-accept-org-${randomString()}`,
isOrganization: true,
});
// Update organizationSettings with orgAutoAcceptEmail
await organizationsRepositoryFixture.updateSettings(orgWithAutoAccept.id, {
orgAutoAcceptEmail: "acme.com",
isOrganizationVerified: true,
isOrganizationConfigured: true,
});
// Create subteam
subteamWithAutoAccept = await teamsRepositoryFixture.create({
name: `auto-accept-subteam-${randomString()}`,
isOrganization: false,
parent: { connect: { id: orgWithAutoAccept.id } },
});
// Create event type with assignAllTeamMembers
await eventTypesRepositoryFixture.createTeamEventType({
schedulingType: "COLLECTIVE",
team: { connect: { id: subteamWithAutoAccept.id } },
title: "Auto Accept Event Type",
slug: "auto-accept-event-type",
length: 30,
assignAllTeamMembers: true,
bookingFields: [],
locations: [],
});
// Create users
userWithMatchingEmail = await userRepositoryFixture.create({
email: `alice@acme.com`,
username: `alice-${randomString()}`,
});
userWithUppercaseEmail = await userRepositoryFixture.create({
email: `bob@ACME.COM`,
username: `bob-${randomString()}`,
});
userWithMatchingEmailForOverride = await userRepositoryFixture.create({
email: `david@acme.com`,
username: `david-${randomString()}`,
});
userWithNonMatchingEmail = await userRepositoryFixture.create({
email: `charlie@external.com`,
username: `charlie-${randomString()}`,
});
// Add users to org
await membershipsRepositoryFixture.create({
role: "MEMBER",
accepted: true,
user: { connect: { id: userWithMatchingEmail.id } },
team: { connect: { id: orgWithAutoAccept.id } },
});
await membershipsRepositoryFixture.create({
role: "MEMBER",
accepted: true,
user: { connect: { id: userWithUppercaseEmail.id } },
team: { connect: { id: orgWithAutoAccept.id } },
});
await membershipsRepositoryFixture.create({
role: "MEMBER",
accepted: true,
user: { connect: { id: userWithMatchingEmailForOverride.id } },
team: { connect: { id: orgWithAutoAccept.id } },
});
await membershipsRepositoryFixture.create({
role: "MEMBER",
accepted: true,
user: { connect: { id: userWithNonMatchingEmail.id } },
team: { connect: { id: orgWithAutoAccept.id } },
});
// Create profiles for users
await profileRepositoryFixture.create({
uid: `usr-${userWithMatchingEmail.id}`,
username: userWithMatchingEmail.username || `user-${userWithMatchingEmail.id}`,
organization: { connect: { id: orgWithAutoAccept.id } },
user: { connect: { id: userWithMatchingEmail.id } },
});
await profileRepositoryFixture.create({
uid: `usr-${userWithUppercaseEmail.id}`,
username: userWithUppercaseEmail.username || `user-${userWithUppercaseEmail.id}`,
organization: { connect: { id: orgWithAutoAccept.id } },
user: { connect: { id: userWithUppercaseEmail.id } },
});
await profileRepositoryFixture.create({
uid: `usr-${userWithMatchingEmailForOverride.id}`,
username: userWithMatchingEmailForOverride.username || `user-${userWithMatchingEmailForOverride.id}`,
organization: { connect: { id: orgWithAutoAccept.id } },
user: { connect: { id: userWithMatchingEmailForOverride.id } },
});
await profileRepositoryFixture.create({
uid: `usr-${userWithNonMatchingEmail.id}`,
username: userWithNonMatchingEmail.username || `user-${userWithNonMatchingEmail.id}`,
organization: { connect: { id: orgWithAutoAccept.id } },
user: { connect: { id: userWithNonMatchingEmail.id } },
});
// Make user an admin of the org for API access
await membershipsRepositoryFixture.create({
role: "ADMIN",
accepted: true,
user: { connect: { id: user.id } },
team: { connect: { id: orgWithAutoAccept.id } },
});
});
it("should auto-accept when email matches orgAutoAcceptEmail", async () => {
const response = await request(app.getHttpServer())
.post(`/v2/organizations/${orgWithAutoAccept.id}/teams/${subteamWithAutoAccept.id}/memberships`)
.send({
userId: userWithMatchingEmail.id,
role: "MEMBER",
} satisfies CreateOrgTeamMembershipDto)
.expect(201);
const responseBody: ApiSuccessResponse<TeamMembershipOutput> = response.body;
expect(responseBody.data.accepted).toBe(true);
// Verify EventTypes assignment
const eventTypes = await eventTypesRepositoryFixture.getAllTeamEventTypes(
subteamWithAutoAccept.id
);
const eventTypeWithAssignAll = eventTypes.find((et) => et.assignAllTeamMembers);
expect(eventTypeWithAssignAll).toBeTruthy();
const userIsHost = eventTypeWithAssignAll?.hosts.some((h) => h.userId === userWithMatchingEmail.id);
expect(userIsHost).toBe(true);
});
it("should handle case-insensitive email domain matching", async () => {
// User with email="bob@ACME.COM" should match orgAutoAcceptEmail="acme.com"
const response = await request(app.getHttpServer())
.post(`/v2/organizations/${orgWithAutoAccept.id}/teams/${subteamWithAutoAccept.id}/memberships`)
.send({
userId: userWithUppercaseEmail.id,
role: "MEMBER",
} satisfies CreateOrgTeamMembershipDto)
.expect(201);
const responseBody: ApiSuccessResponse<TeamMembershipOutput> = response.body;
expect(responseBody.data.accepted).toBe(true);
});
it("should ALWAYS auto-accept when email matches, even if accepted:false", async () => {
const response = await request(app.getHttpServer())
.post(`/v2/organizations/${orgWithAutoAccept.id}/teams/${subteamWithAutoAccept.id}/memberships`)
.send({
userId: userWithMatchingEmailForOverride.id,
role: "MEMBER",
accepted: false,
} satisfies CreateOrgTeamMembershipDto)
.expect(201);
const responseBody: ApiSuccessResponse<TeamMembershipOutput> = response.body;
// Should override to true because email matches
expect(responseBody.data.accepted).toBe(true);
});
it("should NOT auto-accept when email does not match orgAutoAcceptEmail", async () => {
const response = await request(app.getHttpServer())
.post(`/v2/organizations/${orgWithAutoAccept.id}/teams/${subteamWithAutoAccept.id}/memberships`)
.send({
userId: userWithNonMatchingEmail.id,
role: "MEMBER",
} satisfies CreateOrgTeamMembershipDto)
.expect(201);
const responseBody: ApiSuccessResponse<TeamMembershipOutput> = response.body;
expect(responseBody.data.accepted).toBe(false);
});
afterAll(async () => {
await userRepositoryFixture.deleteByEmail(userWithMatchingEmail.email);
await userRepositoryFixture.deleteByEmail(userWithUppercaseEmail.email);
await userRepositoryFixture.deleteByEmail(userWithMatchingEmailForOverride.email);
await userRepositoryFixture.deleteByEmail(userWithNonMatchingEmail.email);
await organizationsRepositoryFixture.delete(orgWithAutoAccept.id);
});
});
afterAll(async () => {
await userRepositoryFixture.deleteByEmail(user.email);
await userRepositoryFixture.deleteByEmail(userToInviteViaApi.email);
@@ -12,6 +12,7 @@ import { IsAdminAPIEnabledGuard } from "@/modules/auth/guards/organizations/is-a
import { IsOrgGuard } from "@/modules/auth/guards/organizations/is-org.guard";
import { RolesGuard } from "@/modules/auth/guards/roles/roles.guard";
import { IsTeamInOrg } from "@/modules/auth/guards/teams/is-team-in-org.guard";
import { OrganizationMembershipService } from "@/lib/services/organization-membership.service";
import { OrganizationsRepository } from "@/modules/organizations/index/organizations.repository";
import { CreateOrgTeamMembershipDto } from "@/modules/organizations/teams/memberships/inputs/create-organization-team-membership.input";
import { UpdateOrgTeamMembershipDto } from "@/modules/organizations/teams/memberships/inputs/update-organization-team-membership.input";
@@ -58,8 +59,9 @@ export class OrganizationsTeamsMembershipsController {
constructor(
private organizationsTeamsMembershipsService: OrganizationsTeamsMembershipsService,
private readonly organizationsRepository: OrganizationsRepository
) {}
private readonly organizationsRepository: OrganizationsRepository,
private readonly orgMembershipService: OrganizationMembershipService
) { }
@Get("/")
@ApiOperation({ summary: "Get all memberships" })
@@ -168,6 +170,9 @@ export class OrganizationsTeamsMembershipsController {
};
}
// TODO: Refactor to use inviteMembersWithNoInviterPermissionCheck when it is moved to a Service
// See: packages/trpc/server/routers/viewer/teams/inviteMember/inviteMember.handler.ts
@Roles("TEAM_ADMIN")
@PlatformPlan("ESSENTIALS")
@Post("/")
@@ -184,7 +189,21 @@ export class OrganizationsTeamsMembershipsController {
throw new UnprocessableEntityException("User is not part of the Organization");
}
const membership = await this.organizationsTeamsMembershipsService.createOrgTeamMembership(teamId, data);
const shouldAutoAccept = await this.orgMembershipService.shouldAutoAccept({
organizationId: orgId,
userEmail: user.email,
});
// ALWAYS override when email matches - prevents pending memberships
// Remember organizations expect added team member to automatically start receiving bookings for the team event
const acceptedStatus = shouldAutoAccept ? true : (data.accepted ?? false);
const membershipData = { ...data, accepted: acceptedStatus };
const membership = await this.organizationsTeamsMembershipsService.createOrgTeamMembership(
teamId,
membershipData
);
if (membership.accepted) {
try {
await updateNewTeamMemberEventTypes(user.id, teamId);
@@ -37,6 +37,20 @@ export class OrganizationRepositoryFixture {
});
}
async updateSettings(
teamId: Team["id"],
settings: {
orgAutoAcceptEmail?: string;
isOrganizationVerified?: boolean;
isOrganizationConfigured?: boolean;
}
) {
return this.prismaWriteClient.organizationSettings.update({
where: { organizationId: teamId },
data: settings,
});
}
async delete(teamId: Team["id"]) {
return await this.prismaWriteClient.$transaction(async (prisma) => {
await prisma.organizationSettings.delete({
+2 -1
View File
@@ -29,7 +29,8 @@
"@calcom/platform-libraries/conferencing": ["../../../packages/platform/libraries/conferencing.ts"],
"@calcom/platform-libraries/repositories": ["../../../packages/platform/libraries/repositories.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"]
},
"incremental": true,
"skipLibCheck": true,
@@ -6,7 +6,7 @@ import { cookies, headers } from "next/headers";
import { redirect } from "next/navigation";
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
import { AvailabilitySliderTable } from "@calcom/features/timezone-buddy/components/AvailabilitySliderTable";
import { getScheduleListItemData } from "@calcom/lib/schedules/transformers/getScheduleListItemData";
@@ -61,8 +61,9 @@ const Page = async ({ searchParams: _searchParams }: PageProps) => {
};
const organizationId = session?.user?.profile?.organizationId ?? session?.user.org?.id;
const organizationRepository = getOrganizationRepository();
const isOrgPrivate = organizationId
? await OrganizationRepository.checkIfPrivate({
? await organizationRepository.checkIfPrivate({
orgId: organizationId,
})
: false;
@@ -4,12 +4,13 @@ import { z } from "zod";
import LicenseRequired from "@calcom/features/ee/common/components/LicenseRequired";
import { OrgForm } from "@calcom/features/ee/organizations/pages/settings/admin/AdminOrgEditPage";
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";
const orgIdSchema = z.object({ id: z.coerce.number() });
export const generateMetadata = async ({ params }: { params: Params }) => {
const organizationRepository = getOrganizationRepository();
const input = orgIdSchema.safeParse(await params);
if (!input.success) {
return await _generateMetadata(
@@ -21,7 +22,7 @@ export const generateMetadata = async ({ params }: { params: Params }) => {
);
}
const org = await OrganizationRepository.adminFindById({ id: input.data.id });
const org = await organizationRepository.adminFindById({ id: input.data.id });
return await _generateMetadata(
(t) => `${t("editing_org")}: ${org.name}`,
@@ -33,11 +34,12 @@ export const generateMetadata = async ({ params }: { params: Params }) => {
};
const Page = async ({ params }: { params: Params }) => {
const organizationRepository = getOrganizationRepository();
const input = orgIdSchema.safeParse(await params);
if (!input.success) throw new Error("Invalid access");
const org = await OrganizationRepository.adminFindById({ id: input.data.id });
const org = await organizationRepository.adminFindById({ id: input.data.id });
const t = await getTranslate();
return (
<SettingsHeader title={`${t("editing_org")}: ${org.name}`} description={t("admin_orgs_edit_description")}>
@@ -2,7 +2,7 @@ import { _generateMetadata, getTranslate } from "app/_utils";
import { redirect } from "next/navigation";
import { OtherTeamsListing } from "@calcom/features/ee/organizations/pages/components/OtherTeamsListing";
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";
import { validateUserHasOrg } from "../../../actions/validateUserHasOrg";
@@ -24,11 +24,12 @@ const Page = async () => {
redirect("/auth/login");
}
const organizationId = session?.user?.org?.id;
const organizationRepository = getOrganizationRepository();
const otherTeams = organizationId
? await OrganizationRepository.findTeamsInOrgIamNotPartOf({
userId: session?.user.id,
parentId: organizationId,
})
? await organizationRepository.findTeamsInOrgIamNotPartOf({
userId: session?.user.id,
parentId: organizationId,
})
: [];
return (
+3 -4
View File
@@ -10,7 +10,6 @@ import { moveUserToMatchingOrg } from "./verify-email";
// TODO: This test passes but coverage is very low.
vi.mock("@calcom/trpc/server/routers/viewer/teams/inviteMember/inviteMember.handler");
vi.mock("@calcom/features/ee/organizations/repositories/OrganizationRepository");
vi.mock("@calcom/prisma", () => {
return {
prisma: vi.fn(),
@@ -37,7 +36,7 @@ describe("moveUserToMatchingOrg", () => {
});
it("should not proceed if no matching organization is found", async () => {
organizationScenarios.OrganizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail.fakeNoMatch();
organizationScenarios.organizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail.fakeNoMatch();
await moveUserToMatchingOrg({ email });
@@ -64,7 +63,7 @@ describe("moveUserToMatchingOrg", () => {
requestedSlug: "requested-test-org",
};
organizationScenarios.OrganizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail.fakeReturnOrganization(
organizationScenarios.organizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail.fakeReturnOrganization(
org,
{ email }
);
@@ -85,7 +84,7 @@ describe("moveUserToMatchingOrg", () => {
requestedSlug: "requested-test-org",
};
organizationScenarios.OrganizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail.fakeReturnOrganization(
organizationScenarios.organizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail.fakeReturnOrganization(
org,
{ email }
);
+3 -2
View File
@@ -2,7 +2,7 @@ import type { NextApiRequest, NextApiResponse } from "next";
import { z } from "zod";
import dayjs from "@calcom/dayjs";
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
import { StripeBillingService } from "@calcom/features/ee/billing/stripe-billing-service";
import { OnboardingPathService } from "@calcom/features/onboarding/lib/onboarding-path.service";
import { WEBAPP_URL } from "@calcom/lib/constants";
@@ -21,7 +21,8 @@ const USER_ALREADY_EXISTING_MESSAGE = "A User already exists with this email";
// TODO: To be unit tested
export async function moveUserToMatchingOrg({ email }: { email: string }) {
const org = await OrganizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail({ email });
const organizationRepository = getOrganizationRepository();
const org = await organizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail({ email });
if (!org) {
return;
@@ -8,7 +8,7 @@ import {
} from "@calcom/app-store/dailyvideo/lib/VideoApiAdapter";
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
import { BookingRepository } from "@calcom/features/bookings/repositories/BookingRepository";
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
import { EventTypeRepository } from "@calcom/features/eventtypes/repositories/eventTypeRepository";
import { getCalVideoReference } from "@calcom/features/get-cal-video-reference";
import { UserRepository } from "@calcom/features/users/repositories/UserRepository";
@@ -167,8 +167,10 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
).profile
: null;
const organizationRepository = getOrganizationRepository();
const calVideoLogo = profile?.organization
? await OrganizationRepository.findCalVideoLogoByOrgId({ id: profile.organization.id })
? await organizationRepository.findCalVideoLogoByOrgId({ id: profile.organization.id })
: null;
//daily.co calls have a 14 days exit buffer when a user enters a call when it's not available it will trigger the modals
@@ -6,7 +6,7 @@ import { metadata as googleCalendarMetadata } from "@calcom/app-store/googlecale
import { metadata as googleMeetMetadata } from "@calcom/app-store/googlevideo/_metadata";
import type { ServiceAccountKey } from "@calcom/features/delegation-credentials/repositories/DelegationCredentialRepository";
import { DelegationCredentialRepository } from "@calcom/features/delegation-credentials/repositories/DelegationCredentialRepository";
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import { organizationRepositoryMock } from "@calcom/features/ee/organizations/__mocks__/organizationMock";
import { SMSLockState, RRTimestampBasis } from "@calcom/prisma/enums";
import type { CredentialForCalendarService, CredentialPayload } from "@calcom/types/Credential";
@@ -20,11 +20,8 @@ import {
getAllDelegationCredentialsForUserIncludeServiceAccountKey,
} from "./delegationCredential";
// Mock OrganizationRepository
vi.mock("@calcom/features/ee/organizations/repositories/OrganizationRepository", () => ({
OrganizationRepository: {
findByMemberEmail: vi.fn(),
},
vi.mock("@calcom/prisma", () => ({
prisma: {},
}));
// Mock DelegationCredentialRepository
@@ -187,7 +184,7 @@ describe("getAllDelegationCredentialsForUserIncludeServiceAccountKey", () => {
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(OrganizationRepository.findByMemberEmail).mockResolvedValue(mockOrganization);
organizationRepositoryMock.findByMemberEmail.mockResolvedValue(mockOrganization);
});
it("should return empty array when no DelegationCredential found", async () => {
@@ -16,7 +16,7 @@ import { CredentialRepository } from "@calcom/features/credentials/repositories/
import createUsersAndConnectToOrg from "@calcom/features/ee/dsync/lib/users/createUsersAndConnectToOrg";
import ImpersonationProvider from "@calcom/features/ee/impersonation/lib/ImpersonationProvider";
import { getOrgFullOrigin, subdomainSuffix } from "@calcom/features/ee/organizations/lib/orgDomains";
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
import { clientSecretVerifier, hostedCal, isSAMLLoginEnabled } from "@calcom/features/ee/sso/lib/saml";
import { ProfileRepository } from "@calcom/features/profile/repositories/ProfileRepository";
import { UserRepository } from "@calcom/features/users/repositories/UserRepository";
@@ -389,7 +389,8 @@ if (isSAMLLoginEnabled) {
const hostedCal = Boolean(HOSTED_CAL_FEATURES);
if (hostedCal && email) {
const domain = getDomainFromEmail(email);
const org = await OrganizationRepository.getVerifiedOrganizationByAutoAcceptEmailDomain(domain);
const organizationRepository = getOrganizationRepository();
const org = await organizationRepository.getVerifiedOrganizationByAutoAcceptEmailDomain(domain);
if (org) {
const createUsersAndConnectToOrgProps = {
emailsToCreate: [email],
@@ -407,7 +408,7 @@ if (isSAMLLoginEnabled) {
}
if (!user) throw new Error(ErrorCode.UserNotFound);
}
const [userProfile] = user?.allProfiles;
const [userProfile] = user?.allProfiles ?? [];
return {
id: id as unknown as number,
firstName,
@@ -1,9 +1,9 @@
import { createContainer } from "@calcom/lib/di/di";
import { createContainer } from "@calcom/features/di/di";
import {
type BookingCancelService,
moduleLoader as bookingCancelServiceModule,
} from "../modules/BookingCancelService.module";
} from "./BookingCancelService.module";
const bookingCancelServiceContainer = createContainer();
@@ -1,6 +1,6 @@
import { BookingCancelService } from "@calcom/features/bookings/lib/handleCancelBooking";
import { bindModuleToClassOnToken, createModule } from "@calcom/lib/di/di";
import { DI_TOKENS } from "@calcom/lib/di/tokens";
import { bindModuleToClassOnToken, createModule } from "@calcom/features/di/di";
import { DI_TOKENS } from "@calcom/features/di/tokens";
import { moduleLoader as prismaModuleLoader } from "@calcom/features/di/modules/Prisma";
const thisModule = createModule();
@@ -2,22 +2,27 @@ import prismock from "../../../../tests/libs/__mocks__/prisma";
import { describe, expect, it, beforeEach, vi } from "vitest";
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import { encryptServiceAccountKey } from "@calcom/lib/server/serviceAccountKey";
import { DelegationCredentialRepository } from "./DelegationCredentialRepository";
vi.mock("@calcom/features/ee/organizations/repositories/OrganizationRepository", () => ({
OrganizationRepository: {
findByMemberEmail: vi.fn(),
},
const mockOrganizationRepository = {
findByMemberEmail: vi.fn(),
};
vi.mock("@calcom/features/ee/organizations/di/OrganizationRepository.container", () => ({
getOrganizationRepository: () => mockOrganizationRepository,
}));
vi.mock("@calcom/prisma", () => ({
prisma: {},
}));
// Mock service account key functions
vi.mock("@calcom/lib/crypto", async (importOriginal) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const actual = await importOriginal<any>();
const actual = await importOriginal<typeof import("@calcom/lib/crypto")>();
return {
...actual,
symmetricEncrypt: vi.fn((serviceAccountKey) => {
@@ -92,8 +97,8 @@ const createTestDelegationCredential = async (overrides = {}) => {
});
};
const setupOrganizationMock = (returnValue: any) => {
vi.mocked(OrganizationRepository.findByMemberEmail).mockResolvedValue(returnValue);
const setupOrganizationMock = (returnValue: { id: number } | null) => {
mockOrganizationRepository.findByMemberEmail.mockResolvedValue(returnValue);
};
describe("DelegationCredentialRepository", () => {
@@ -1,4 +1,4 @@
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
import logger from "@calcom/lib/logger";
import {
serviceAccountKeySchema,
@@ -137,7 +137,8 @@ export class DelegationCredentialRepository {
prefix: ["findUniqueByOrgMemberEmailIncludeSensitiveServiceAccountKey"],
});
log.debug("called with", { email });
const organization = await OrganizationRepository.findByMemberEmail({ email });
const organizationRepository = getOrganizationRepository();
const organization = await organizationRepository.findByMemberEmail({ email });
if (!organization) {
log.debug("Email not found in any organization:", email);
return null;
+2 -1
View File
@@ -1,6 +1,6 @@
import { BOOKING_DI_TOKENS } from "@calcom/features/bookings/di/tokens";
import { HASHED_LINK_DI_TOKENS } from "@calcom/features/hashedLink/di/tokens";
import { ORGANIZATION_DI_TOKENS } from "@calcom/features/ee/organizations/di/tokens";
import { WATCHLIST_DI_TOKENS } from "./watchlist/Watchlist.tokens";
export const DI_TOKENS = {
@@ -63,4 +63,5 @@ export const DI_TOKENS = {
...HASHED_LINK_DI_TOKENS,
// Watchlist service tokens
...WATCHLIST_DI_TOKENS,
...ORGANIZATION_DI_TOKENS,
};
@@ -25,7 +25,7 @@ const session: Session = {
vi.mock("@calcom/prisma", () => {
return {
default: vi.fn(),
prisma: vi.fn(),
};
});
@@ -1,23 +1,24 @@
import { vi, beforeEach } from "vitest";
import { mockReset, mockDeep } from "vitest-mock-extended";
import { mockDeep, mockReset } from "vitest-mock-extended";
import type * as organization from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import type { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
const mockedSingleton = mockDeep<OrganizationRepository>();
vi.mock("@calcom/features/ee/organizations/di/OrganizationRepository.container", () => ({
getOrganizationRepository: () => mockedSingleton,
}));
vi.mock("@calcom/features/ee/organizations/repositories/OrganizationRepository", () => organizationMock);
type OrganizationModule = typeof organization;
beforeEach(() => {
mockReset(organizationMock);
mockReset(mockedSingleton);
});
const organizationMock = mockDeep<OrganizationModule>();
const OrganizationRepository = organizationMock.OrganizationRepository;
export const organizationScenarios = {
OrganizationRepository: {
organizationRepository: {
findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fakeReturnOrganization: (org: any, forInput: any) => {
OrganizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail.mockImplementation(
mockedSingleton.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail.mockImplementation(
(arg) => {
if (forInput.email === arg.email) {
return org;
@@ -29,10 +30,11 @@ export const organizationScenarios = {
);
},
fakeNoMatch: () => {
OrganizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail.mockResolvedValue(null);
mockedSingleton.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail.mockResolvedValue(null);
},
},
} satisfies Partial<Record<keyof OrganizationModule["OrganizationRepository"], unknown>>,
} satisfies Partial<Record<keyof OrganizationModule, unknown>>;
},
};
export default organizationMock;
export { mockedSingleton as organizationRepositoryMock };
export default { organizationRepository: mockedSingleton };
@@ -0,0 +1,27 @@
import { bindModuleToClassOnToken, createModule } from "@calcom/features/di/di";
import { OrganizationMembershipService } from "@calcom/features/ee/organizations/lib/service/OrganizationMembershipService";
import { moduleLoader as organizationRepositoryModuleLoader } from "./OrganizationRepository.module";
import { ORGANIZATION_DI_TOKENS } from "./tokens";
const thisModule = createModule();
const token = ORGANIZATION_DI_TOKENS.ORGANIZATION_MEMBERSHIP_SERVICE;
const moduleToken = ORGANIZATION_DI_TOKENS.ORGANIZATION_MEMBERSHIP_SERVICE_MODULE;
const loadModule = bindModuleToClassOnToken({
module: thisModule,
moduleToken,
token,
classs: OrganizationMembershipService,
depsMap: {
organizationRepository: organizationRepositoryModuleLoader,
},
});
export const moduleLoader = {
token,
loadModule,
};
export type { OrganizationMembershipService };
@@ -0,0 +1,14 @@
import { createContainer } from "@calcom/features/di/di";
import {
type OrganizationRepository,
moduleLoader as organizationRepositoryModule,
} from "./OrganizationRepository.module";
const organizationRepositoryContainer = createContainer();
export function getOrganizationRepository(): OrganizationRepository {
organizationRepositoryModule.loadModule(organizationRepositoryContainer);
return organizationRepositoryContainer.get<OrganizationRepository>(organizationRepositoryModule.token);
}
@@ -0,0 +1,26 @@
import { bindModuleToClassOnToken, createModule, type ModuleLoader } from "@calcom/features/di/di";
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import { moduleLoader as prismaModuleLoader } from "@calcom/features/di/modules/Prisma";
import { ORGANIZATION_DI_TOKENS } from "./tokens";
export const organizationRepositoryModule = createModule();
const token = ORGANIZATION_DI_TOKENS.ORGANIZATION_REPOSITORY;
const moduleToken = ORGANIZATION_DI_TOKENS.ORGANIZATION_REPOSITORY_MODULE;
const loadModule = bindModuleToClassOnToken({
module: organizationRepositoryModule,
moduleToken,
token,
classs: OrganizationRepository,
depsMap: {
prismaClient: prismaModuleLoader,
},
});
export const moduleLoader: ModuleLoader = {
token,
loadModule,
};
export type { OrganizationRepository };
@@ -0,0 +1,6 @@
export const ORGANIZATION_DI_TOKENS = {
ORGANIZATION_REPOSITORY: Symbol("OrganizationRepository"),
ORGANIZATION_REPOSITORY_MODULE: Symbol("OrganizationRepositoryModule"),
ORGANIZATION_MEMBERSHIP_SERVICE: Symbol("OrganizationMembershipService"),
ORGANIZATION_MEMBERSHIP_SERVICE_MODULE: Symbol("OrganizationMembershipServiceModule"),
};
@@ -1,4 +1,4 @@
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
import { UserPermissionRole } from "@calcom/kysely/types";
import { ORGANIZATION_SELF_SERVE_MIN_SEATS, ORGANIZATION_SELF_SERVE_PRICE } from "@calcom/lib/constants";
import logger from "@calcom/lib/logger";
@@ -39,7 +39,8 @@ export class OrganizationPermissionService {
* If an onboarding is complete then it also means that org is created already.
*/
async hasConflictingOrganization({ slug }: { slug: string }): Promise<boolean> {
return !!(await OrganizationRepository.findBySlug({ slug }));
const organizationRepository = getOrganizationRepository();
return !!(await organizationRepository.findBySlug({ slug }));
}
async hasCompletedOnboarding(email: string): Promise<boolean> {
@@ -1,12 +1,13 @@
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
/**
* It assumes that a user can only impersonate the members of the organization he is logged in to.
* Note: Ensuring that one organization's member can't impersonate other organization's member isn't the job of this function
*/
export async function ensureOrganizationIsReviewed(loggedInUserOrgId: number | undefined) {
const organizationRepository = getOrganizationRepository();
if (loggedInUserOrgId) {
const org = await OrganizationRepository.findByIdIncludeOrganizationSettings({
const org = await organizationRepository.findByIdIncludeOrganizationSettings({
id: loggedInUserOrgId,
});
@@ -0,0 +1,6 @@
export interface IOrganizationRepository {
getOrganizationAutoAcceptSettings(organizationId: number): Promise<{
orgAutoAcceptEmail: string | null;
isOrganizationVerified: boolean | null;
} | null>;
}
@@ -0,0 +1,37 @@
import type { IOrganizationRepository } from "../repository/IOrganizationRepository";
export interface IOrganizationMembershipServiceDependencies {
organizationRepository: IOrganizationRepository;
}
export class OrganizationMembershipService {
constructor(private readonly deps: IOrganizationMembershipServiceDependencies) { }
/**
* Determines if user should be auto-accepted to an organization or its sub-teams based on email domain
*/
async shouldAutoAccept({
organizationId,
userEmail,
}: {
organizationId: number;
userEmail: string;
}): Promise<boolean> {
const orgSettings = await this.deps.organizationRepository.getOrganizationAutoAcceptSettings(
organizationId
);
if (!orgSettings) return false;
const { orgAutoAcceptEmail, isOrganizationVerified } = orgSettings;
if (!isOrganizationVerified || !orgAutoAcceptEmail) return false;
// Case-insensitive comparison (email domains are case-insensitive per RFC)
const emailDomain = userEmail.split("@")[1]?.trim().toLowerCase();
const autoAcceptEmailDomain = orgAutoAcceptEmail.trim().toLowerCase();
if (!emailDomain) return false;
return emailDomain === autoAcceptEmailDomain;
}
}
@@ -8,7 +8,7 @@ import {
findUserToBeOrgOwner,
setupDomain,
} from "@calcom/features/ee/organizations/lib/server/orgCreationUtils";
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
import { UserRepository } from "@calcom/features/users/repositories/UserRepository";
import { DEFAULT_SCHEDULE, getAvailabilityFromSchedule } from "@calcom/lib/availability";
import { WEBAPP_URL } from "@calcom/lib/constants";
@@ -296,8 +296,9 @@ export abstract class BaseOnboardingService implements IOrganizationOnboardingSe
owner: NonNullable<Awaited<ReturnType<typeof findUserToBeOrgOwner>>>;
orgData: OrganizationData;
}) {
const organizationRepository = getOrganizationRepository();
const orgOwnerTranslation = await getTranslation(owner.locale || "en", "common");
let organization = orgData.id ? await OrganizationRepository.findById({ id: orgData.id }) : null;
let organization = orgData.id ? await organizationRepository.findById({ id: orgData.id }) : null;
if (organization) {
log.info(
@@ -328,7 +329,7 @@ export abstract class BaseOnboardingService implements IOrganizationOnboardingSe
const nonOrgUsername = owner.username || "";
// Create organization first to get the ID
const orgCreationResult = await OrganizationRepository.createWithExistingUserAsOwner({
const orgCreationResult = await organizationRepository.createWithExistingUserAsOwner({
orgData: {
...orgData,
// Don't pass brand assets yet - will be uploaded after org is created
@@ -394,9 +395,10 @@ export abstract class BaseOnboardingService implements IOrganizationOnboardingSe
email: string;
orgData: OrganizationData;
}) {
const organizationRepository = getOrganizationRepository();
let organization = orgData.id
? await OrganizationRepository.findById({ id: orgData.id })
: await OrganizationRepository.findBySlug({ slug: orgData.slug });
? await organizationRepository.findById({ id: orgData.id })
: await organizationRepository.findBySlug({ slug: orgData.slug });
if (organization) {
log.info(
@@ -410,7 +412,7 @@ export abstract class BaseOnboardingService implements IOrganizationOnboardingSe
return { organization, owner };
}
const orgCreationResult = await OrganizationRepository.createWithNonExistentOwner({
const orgCreationResult = await organizationRepository.createWithNonExistentOwner({
orgData: {
...orgData,
// To be uploaded after org is created
@@ -670,7 +672,8 @@ export abstract class BaseOnboardingService implements IOrganizationOnboardingSe
}
const existingMetadata = teamMetadataStrictSchema.parse(organization.metadata);
const updatedOrganization = await OrganizationRepository.updateStripeSubscriptionDetails({
const organizationRepository = getOrganizationRepository();
const updatedOrganization = await organizationRepository.updateStripeSubscriptionDetails({
id: organization.id,
stripeSubscriptionId: paymentSubscriptionId,
stripeSubscriptionItemId: paymentSubscriptionItemId,
@@ -680,7 +683,8 @@ export abstract class BaseOnboardingService implements IOrganizationOnboardingSe
}
protected async hasConflictingOrganization({ slug, onboardingId }: { slug: string; onboardingId: string }) {
const organization = await OrganizationRepository.findBySlugIncludeOnboarding({ slug });
const organizationRepository = getOrganizationRepository();
const organization = await organizationRepository.findBySlugIncludeOnboarding({ slug });
if (!organization?.organizationOnboarding) {
return false;
}
@@ -1,5 +1,5 @@
import { findUserToBeOrgOwner } from "@calcom/features/ee/organizations/lib/server/orgCreationUtils";
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
import { IS_SELF_HOSTED } from "@calcom/lib/constants";
import logger from "@calcom/lib/logger";
import { safeStringify } from "@calcom/lib/safeStringify";
@@ -143,6 +143,7 @@ export class BillingEnabledOrgOnboardingService extends BaseOnboardingService {
organizationOnboarding: OrganizationOnboardingData,
paymentDetails?: { subscriptionId: string; subscriptionItemId: string }
): Promise<{ organization: Team; owner: User }> {
const organizationRepository = getOrganizationRepository();
log.info(
"createOrganization (billing-enabled)",
safeStringify({
@@ -245,7 +246,7 @@ export class BillingEnabledOrgOnboardingService extends BaseOnboardingService {
if (!organization.slug) {
try {
const { slug } = await OrganizationRepository.setSlug({
const { slug } = await organizationRepository.setSlug({
id: organization.id,
slug: organizationOnboarding.slug,
});
@@ -1,6 +1,6 @@
import { LicenseKeySingleton } from "@calcom/ee/common/server/LicenseKeyService";
import { findUserToBeOrgOwner } from "@calcom/features/ee/organizations/lib/server/orgCreationUtils";
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
import { IS_SELF_HOSTED } from "@calcom/lib/constants";
import logger from "@calcom/lib/logger";
import { safeStringify } from "@calcom/lib/safeStringify";
@@ -113,6 +113,7 @@ export class SelfHostedOrganizationOnboardingService extends BaseOnboardingServi
async createOrganization(
organizationOnboarding: OrganizationOnboardingData
): Promise<{ organization: Team; owner: User }> {
const organizationRepository = getOrganizationRepository();
log.info(
"createOrganization (self-hosted)",
safeStringify({
@@ -205,7 +206,7 @@ export class SelfHostedOrganizationOnboardingService extends BaseOnboardingServi
if (!organization.slug) {
try {
const { slug } = await OrganizationRepository.setSlug({
const { slug } = await organizationRepository.setSlug({
id: organization.id,
slug: organizationOnboarding.slug,
});
@@ -2,7 +2,7 @@
import { useState } from "react";
import type { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import type { OrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.module";
import { trackFormbricksAction } from "@calcom/features/formbricks/formbricks-client";
import { trpc } from "@calcom/trpc/react";
import { showToast } from "@calcom/ui/components/toast";
@@ -10,7 +10,7 @@ import { showToast } from "@calcom/ui/components/toast";
import OtherTeamListItem from "./OtherTeamListItem";
interface Props {
teams: Awaited<ReturnType<typeof OrganizationRepository.findTeamsInOrgIamNotPartOf>>;
teams: Awaited<ReturnType<OrganizationRepository["findTeamsInOrgIamNotPartOf"]>>;
pending?: boolean;
}
@@ -1,13 +1,13 @@
"use client";
import type { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import type { OrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.module";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { EmptyScreen } from "@calcom/ui/components/empty-screen";
import OtherTeamList from "./OtherTeamList";
type OtherTeamsListingProps = {
teams: Awaited<ReturnType<typeof OrganizationRepository.findTeamsInOrgIamNotPartOf>>;
teams: Awaited<ReturnType<OrganizationRepository["findTeamsInOrgIamNotPartOf"]>>;
};
export function OtherTeamsListing({ teams }: OtherTeamsListingProps) {
const { t } = useLocale();
@@ -6,9 +6,11 @@ import { OrganizationRepository } from "@calcom/features/ee/organizations/reposi
import type { Prisma } from "@calcom/prisma/client";
vi.mock("@calcom/lib/server/repository/teamUtils", () => ({
getParsedTeam: (org: any) => org,
getParsedTeam: <T>(org: T) => org,
}));
const organizationRepository = new OrganizationRepository({ prismaClient: prismock });
async function createOrganization(
data: Prisma.TeamCreateInput & {
organizationSettings: {
@@ -72,7 +74,7 @@ beforeEach(async () => {
describe("Organization.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail", () => {
it("should return null if no organization matches the email domain", async () => {
const result = await OrganizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail({
const result = await organizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail({
email: "test@example.com",
});
@@ -84,7 +86,7 @@ describe("Organization.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail", () =
await createReviewedOrganization({ name: "Test Org 2", orgAutoAcceptEmail: "example.com" });
await expect(
OrganizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail({ email: "test@example.com" })
organizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail({ email: "test@example.com" })
).rejects.toThrow("Multiple organizations found with the same auto accept email domain");
});
@@ -94,7 +96,7 @@ describe("Organization.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail", () =
orgAutoAcceptEmail: "example.com",
});
const result = await OrganizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail({
const result = await organizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail({
email: "test@example.com",
});
@@ -104,7 +106,7 @@ describe("Organization.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail", () =
it("should not confuse a team with organization", async () => {
await createTeam({ name: "Test Team", orgAutoAcceptEmail: "example.com" });
const result = await OrganizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail({
const result = await organizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail({
email: "test@example.com",
});
@@ -114,7 +116,7 @@ describe("Organization.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail", () =
it("should correctly match orgAutoAcceptEmail", async () => {
await createReviewedOrganization({ name: "Test Org", orgAutoAcceptEmail: "noexample.com" });
const result = await OrganizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail({
const result = await organizationRepository.findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail({
email: "test@example.com",
});
@@ -129,7 +131,7 @@ describe("Organization.getVerifiedOrganizationByAutoAcceptEmailDomain", () => {
organizationSettings: { create: { orgAutoAcceptEmail: "cal.com", isOrganizationVerified: true } },
});
const result = await OrganizationRepository.getVerifiedOrganizationByAutoAcceptEmailDomain("cal.com");
const result = await organizationRepository.getVerifiedOrganizationByAutoAcceptEmailDomain("cal.com");
expect(result).toEqual({
id: verifiedOrganization.id,
@@ -145,7 +147,7 @@ describe("Organization.getVerifiedOrganizationByAutoAcceptEmailDomain", () => {
organizationSettings: { create: { orgAutoAcceptEmail: "cal.com", isOrganizationVerified: false } },
});
const result = await OrganizationRepository.getVerifiedOrganizationByAutoAcceptEmailDomain("cal.com");
const result = await organizationRepository.getVerifiedOrganizationByAutoAcceptEmailDomain("cal.com");
expect(result).toEqual(null);
});
@@ -169,7 +171,7 @@ describe("Organization.create", () => {
bannerUrl: "https://example.com/banner.jpg",
};
const organization = await OrganizationRepository.create(orgData);
const organization = await organizationRepository.create(orgData);
expect(organization).toMatchObject({
name: "Test Organization",
@@ -198,7 +200,7 @@ describe("Organization.create", () => {
bannerUrl: null,
};
const organization = await OrganizationRepository.create(orgData);
const organization = await organizationRepository.create(orgData);
expect(organization).toMatchObject({
name: "Test Organization",
@@ -6,7 +6,7 @@ import { UserRepository } from "@calcom/features/users/repositories/UserReposito
import logger from "@calcom/lib/logger";
import { safeStringify } from "@calcom/lib/safeStringify";
import { getParsedTeam } from "@calcom/lib/server/repository/teamUtils";
import { prisma } from "@calcom/prisma";
import type { PrismaClient } from "@calcom/prisma";
import { MembershipRole } from "@calcom/prisma/enums";
import type { CreationSource } from "@calcom/prisma/enums";
import type { teamMetadataStrictSchema } from "@calcom/prisma/zod-utils";
@@ -20,7 +20,13 @@ const orgSelect = {
};
export class OrganizationRepository {
static async createWithExistingUserAsOwner({
protected readonly prismaClient: PrismaClient;
constructor(deps: { prismaClient: PrismaClient }) {
this.prismaClient = deps.prismaClient;
}
async createWithExistingUserAsOwner({
orgData,
owner,
}: {
@@ -57,7 +63,7 @@ export class OrganizationRepository {
organizationId: organization.id,
});
await prisma.membership.create({
await this.prismaClient.membership.create({
data: {
createdAt: new Date(),
userId: owner.id,
@@ -69,7 +75,7 @@ export class OrganizationRepository {
return { organization, ownerProfile };
}
static async createWithNonExistentOwner({
async createWithNonExistentOwner({
orgData,
owner,
creationSource,
@@ -97,7 +103,7 @@ export class OrganizationRepository {
logger.debug("createWithNonExistentOwner", safeStringify({ orgData, owner }));
const organization = await this.create(orgData);
const ownerUsernameInOrg = getOrgUsernameFromEmail(owner.email, orgData.autoAcceptEmail);
const userRepo = new UserRepository(prisma);
const userRepo = new UserRepository(this.prismaClient);
const ownerInDb = await userRepo.create({
email: owner.email,
username: ownerUsernameInOrg,
@@ -106,7 +112,7 @@ export class OrganizationRepository {
creationSource,
});
await prisma.membership.create({
await this.prismaClient.membership.create({
data: {
createdAt: new Date(),
userId: ownerInDb.id,
@@ -125,7 +131,7 @@ export class OrganizationRepository {
};
}
static async create(orgData: {
async create(orgData: {
name: string;
slug: string | null;
isOrganizationConfigured: boolean;
@@ -141,7 +147,7 @@ export class OrganizationRepository {
bannerUrl: string | null;
requestedSlug?: string | null;
}) {
return await prisma.team.create({
return await this.prismaClient.team.create({
data: {
name: orgData.name,
isOrganization: true,
@@ -176,8 +182,8 @@ export class OrganizationRepository {
});
}
static async findById({ id }: { id: number }) {
return prisma.team.findUnique({
async findById({ id }: { id: number }) {
return this.prismaClient.team.findUnique({
where: {
id,
isOrganization: true,
@@ -185,9 +191,9 @@ export class OrganizationRepository {
});
}
static async findBySlug({ slug }: { slug: string }) {
async findBySlug({ slug }: { slug: string }) {
// Slug is unique but could be null as well, so we can't use findUnique
return prisma.team.findFirst({
return this.prismaClient.team.findFirst({
where: {
slug,
isOrganization: true,
@@ -195,8 +201,8 @@ export class OrganizationRepository {
});
}
static async findBySlugIncludeOnboarding({ slug }: { slug: string }) {
return prisma.team.findFirst({
async findBySlugIncludeOnboarding({ slug }: { slug: string }) {
return this.prismaClient.team.findFirst({
where: { slug, isOrganization: true },
include: {
organizationOnboarding: {
@@ -213,8 +219,8 @@ export class OrganizationRepository {
});
}
static async findByIdIncludeOrganizationSettings({ id }: { id: number }) {
return prisma.team.findUnique({
async findByIdIncludeOrganizationSettings({ id }: { id: number }) {
return this.prismaClient.team.findUnique({
where: {
id,
isOrganization: true,
@@ -226,9 +232,9 @@ export class OrganizationRepository {
});
}
static async findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail({ email }: { email: string }) {
async findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail({ email }: { email: string }) {
const emailDomain = email.split("@").at(-1);
const orgs = await prisma.team.findMany({
const orgs = await this.prismaClient.team.findMany({
where: {
isOrganization: true,
isPlatform: false,
@@ -254,8 +260,8 @@ export class OrganizationRepository {
return getParsedTeam(org);
}
static async findCurrentOrg({ userId, orgId }: { userId: number; orgId: number }) {
const membership = await prisma.membership.findUnique({
async findCurrentOrg({ userId, orgId }: { userId: number; orgId: number }) {
const membership = await this.prismaClient.membership.findUnique({
where: {
userId_teamId: {
userId,
@@ -267,7 +273,7 @@ export class OrganizationRepository {
},
});
const organizationSettings = await prisma.organizationSettings.findUnique({
const organizationSettings = await this.prismaClient.organizationSettings.findUnique({
where: {
organizationId: orgId,
},
@@ -307,8 +313,8 @@ export class OrganizationRepository {
};
}
static async findTeamsInOrgIamNotPartOf({ userId, parentId }: { userId: number; parentId: number | null }) {
const teamsInOrgIamNotPartOf = await prisma.team.findMany({
async findTeamsInOrgIamNotPartOf({ userId, parentId }: { userId: number; parentId: number | null }) {
const teamsInOrgIamNotPartOf = await this.prismaClient.team.findMany({
where: {
parentId,
members: {
@@ -329,8 +335,8 @@ export class OrganizationRepository {
return teamsInOrgIamNotPartOf;
}
static async adminFindById({ id }: { id: number }) {
const org = await prisma.team.findUnique({
async adminFindById({ id }: { id: number }) {
const org = await this.prismaClient.team.findUnique({
where: {
id,
},
@@ -374,8 +380,8 @@ export class OrganizationRepository {
return { ...org, metadata: parsedMetadata };
}
static async findByMemberEmail({ email }: { email: string }) {
const organization = await prisma.team.findFirst({
async findByMemberEmail({ email }: { email: string }) {
const organization = await this.prismaClient.team.findFirst({
where: {
isOrganization: true,
members: {
@@ -388,10 +394,10 @@ export class OrganizationRepository {
return organization ?? null;
}
static async findByMemberEmailId({ email }: { email: string }) {
async findByMemberEmailId({ email }: { email: string }) {
const log = logger.getSubLogger({ prefix: ["findByMemberEmailId"] });
log.debug("called with", { email });
const organization = await prisma.team.findFirst({
const organization = await this.prismaClient.team.findFirst({
where: {
isOrganization: true,
members: {
@@ -407,8 +413,8 @@ export class OrganizationRepository {
return organization;
}
static async findCalVideoLogoByOrgId({ id }: { id: number }) {
const org = await prisma.team.findUnique({
async findCalVideoLogoByOrgId({ id }: { id: number }) {
const org = await this.prismaClient.team.findUnique({
where: {
id,
},
@@ -420,8 +426,8 @@ export class OrganizationRepository {
return org?.calVideoLogo;
}
static async getVerifiedOrganizationByAutoAcceptEmailDomain(domain: string) {
return await prisma.team.findFirst({
async getVerifiedOrganizationByAutoAcceptEmailDomain(domain: string) {
return await this.prismaClient.team.findFirst({
where: {
organizationSettings: {
isOrganizationVerified: true,
@@ -439,14 +445,14 @@ export class OrganizationRepository {
});
}
static async setSlug({ id, slug }: { id: number; slug: string }) {
return await prisma.team.update({
async setSlug({ id, slug }: { id: number; slug: string }) {
return await this.prismaClient.team.update({
where: { id, isOrganization: true },
data: { slug },
});
}
static async updateStripeSubscriptionDetails({
async updateStripeSubscriptionDetails({
id,
stripeSubscriptionId,
stripeSubscriptionItemId,
@@ -457,7 +463,7 @@ export class OrganizationRepository {
stripeSubscriptionItemId: string;
existingMetadata: z.infer<typeof teamMetadataStrictSchema>;
}) {
return await prisma.team.update({
return await this.prismaClient.team.update({
where: { id, isOrganization: true },
data: {
metadata: {
@@ -469,8 +475,8 @@ export class OrganizationRepository {
});
}
static async checkIfPrivate({ orgId }: { orgId: number }) {
const team = await prisma.team.findUnique({
async checkIfPrivate({ orgId }: { orgId: number }) {
const team = await this.prismaClient.team.findUnique({
where: {
id: orgId,
isOrganization: true,
@@ -482,4 +488,20 @@ export class OrganizationRepository {
return team?.isPrivate ?? false;
}
async getOrganizationAutoAcceptSettings(organizationId: number) {
const org = await this.prismaClient.team.findUnique({
where: { id: organizationId, isOrganization: true },
select: {
organizationSettings: {
select: {
orgAutoAcceptEmail: true,
isOrganizationVerified: true,
},
},
},
});
return org?.organizationSettings ?? null;
}
}
+3 -2
View File
@@ -1,5 +1,5 @@
import createUsersAndConnectToOrg from "@calcom/features/ee/dsync/lib/users/createUsersAndConnectToOrg";
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
import { HOSTED_CAL_FEATURES } from "@calcom/lib/constants";
import type { PrismaClient } from "@calcom/prisma";
import { IdentityProvider } from "@calcom/prisma/enums";
@@ -36,7 +36,8 @@ export const ssoTenantProduct = async (prisma: PrismaClient, email: string) => {
});
const domain = email.split("@")[1];
const organization = await OrganizationRepository.getVerifiedOrganizationByAutoAcceptEmailDomain(domain);
const organizationRepository = getOrganizationRepository();
const organization = await organizationRepository.getVerifiedOrganizationByAutoAcceptEmailDomain(domain);
if (!organization)
throw new TRPCError({
@@ -0,0 +1,4 @@
export { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
export { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
export { OrganizationMembershipService } from "@calcom/features/ee/organizations/lib/service/OrganizationMembershipService";
export type { IOrganizationRepository } from "@calcom/features/ee/organizations/lib/repository/IOrganizationRepository";
+8
View File
@@ -79,6 +79,11 @@
"require": "./dist/bookings.cjs",
"types": "./dist/bookings.d.ts"
},
"./organizations": {
"import": "./dist/organizations.js",
"require": "./dist/organizations.cjs",
"types": "./dist/organizations.d.ts"
},
"./private-links": {
"import": "./dist/private-links.js",
"require": "./dist/private-links.cjs",
@@ -119,6 +124,9 @@
"bookings": [
"dist/bookings.d.ts"
],
"organizations": [
"dist/organizations.d.ts"
],
"private-links": [
"dist/private-links.d.ts"
],
@@ -36,6 +36,7 @@ export default defineConfig({
conferencing: resolve(__dirname, "./conferencing.ts"),
repositories: resolve(__dirname, "./repositories.ts"),
bookings: resolve(__dirname, "./bookings.ts"),
organizations: resolve(__dirname, "./organizations.ts"),
"private-links": resolve(__dirname, "./private-links.ts"),
pbac: resolve(__dirname, "./pbac.ts"),
},
@@ -1,4 +1,4 @@
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
import type { TrpcSessionUser } from "../../../types";
import type { TAdminGet } from "./adminGet.schema";
@@ -11,7 +11,8 @@ type AdminGetOptions = {
};
export const adminGetHandler = async ({ input }: AdminGetOptions) => {
return await OrganizationRepository.adminFindById({ id: input.id });
const organizationRepository = getOrganizationRepository();
return await organizationRepository.adminFindById({ id: input.id });
};
export default adminGetHandler;
@@ -4,7 +4,7 @@ import { getOrgFullOrigin } from "@calcom/ee/organizations/lib/orgDomains";
import { isNotACompanyEmail } from "@calcom/ee/organizations/lib/server/orgCreationUtils";
import { sendAdminOrganizationNotification, sendOrganizationCreationEmail } from "@calcom/emails";
import { sendEmailVerification } from "@calcom/features/auth/lib/verifyEmail";
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
import { UserRepository } from "@calcom/features/users/repositories/UserRepository";
import { DEFAULT_SCHEDULE, getAvailabilityFromSchedule } from "@calcom/lib/availability";
import {
@@ -44,6 +44,7 @@ const getIPAddress = async (url: string): Promise<string> => {
* TODO: To be removed. We need to reuse the logic from orgCreationUtils like in intentToCreateOrgHandler
*/
export const createHandler = async ({ input, ctx }: CreateOptions) => {
const organizationRepository = getOrganizationRepository();
const {
slug,
name,
@@ -189,7 +190,7 @@ export const createHandler = async ({ input, ctx }: CreateOptions) => {
// Create a new user and invite them as the owner of the organization
if (!orgOwner) {
const data = await OrganizationRepository.createWithNonExistentOwner({
const data = await organizationRepository.createWithNonExistentOwner({
orgData,
owner: {
email: orgOwnerEmail,
@@ -246,7 +247,7 @@ export const createHandler = async ({ input, ctx }: CreateOptions) => {
}
const nonOrgUsernameForOwner = orgOwner.username || "";
const { organization, ownerProfile } = await OrganizationRepository.createWithExistingUserAsOwner({
const { organization, ownerProfile } = await organizationRepository.createWithExistingUserAsOwner({
orgData,
owner: {
id: orgOwner.id,
@@ -1,4 +1,4 @@
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
import { FeaturesRepository } from "@calcom/features/flags/features.repository";
import prisma from "@calcom/prisma";
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
@@ -18,7 +18,8 @@ export const listHandler = async ({ ctx }: ListHandlerInput) => {
throw new TRPCError({ code: "BAD_REQUEST", message: "You do not belong to an organization" });
}
const currentOrg = await OrganizationRepository.findCurrentOrg({
const organizationRepository = getOrganizationRepository();
const currentOrg = await organizationRepository.findCurrentOrg({
userId: ctx.user.id,
orgId: organizationId,
});
@@ -1,4 +1,4 @@
import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository";
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
import type { TrpcSessionUser } from "../../../types";
@@ -13,7 +13,8 @@ export const listOtherTeamHandler = async ({ ctx: { user } }: ListOptions) => {
return [];
}
return await OrganizationRepository.findTeamsInOrgIamNotPartOf({
const organizationRepository = getOrganizationRepository();
return await organizationRepository.findTeamsInOrgIamNotPartOf({
userId: user.id,
parentId: user?.organization?.id ?? null,
});