38 lines
949 B
TypeScript
38 lines
949 B
TypeScript
import { and, eq } from "drizzle-orm";
|
|
import type { NextFunction, Request, Response } from "express";
|
|
import { db } from "../db/db.controller.js";
|
|
import { settings } from "../db/schema/settings.schema.js";
|
|
import { tryCatch } from "../utils/trycatch.utils.js";
|
|
|
|
/**
|
|
*
|
|
* @param moduleName name of the module we are checking if is enabled or not.
|
|
*/
|
|
export const featureCheck = (moduleName: string) => {
|
|
// get the features from the settings
|
|
|
|
return async (_req: Request, res: Response, next: NextFunction) => {
|
|
const { data: sData, error: sError } = await tryCatch(
|
|
db
|
|
.select()
|
|
.from(settings)
|
|
.where(
|
|
and(
|
|
eq(settings.settingType, "feature"),
|
|
eq(settings.name, moduleName),
|
|
),
|
|
),
|
|
);
|
|
|
|
if (sError) {
|
|
return res.status(403).json({ error: "Internal Error" });
|
|
}
|
|
|
|
if (!sData?.length || !sData[0]?.active) {
|
|
return res.status(403).json({ error: "Feature disabled" });
|
|
}
|
|
|
|
next();
|
|
};
|
|
};
|