mirror of
https://github.com/fosrl/pangolin.git
synced 2026-05-29 21:13:32 +00:00
18d380ce30
Signed-off-by: Marc Schäfer <git@marcschaeferger.de>
48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import { Request, Response, NextFunction } from "express";
|
|
import createHttpError from "http-errors";
|
|
import HttpCode from "@server/types/HttpCode";
|
|
import { usageService } from "@server/lib/billing/usageService";
|
|
import { build } from "@server/build";
|
|
import { getFirstString } from "@server/lib/requestParams";
|
|
|
|
export async function verifyLimits(
|
|
req: Request,
|
|
res: Response,
|
|
next: NextFunction
|
|
) {
|
|
if (build != "saas") {
|
|
return next();
|
|
}
|
|
|
|
const orgId =
|
|
req.userOrgId ||
|
|
req.apiKeyOrg?.orgId ||
|
|
getFirstString(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"
|
|
)
|
|
);
|
|
}
|
|
}
|