fix: billing (#5483)

This commit is contained in:
Dhruwang Jariwala
2025-04-23 15:24:08 +05:30
committed by GitHub
parent 27da540846
commit e1bbb0a10f
5 changed files with 235 additions and 29 deletions
@@ -1,5 +1,6 @@
import { cache } from "@/lib/cache";
import { organizationCache } from "@/lib/organization/cache";
import { getBillingPeriodStartDate } from "@/lib/utils/billing";
import { ApiErrorResponseV2 } from "@/modules/api/v2/types/api-error";
import { Organization } from "@prisma/client";
import { cache as reactCache } from "react";
@@ -133,22 +134,7 @@ export const getMonthlyOrganizationResponseCount = reactCache(async (organizatio
}
// Determine the start date based on the plan type
let startDate: Date;
if (billing.data.plan === "free") {
// For free plans, use the first day of the current calendar month
const now = new Date();
startDate = new Date(now.getFullYear(), now.getMonth(), 1);
} else {
// For other plans, use the periodStart from billing
if (!billing.data.periodStart) {
return err({
type: "internal_server_error",
details: [{ field: "organization", issue: "billing period start is not set" }],
});
}
startDate = billing.data.periodStart;
}
const startDate = getBillingPeriodStartDate(billing.data);
// Get all environment IDs for the organization
const environmentIdsResult = await getAllEnvironmentsFromOrganizationId(organizationId);