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

245 lines
5.5 KiB
TypeScript

import type { PrismaClient } from "@calcom/prisma/client";
import { BookingStatus } from "@calcom/prisma/enums";
const BOOKING_DETAIL_LIMIT = 100;
export class ActiveUserBillingRepository {
constructor(private readonly prismaClient: PrismaClient) {}
/**
* Get all managed user emails for a platform org identified by its PlatformBilling subscription ID.
*/
async getManagedUserEmailsBySubscriptionId(subscriptionId: string): Promise<{ email: string }[]> {
return this.prismaClient.user.findMany({
distinct: ["email"],
where: {
isPlatformManaged: true,
profiles: {
some: {
organization: {
platformBilling: {
subscriptionId,
},
},
},
},
},
select: {
email: true,
},
});
}
/**
* Get all member emails for a regular organization identified by its org (team) ID.
* Uses the Membership table to find accepted org members.
*/
async getOrgMemberEmailsByOrgId(orgId: number): Promise<{ email: string }[]> {
return this.prismaClient.user.findMany({
distinct: ["email"],
where: {
teams: {
some: {
teamId: orgId,
accepted: true,
},
},
},
select: {
email: true,
},
});
}
async getOrgMemberDetailsByOrgId(
orgId: number
): Promise<{ id: number; email: string; name: string | null }[]> {
return this.prismaClient.user.findMany({
distinct: ["email"],
where: {
teams: {
some: {
teamId: orgId,
accepted: true,
},
},
},
select: {
id: true,
email: true,
name: true,
},
});
}
/**
* Find platform-managed users (by email) who hosted at least one booking in the given period.
* Filters on isPlatformManaged to match only platform org users.
*/
async getActivePlatformUsersAsHost(
subscriptionId: string,
periodStart: Date,
periodEnd: Date
): Promise<{ email: string }[]> {
return this.prismaClient.user.findMany({
distinct: ["email"],
where: {
isPlatformManaged: true,
profiles: {
some: {
organization: {
platformBilling: {
subscriptionId,
},
},
},
},
bookings: {
some: {
userId: { not: null },
status: BookingStatus.ACCEPTED,
startTime: {
gte: periodStart,
lt: periodEnd,
},
},
},
},
select: {
email: true,
},
});
}
/**
* Find users (by email) who hosted at least one booking in the given period.
*/
async getActiveUsersAsHost(
userEmails: string[],
periodStart: Date,
periodEnd: Date
): Promise<{ email: string }[]> {
return this.prismaClient.user.findMany({
distinct: ["email"],
where: {
email: { in: userEmails },
bookings: {
some: {
userId: { not: null },
status: BookingStatus.ACCEPTED,
startTime: {
gte: periodStart,
lt: periodEnd,
},
},
},
},
select: {
email: true,
},
});
}
async getBookingsByHostUserId(
userId: number,
periodStart: Date,
periodEnd: Date
): Promise<
Array<{
id: number;
uid: string;
title: string;
startTime: Date;
endTime: Date;
attendees: Array<{ email: string; name: string | null }>;
}>
> {
return this.prismaClient.booking.findMany({
where: {
userId,
status: BookingStatus.ACCEPTED,
startTime: { gte: periodStart, lt: periodEnd },
},
orderBy: { startTime: "desc" },
take: BOOKING_DETAIL_LIMIT,
select: {
id: true,
uid: true,
title: true,
startTime: true,
endTime: true,
attendees: {
select: {
email: true,
name: true,
},
},
},
});
}
async getBookingsByAttendeeEmail(
email: string,
periodStart: Date,
periodEnd: Date
): Promise<
Array<{
id: number;
uid: string;
title: string;
startTime: Date;
endTime: Date;
user: { name: string | null; email: string } | null;
}>
> {
return this.prismaClient.booking.findMany({
where: {
attendees: { some: { email } },
status: BookingStatus.ACCEPTED,
startTime: { gte: periodStart, lt: periodEnd },
},
orderBy: { startTime: "desc" },
take: BOOKING_DETAIL_LIMIT,
select: {
id: true,
uid: true,
title: true,
startTime: true,
endTime: true,
user: {
select: {
name: true,
email: true,
},
},
},
});
}
/**
* Find users (by email) who attended at least one booking in the given period.
*/
async getActiveUsersAsAttendee(
userEmails: string[],
periodStart: Date,
periodEnd: Date
): Promise<{ email: string }[]> {
return this.prismaClient.attendee.findMany({
distinct: ["email"],
where: {
email: { in: userEmails },
booking: {
status: BookingStatus.ACCEPTED,
startTime: {
gte: periodStart,
lt: periodEnd,
},
},
},
select: {
email: true,
},
});
}
}