More convenient way of getting a limit

This commit is contained in:
Matt Aitken
2024-07-04 18:41:16 +01:00
parent 1f678ff11e
commit 4beb50e62e
2 changed files with 32 additions and 9 deletions
@@ -1,11 +1,10 @@
import { Prisma, RuntimeEnvironmentType } from "@trigger.dev/database";
import { ScheduleListFilters } from "~/components/runs/v3/ScheduleFilters";
import { PrismaClient, prisma, sqlDatabaseSchema } from "~/db.server";
import { sqlDatabaseSchema } from "~/db.server";
import { displayableEnvironment } from "~/models/runtimeEnvironment.server";
import { getUsername } from "~/utils/username";
import { getCurrentPlan, getLimit, getLimits } from "~/services/platform.v3.server";
import { calculateNextScheduledTimestamp } from "~/v3/utils/calculateNextSchedule.server";
import { BasePresenter } from "./basePresenter.server";
import { getCurrentPlan } from "~/services/platform.v3.server";
type ScheduleListOptions = {
projectId: string;
@@ -245,11 +244,7 @@ export class ScheduleListPresenter extends BasePresenter {
};
});
let limit = 500;
const plan = await getCurrentPlan(project.organizationId);
if (plan?.v3Subscription.plan) {
limit = plan.v3Subscription.plan.limits.schedules.number;
}
const limit = await getLimit(project.organizationId, "schedules", 500);
return {
currentPage: page,
+29 -1
View File
@@ -1,4 +1,4 @@
import { BillingClient, SetPlanBody, UsageSeriesParams } from "@trigger.dev/billing/v3";
import { BillingClient, Limits, SetPlanBody, UsageSeriesParams } from "@trigger.dev/billing/v3";
import { Organization, Project } from "@trigger.dev/database";
import { redirect } from "remix-typedjson";
import { $replica } from "~/db.server";
@@ -59,6 +59,34 @@ export async function getCurrentPlan(orgId: string) {
}
}
export async function getLimits(orgId: string) {
const client = getClient();
if (!client) return undefined;
try {
const result = await client.currentPlan(orgId);
if (!result.success) {
logger.error("Error getting limits", { orgId, error: result.error });
return undefined;
}
return result.v3Subscription?.plan?.limits;
} catch (e) {
logger.error("Error getting limits", { orgId, error: e });
return undefined;
}
}
export async function getLimit(orgId: string, limit: keyof Limits, fallback: number) {
const limits = await getLimits(orgId);
if (!limits) return fallback;
const result = limits[limit];
if (!result) return fallback;
if (typeof result === "number") return result;
if (typeof result === "object" && "number" in result) return result.number;
return fallback;
}
export async function customerPortalUrl(orgId: string, orgSlug: string) {
const client = getClient();
if (!client) return undefined;