fix: adjust free monthly credits for orgs (#23798)

* add org monthly credits env variable

* use fallback for env variable

* remove comment

---------

Co-authored-by: CarinaWolli <[email protected]>
This commit is contained in:
Carina WollendorferandCarinaWolli authored and GitHub committed 2025-09-12 17:47:13 +00:00
1 parent a62c74f79e
commit 2be388373f
4 files changed
+38 -20

No files matched your search

@@ -435,8 +435,8 @@ describe("CreditService", () => {
expect(result).toBe(1500); // (3 members * 1000 price) / 2
});
it("should calculate credits with 20% multiplier for organizations", async () => {
vi.stubEnv("STRIPE_ORG_MONTHLY_PRICE_ID", "price_org_monthly");
it("should calculate credits for organizations using ORG_MONTHLY_CREDITS", async () => {
vi.stubEnv("ORG_MONTHLY_CREDITS", "1500");
const mockTeamRepo = {
findTeamWithMembers: vi.fn().mockResolvedValue({
id: 1,
@@ -453,15 +453,31 @@ describe("CreditService", () => {
mockTeamBillingService.getSubscriptionStatus
);
const mockStripeBillingService = {
getPrice: vi.fn().mockResolvedValue({ unit_amount: 3700 }),
const result = await creditService.getMonthlyCredits(1);
expect(result).toBe(3000); // 2 members * 1500 credits per seat
});
it("should calculate credits for organizations with default 1000 credits per seat", async () => {
// Clear ORG_MONTHLY_CREDITS to test default behavior
vi.stubEnv("ORG_MONTHLY_CREDITS", undefined);
const mockTeamRepo = {
findTeamWithMembers: vi.fn().mockResolvedValue({
id: 1,
isOrganization: true,
members: [{ accepted: true }, { accepted: true }, { accepted: true }],
}),
};
vi.spyOn(StripeBillingService.prototype, "getPrice").mockImplementation(
mockStripeBillingService.getPrice
vi.mocked(TeamRepository).mockImplementation(() => mockTeamRepo as any);
const mockTeamBillingService = {
getSubscriptionStatus: vi.fn().mockResolvedValue("active"),
};
vi.spyOn(InternalTeamBilling.prototype, "getSubscriptionStatus").mockImplementation(
mockTeamBillingService.getSubscriptionStatus
);
const result = await creditService.getMonthlyCredits(1);
expect(result).toBe(1480); // (2 members * 3700 price) * 0.2
expect(result).toBe(3000); // 3 members * 1000 credits per seat (default)
});
});
+11 -13
View File
@@ -588,8 +588,6 @@ export class CreditService {
if (!team) return 0;
let totalMonthlyCredits = 0;
const teamBillingService = new InternalTeamBilling(team);
const subscriptionStatus = await teamBillingService.getSubscriptionStatus();
@@ -599,25 +597,25 @@ export class CreditService {
const activeMembers = team.members.filter((member) => member.accepted).length;
const billingService = new StripeBillingService();
if (team.isOrganization) {
const orgMonthlyCredits = process.env.ORG_MONTHLY_CREDITS;
const creditsPerSeat = orgMonthlyCredits ? parseInt(orgMonthlyCredits) : 1000;
return activeMembers * creditsPerSeat;
}
const priceId = team.isOrganization
? process.env.STRIPE_ORG_MONTHLY_PRICE_ID
: process.env.STRIPE_TEAM_MONTHLY_PRICE_ID;
const billingService = new StripeBillingService();
const priceId = process.env.STRIPE_TEAM_MONTHLY_PRICE_ID;
if (!priceId) {
log.warn("Monthly price ID not configured", { teamId, isOrganization: team.isOrganization });
log.warn("Monthly price ID not configured", { teamId });
return 0;
}
const monthlyPrice = await billingService.getPrice(priceId || "");
const monthlyPrice = await billingService.getPrice(priceId);
const pricePerSeat = monthlyPrice.unit_amount ?? 0;
const creditsPerSeat = pricePerSeat * 0.5;
// Teams get 50% of the price as credits, organizations get 20%
const creditMultiplier = team.isOrganization ? 0.2 : 0.5;
totalMonthlyCredits = activeMembers * pricePerSeat * creditMultiplier;
return totalMonthlyCredits;
return activeMembers * creditsPerSeat;
}
calculateCreditsFromPrice(price: number) {