mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-03 17:44:44 +02:00
add supporer key program
This commit is contained in:
parent
1c2ba4076a
commit
cdc415079c
17 changed files with 908 additions and 74 deletions
54
server/routers/supporterKey/isSupporterKeyVisible.ts
Normal file
54
server/routers/supporterKey/isSupporterKeyVisible.ts
Normal file
|
@ -0,0 +1,54 @@
|
|||
import { Request, Response, NextFunction } from "express";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import createHttpError from "http-errors";
|
||||
import logger from "@server/logger";
|
||||
import { response as sendResponse } from "@server/lib";
|
||||
import config from "@server/lib/config";
|
||||
import db from "@server/db";
|
||||
import { count } from "drizzle-orm";
|
||||
import { users } from "@server/db/schema";
|
||||
|
||||
export type IsSupporterKeyVisibleResponse = {
|
||||
visible: boolean;
|
||||
};
|
||||
|
||||
const USER_LIMIT = 5;
|
||||
|
||||
export async function isSupporterKeyVisible(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<any> {
|
||||
try {
|
||||
const hidden = config.isSupporterKeyHidden();
|
||||
const key = config.getSupporterData();
|
||||
|
||||
let visible = !hidden && key?.valid !== true;
|
||||
|
||||
if (key?.tier === "Limited Supporter") {
|
||||
const [numUsers] = await db.select({ count: count() }).from(users);
|
||||
|
||||
if (numUsers.count > USER_LIMIT) {
|
||||
visible = true;
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug(`Supporter key visible: ${visible}`);
|
||||
logger.debug(JSON.stringify(key));
|
||||
|
||||
return sendResponse<IsSupporterKeyVisibleResponse>(res, {
|
||||
data: {
|
||||
visible
|
||||
},
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Status",
|
||||
status: HttpCode.OK
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
return next(
|
||||
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue