mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-10 20:02:26 +00:00
48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
import { Request, Response, NextFunction } from "express";
|
|
import { db, orgs } from "@server/db";
|
|
import { userOrgs } from "@server/db";
|
|
import { and, eq } from "drizzle-orm";
|
|
import createHttpError from "http-errors";
|
|
import HttpCode from "@server/types/HttpCode";
|
|
import { checkOrgAccessPolicy } from "#dynamic/lib/checkOrgAccessPolicy";
|
|
import { usageService } from "@server/lib/billing/usageService";
|
|
import { build } from "@server/build";
|
|
|
|
export async function verifyLimits(
|
|
req: Request,
|
|
res: Response,
|
|
next: NextFunction
|
|
) {
|
|
if (build != "saas") {
|
|
return next();
|
|
}
|
|
|
|
const orgId = req.userOrgId || req.apiKeyOrg?.orgId || req.params.orgId;
|
|
|
|
if (!orgId) {
|
|
return next(); // its fine if we silently fail here because this is not critical to operation or security and its better user experience if we dont fail
|
|
}
|
|
|
|
try {
|
|
const reject = await usageService.checkLimitSet(orgId);
|
|
|
|
if (reject) {
|
|
return next(
|
|
createHttpError(
|
|
HttpCode.PAYMENT_REQUIRED,
|
|
"Organization has exceeded its usage limits. Please upgrade your plan or contact support."
|
|
)
|
|
);
|
|
}
|
|
|
|
return next();
|
|
} catch (e) {
|
|
return next(
|
|
createHttpError(
|
|
HttpCode.INTERNAL_SERVER_ERROR,
|
|
"Error checking limits"
|
|
)
|
|
);
|
|
}
|
|
}
|