Files
cal.diy/packages/features/ee/organizations/repositories/OrganizationRepository.ts
T
c60addc528 feat: add PlatformOrganizationBillingTasker with sync and trigger.dev versions (#26803)
* feat: add PlatformOrganizationBillingTasker with sync and trigger.dev versions

- Create PlatformOrganizationBillingTasker extending Tasker base class
- Add sync tasker (PlatformOrganizationBillingSyncTasker) for synchronous execution
- Add trigger.dev tasker (PlatformOrganizationBillingTriggerTasker) for async execution
- Create trigger.dev task for incrementing subscription usage
- Add PlatformOrganizationBillingTaskService with business logic
- Add PlatformOrganizationBillingRepository for data access
- Add createSubscriptionUsageRecord method to StripeBillingService
- Follow BookingEmailAndSmsTasker pattern for consistency

Co-Authored-By: [email protected] <[email protected]>

* refactor: move repository methods per PR feedback

- Move findPlatformOrgByUserId to OrganizationRepository
- Create new PlatformBillingRepository for billing queries
- Remove PlatformOrganizationBillingRepository (consolidated)
- Update task service and trigger.dev task to use new repositories

Co-Authored-By: [email protected] <[email protected]>

* feat: add DI using @evyweb/ioctopus for platform billing tasker

- Create di/tasker/ directory following BookingEmailAndSmsTasker pattern
- Add tokens.ts with DI tokens for all billing tasker classes
- Add module files for PlatformBillingRepository, TaskService, SyncTasker, TriggerTasker, Tasker
- Add container files with getter functions
- Update trigger.dev task to use DI container instead of manual instantiation

Co-Authored-By: [email protected] <[email protected]>

* refactor: add select statement to PlatformBillingRepository.findByTeamId

Only select subscriptionId field since that's the only field used by the task service

Co-Authored-By: [email protected] <[email protected]>

* feat: add NestJS DI modules for platform billing tasker in API v2

- Add platform billing tasker exports to platform-libraries/organizations.ts
- Export IBillingProviderService type from platform-libraries
- Create StripeBillingProviderService wrapper implementing IBillingProviderService
- Create PrismaPlatformBillingRepository extending PlatformBillingRepository
- Create NestJS service wrappers for all platform billing tasker classes
- Create PlatformBillingTaskerModule with all providers and exports
- Update PlatformOrganizationBillingTaskService to use Pick<IBillingProviderService>

Co-Authored-By: [email protected] <[email protected]>

* feat: handle cancel and reschedule usage

* fix: restore IsUserInBillingOrg to billing module providers

Co-Authored-By: [email protected] <[email protected]>

* chore: add idempotency key

* fix: just log error]

* fix: logger and typo

* fix: address Cubic AI review feedback (high confidence issues)

- Fix grammar error: 'Delayed task are' -> 'Delayed tasks are' in PlatformOrganizationBillingSyncTasker.ts (3 occurrences)
- Re-throw error after logging in increment-usage.ts to enable trigger.dev retry mechanism

Co-Authored-By: unknown <>

* fix: remove duplicate code and use DI instead

* fix: remove orThrow on find first in findPlatformOrgByUserId

* refactor: prevent usage increment task from re-throwing errors

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-01-20 15:26:50 +09:00

553 lines
16 KiB
TypeScript

import { getOrgUsernameFromEmail } from "@calcom/features/auth/signup/utils/getOrgUsernameFromEmail";
import { getParsedTeam } from "@calcom/features/ee/teams/lib/getParsedTeam";
import { createAProfileForAnExistingUser } from "@calcom/features/profile/lib/createAProfileForAnExistingUser";
import { UserRepository } from "@calcom/features/users/repositories/UserRepository";
import logger from "@calcom/lib/logger";
import { safeStringify } from "@calcom/lib/safeStringify";
import type { PrismaClient } from "@calcom/prisma/client";
import type { CreationSource } from "@calcom/prisma/enums";
import { MembershipRole } from "@calcom/prisma/enums";
import type { teamMetadataStrictSchema } from "@calcom/prisma/zod-utils";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import type { z } from "zod";
const orgSelect = {
id: true,
name: true,
slug: true,
logoUrl: true,
};
export class OrganizationRepository {
protected readonly prismaClient: PrismaClient;
constructor(deps: { prismaClient: PrismaClient }) {
this.prismaClient = deps.prismaClient;
}
async createWithExistingUserAsOwner({
orgData,
owner,
}: {
orgData: {
name: string;
slug: string | null;
isOrganizationConfigured: boolean;
isOrganizationAdminReviewed: boolean;
autoAcceptEmail: string;
seats: number | null;
pricePerSeat: number | null;
isPlatform: boolean;
billingPeriod?: "MONTHLY" | "ANNUALLY";
logoUrl: string | null;
bio: string | null;
brandColor: string | null;
bannerUrl: string | null;
requestedSlug?: string | null;
};
owner: {
id: number;
email: string;
nonOrgUsername: string;
};
}) {
logger.debug("createWithExistingUserAsOwner", safeStringify({ orgData, owner }));
const organization = await this.create(orgData);
const ownerProfile = await createAProfileForAnExistingUser({
user: {
id: owner.id,
email: owner.email,
currentUsername: owner.nonOrgUsername,
},
organizationId: organization.id,
});
await this.prismaClient.membership.create({
data: {
createdAt: new Date(),
userId: owner.id,
role: MembershipRole.OWNER,
accepted: true,
teamId: organization.id,
},
});
return { organization, ownerProfile };
}
async createWithNonExistentOwner({
orgData,
owner,
creationSource,
}: {
orgData: {
name: string;
slug: string;
isOrganizationConfigured: boolean;
isOrganizationAdminReviewed: boolean;
autoAcceptEmail: string;
seats: number | null;
billingPeriod?: "MONTHLY" | "ANNUALLY";
pricePerSeat: number | null;
isPlatform: boolean;
logoUrl: string | null;
bio: string | null;
brandColor: string | null;
bannerUrl: string | null;
};
owner: {
email: string;
};
creationSource: CreationSource;
}) {
logger.debug("createWithNonExistentOwner", safeStringify({ orgData, owner }));
const organization = await this.create(orgData);
const ownerUsernameInOrg = getOrgUsernameFromEmail(owner.email, orgData.autoAcceptEmail);
const userRepo = new UserRepository(this.prismaClient);
const ownerInDb = await userRepo.create({
email: owner.email,
username: ownerUsernameInOrg,
organizationId: organization.id,
locked: false,
creationSource,
});
await this.prismaClient.membership.create({
data: {
createdAt: new Date(),
userId: ownerInDb.id,
role: MembershipRole.OWNER,
accepted: true,
teamId: organization.id,
},
});
return {
orgOwner: ownerInDb,
organization,
ownerProfile: {
username: ownerUsernameInOrg,
},
};
}
async create(orgData: {
name: string;
slug: string | null;
isOrganizationConfigured: boolean;
isOrganizationAdminReviewed: boolean;
autoAcceptEmail: string;
seats: number | null;
billingPeriod?: "MONTHLY" | "ANNUALLY";
pricePerSeat: number | null;
isPlatform: boolean;
logoUrl: string | null;
bio: string | null;
brandColor: string | null;
bannerUrl: string | null;
requestedSlug?: string | null;
}) {
return await this.prismaClient.team.create({
data: {
name: orgData.name,
isOrganization: true,
slug: orgData.slug,
logoUrl: orgData.logoUrl,
bio: orgData.bio,
brandColor: orgData.brandColor,
bannerUrl: orgData.bannerUrl,
organizationSettings: {
create: {
isAdminReviewed: orgData.isOrganizationAdminReviewed,
isOrganizationVerified: true,
isOrganizationConfigured: orgData.isOrganizationConfigured,
orgAutoAcceptEmail: orgData.autoAcceptEmail,
},
},
metadata: {
isPlatform: orgData.isPlatform,
// We still have a case where we don't have slug set directly on organization. The reason is because we want to create an org first(with same slug as another regular team) and then move the team to org.
// Because in such cases there is a fallback existing at multiple places to use requestedSlug, we set it here still :(
requestedSlug: orgData.requestedSlug,
// We set these here still because some parts of code read it from metadata. This data exists in OrganizationOnboarding as well and should be used from there ideally
// We also need to think about existing Organizations that might not have OrganizationOnboarding, before taking any decision
orgSeats: orgData.seats,
orgPricePerSeat: orgData.pricePerSeat,
billingPeriod: orgData.billingPeriod,
},
isPlatform: orgData.isPlatform,
},
});
}
async findById({ id }: { id: number }) {
return this.prismaClient.team.findUnique({
where: {
id,
isOrganization: true,
},
});
}
async findBySlug({ slug }: { slug: string }) {
// Slug is unique but could be null as well, so we can't use findUnique
return this.prismaClient.team.findFirst({
where: {
slug,
isOrganization: true,
},
});
}
async findBySlugIncludeOnboarding({ slug }: { slug: string }) {
return this.prismaClient.team.findFirst({
where: { slug, isOrganization: true },
include: {
organizationOnboarding: {
select: {
id: true,
isDomainConfigured: true,
isPlatform: true,
slug: true,
teams: true,
invitedMembers: true,
},
},
},
});
}
async findByIdIncludeOrganizationSettings({ id }: { id: number }) {
return this.prismaClient.team.findUnique({
where: {
id,
isOrganization: true,
},
select: {
...orgSelect,
metadata: true,
organizationSettings: true,
},
});
}
async findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail({ email }: { email: string }) {
const emailDomain = email.split("@").at(-1);
const orgs = await this.prismaClient.team.findMany({
where: {
isOrganization: true,
isPlatform: false,
organizationSettings: {
orgAutoAcceptEmail: emailDomain,
isOrganizationVerified: true,
isAdminReviewed: true,
orgAutoJoinOnSignup: true,
},
},
});
if (orgs.length > 1) {
logger.error(
"Multiple organizations found with the same auto accept email domain",
safeStringify({ orgIds: orgs.map((org) => org.id), emailDomain })
);
// If we cannot reliably confirm a unique org then return nothing
return null;
}
const org = orgs[0];
if (!org) {
return null;
}
return getParsedTeam(org);
}
async findCurrentOrg({ userId, orgId }: { userId: number; orgId: number }) {
const membership = await this.prismaClient.membership.findUnique({
where: {
userId_teamId: {
userId,
teamId: orgId,
},
},
include: {
team: true,
},
});
const organizationSettings = await this.prismaClient.organizationSettings.findUnique({
where: {
organizationId: orgId,
},
select: {
lockEventTypeCreationForUsers: true,
adminGetsNoSlotsNotification: true,
isAdminReviewed: true,
allowSEOIndexing: true,
orgProfileRedirectsToVerifiedDomain: true,
orgAutoAcceptEmail: true,
disablePhoneOnlySMSNotifications: true,
disableAutofillOnBookingPage: true,
orgAutoJoinOnSignup: true,
disableAttendeeConfirmationEmail: true,
disableAttendeeCancellationEmail: true,
disableAttendeeRescheduledEmail: true,
disableAttendeeRequestEmail: true,
disableAttendeeReassignedEmail: true,
disableAttendeeAwaitingPaymentEmail: true,
disableAttendeeRescheduleRequestEmail: true,
disableAttendeeLocationChangeEmail: true,
disableAttendeeNewEventEmail: true,
},
});
if (!membership) {
throw new Error("You do not have a membership to your organization");
}
const metadata = teamMetadataSchema.parse(membership?.team.metadata);
return {
canAdminImpersonate: !!organizationSettings?.isAdminReviewed,
organizationSettings: {
lockEventTypeCreationForUsers: organizationSettings?.lockEventTypeCreationForUsers,
adminGetsNoSlotsNotification: organizationSettings?.adminGetsNoSlotsNotification,
allowSEOIndexing: organizationSettings?.allowSEOIndexing,
orgProfileRedirectsToVerifiedDomain: organizationSettings?.orgProfileRedirectsToVerifiedDomain,
orgAutoAcceptEmail: organizationSettings?.orgAutoAcceptEmail,
disablePhoneOnlySMSNotifications: organizationSettings?.disablePhoneOnlySMSNotifications,
disableAutofillOnBookingPage: organizationSettings?.disableAutofillOnBookingPage,
orgAutoJoinOnSignup: organizationSettings?.orgAutoJoinOnSignup,
disableAttendeeConfirmationEmail: organizationSettings?.disableAttendeeConfirmationEmail,
disableAttendeeCancellationEmail: organizationSettings?.disableAttendeeCancellationEmail,
disableAttendeeRescheduledEmail: organizationSettings?.disableAttendeeRescheduledEmail,
disableAttendeeRequestEmail: organizationSettings?.disableAttendeeRequestEmail,
disableAttendeeReassignedEmail: organizationSettings?.disableAttendeeReassignedEmail,
disableAttendeeAwaitingPaymentEmail: organizationSettings?.disableAttendeeAwaitingPaymentEmail,
disableAttendeeRescheduleRequestEmail: organizationSettings?.disableAttendeeRescheduleRequestEmail,
disableAttendeeLocationChangeEmail: organizationSettings?.disableAttendeeLocationChangeEmail,
disableAttendeeNewEventEmail: organizationSettings?.disableAttendeeNewEventEmail,
},
user: {
role: membership?.role,
accepted: membership?.accepted,
},
...membership?.team,
metadata: {
requestedSlug: metadata?.requestedSlug ?? null,
},
};
}
async findTeamsInOrgIamNotPartOf({ userId, parentId }: { userId: number; parentId: number | null }) {
const teamsInOrgIamNotPartOf = await this.prismaClient.team.findMany({
where: {
parentId,
members: {
none: {
userId,
},
},
},
select: {
parentId: true,
id: true,
name: true,
logoUrl: true,
slug: true,
},
});
return teamsInOrgIamNotPartOf;
}
async adminFindById({ id }: { id: number }) {
const org = await this.prismaClient.team.findUnique({
where: {
id,
},
select: {
id: true,
name: true,
slug: true,
metadata: true,
isOrganization: true,
members: {
where: {
role: "OWNER",
},
select: {
user: {
select: {
id: true,
name: true,
email: true,
},
},
},
},
organizationSettings: {
select: {
isOrganizationConfigured: true,
isOrganizationVerified: true,
orgAutoAcceptEmail: true,
},
},
},
});
if (!org) {
throw new Error("Organization not found");
}
const parsedMetadata = teamMetadataSchema.parse(org.metadata);
if (!org?.isOrganization) {
throw new Error("Organization not found");
}
return { ...org, metadata: parsedMetadata };
}
async findByMemberEmail({ email }: { email: string }) {
const organization = await this.prismaClient.team.findFirst({
where: {
isOrganization: true,
members: {
some: {
user: { email },
},
},
},
});
return organization ?? null;
}
async findByMemberEmailId({ email }: { email: string }) {
const log = logger.getSubLogger({ prefix: ["findByMemberEmailId"] });
log.debug("called with", { email });
const organization = await this.prismaClient.team.findFirst({
where: {
isOrganization: true,
members: {
some: {
user: {
email,
},
},
},
},
});
return organization;
}
async findCalVideoLogoByOrgId({ id }: { id: number }) {
const org = await this.prismaClient.team.findUnique({
where: {
id,
},
select: {
calVideoLogo: true,
},
});
return org?.calVideoLogo;
}
async getVerifiedOrganizationByAutoAcceptEmailDomain(domain: string) {
return await this.prismaClient.team.findFirst({
where: {
organizationSettings: {
isOrganizationVerified: true,
orgAutoAcceptEmail: domain,
},
},
select: {
id: true,
organizationSettings: {
select: {
orgAutoAcceptEmail: true,
},
},
},
});
}
async setSlug({ id, slug }: { id: number; slug: string }) {
return await this.prismaClient.team.update({
where: { id, isOrganization: true },
data: { slug },
});
}
async updateStripeSubscriptionDetails({
id,
stripeSubscriptionId,
stripeSubscriptionItemId,
existingMetadata,
}: {
id: number;
stripeSubscriptionId: string;
stripeSubscriptionItemId: string;
existingMetadata: z.infer<typeof teamMetadataStrictSchema>;
}) {
return await this.prismaClient.team.update({
where: { id, isOrganization: true },
data: {
metadata: {
...existingMetadata,
subscriptionId: stripeSubscriptionId,
subscriptionItemId: stripeSubscriptionItemId,
},
},
});
}
async checkIfPrivate({ orgId }: { orgId: number }) {
const team = await this.prismaClient.team.findUnique({
where: {
id: orgId,
isOrganization: true,
},
select: {
isPrivate: true,
},
});
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;
}
async findPlatformOrgByUserId(userId: number) {
return this.prismaClient.team.findFirst({
where: {
orgProfiles: {
some: {
userId: userId,
},
},
isPlatform: true,
isOrganization: true,
},
select: {
id: true,
isPlatform: true,
isOrganization: true,
},
});
}
}