···11+import { db, eq } from "@openstatus/db";
22+import { selectWorkspaceSchema, workspace } from "@openstatus/db/src/schema";
33+import { allPlans } from "@openstatus/plans";
44+55+/**
66+ * TODO: move the plan limit into the Unkey `{ meta }` to avoid an additional db call.
77+ * When an API Key is created, we need to include the `{ meta: { plan: "free" } }` to the key.
88+ * Then, we can just read the plan from the key and use it in the middleware.
99+ * Don't forget to update the key whenever a user changes their plan. (via `stripeRoute` webhook)
1010+ *
1111+ * That remindes me we need to downgrade the frequency/periodicity of monitors to 10m if the user downgrades their plan.
1212+ */
1313+1414+export async function getWorkspace(id: number) {
1515+ const _workspace = await db
1616+ .select()
1717+ .from(workspace)
1818+ .where(eq(workspace.id, id))
1919+ .get();
2020+2121+ return selectWorkspaceSchema.parse(_workspace);
2222+}
2323+2424+export async function getLimitByWorkspaceId(id: number) {
2525+ const { plan } = await getWorkspace(id);
2626+ return allPlans[plan];
2727+}
···2233import type { periodicityEnum } from "@openstatus/db/src/schema";
4455-type Plan = {
55+export type Plan = {
66 limits: {
77 monitors: number;
88 "status-pages": number;