Files
d52e2d9fad feat: active user billing (#27867)
* factory and statergie

* chore: use correct method of DI

* feat: add onchagne

* add logic to HWM stat

* add webhook resolver methods to each statergy

* move seat tracking + webhooks over to own statergy

* Move to factory base approach

* move logic to correct class

* rename create -> createByTeamId

* fix: remove debug `true ||` overrides from IS_STRIPE_ENABLED and IS_TEAM_BILLING_ENABLED

Remove accidentally committed debug overrides that short-circuited
IS_STRIPE_ENABLED and IS_TEAM_BILLING_ENABLED to always be true,
bypassing Stripe credential checks. This would break self-hosted
instances without Stripe configured.

Identified by cubic (https://cubic.dev)

Co-Authored-By: unknown <>

* feat: active user billing

* add tests

* UI side for users on active billing

* use correct period of stripe sub

* feat: claude feedback

* fix: skip Stripe sync for canceled/expired subscriptions to prevent repeated API calls

Co-Authored-By: unknown <>

* feat: feedback

* feat: only render when active users mode is set

* fix type error

* fix: constants + feature flags

* fix: default to null in tests

* chore: use seats in test

* fix: use node:crypto protocol for Node.js builtin imports

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

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-02-12 13:57:32 +00:00

101 lines
2.6 KiB
TypeScript

import type { PrismaClient } from "@calcom/prisma";
import { prisma as defaultPrisma } from "@calcom/prisma";
import type { BillingPeriod } from "@calcom/prisma/enums";
export interface TeamWithBillingInfo {
isOrganization: boolean;
billing: {
id: string;
billingPeriod: BillingPeriod | null;
subscriptionStart: Date | null;
subscriptionEnd: Date | null;
subscriptionTrialEnd: Date | null;
pricePerSeat: number | null;
paidSeats: number | null;
subscriptionId: string;
} | null;
}
export interface UpdateBillingPeriodData {
billingPeriod: BillingPeriod;
pricePerSeat?: number | null;
paidSeats?: number | null;
subscriptionStart?: Date | null;
subscriptionEnd?: Date | null;
}
export class BillingPeriodRepository {
private prisma: PrismaClient;
constructor(prisma?: PrismaClient) {
this.prisma = prisma || defaultPrisma;
}
async getTeamWithBillingInfo(teamId: number): Promise<TeamWithBillingInfo | null> {
const team = await this.prisma.team.findUnique({
where: { id: teamId },
select: {
isOrganization: true,
teamBilling: {
select: {
id: true,
billingPeriod: true,
subscriptionStart: true,
subscriptionEnd: true,
subscriptionTrialEnd: true,
pricePerSeat: true,
paidSeats: true,
subscriptionId: true,
},
},
organizationBilling: {
select: {
id: true,
billingPeriod: true,
subscriptionStart: true,
subscriptionEnd: true,
subscriptionTrialEnd: true,
pricePerSeat: true,
paidSeats: true,
subscriptionId: true,
},
},
},
});
if (!team) return null;
const billing = team.isOrganization ? team.organizationBilling : team.teamBilling;
return {
isOrganization: team.isOrganization,
billing,
};
}
async updateOrganizationBillingPeriod(billingId: string, data: UpdateBillingPeriodData): Promise<void> {
await this.prisma.organizationBilling.update({
where: { id: billingId },
data,
});
}
async updateTeamBillingPeriod(billingId: string, data: UpdateBillingPeriodData): Promise<void> {
await this.prisma.teamBilling.update({
where: { id: billingId },
data,
});
}
async getTeamForBillingUpdate(teamId: number) {
return await this.prisma.team.findUnique({
where: { id: teamId },
select: {
isOrganization: true,
teamBilling: { select: { id: true } },
organizationBilling: { select: { id: true } },
},
});
}
}