2024-12-11 03:27:43 +00:00
|
|
|
import { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
|
|
import { Express } from "express";
|
2024-12-25 23:44:29 +00:00
|
|
|
import { createInviteCode, render, UserStatus } from "./util.js";
|
2024-12-11 03:27:43 +00:00
|
|
|
import { inviteCodes, users } from "../db/schema.js";
|
2024-12-25 23:44:29 +00:00
|
|
|
import { and, count, desc, eq, sql } from "drizzle-orm";
|
2024-12-11 03:27:43 +00:00
|
|
|
import dayjs from "dayjs";
|
|
|
|
|
2024-12-25 23:44:29 +00:00
|
|
|
const USER_REFERRAL_EXPIRATION = 7 * 24 * 60 * 60 * 1000
|
|
|
|
|
2024-12-11 03:27:43 +00:00
|
|
|
export default function (app: Express, db: NodePgDatabase) {
|
|
|
|
app.get("/mod", async (req, res) => {
|
2024-12-25 23:44:29 +00:00
|
|
|
if (
|
|
|
|
!req.session["loggedIn"] ||
|
|
|
|
!(req.session["status"] & UserStatus.MODERATOR)
|
|
|
|
) {
|
2024-12-11 03:27:43 +00:00
|
|
|
res.redirect("/");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const now = dayjs();
|
|
|
|
const codes = (
|
|
|
|
await db
|
2024-12-25 23:44:29 +00:00
|
|
|
.select({
|
|
|
|
expires: inviteCodes.expires,
|
|
|
|
token: inviteCodes.token,
|
|
|
|
uname: users.name
|
|
|
|
})
|
2024-12-11 03:27:43 +00:00
|
|
|
.from(inviteCodes)
|
|
|
|
.leftJoin(users, eq(inviteCodes.user, users.id))
|
|
|
|
.orderBy(desc(inviteCodes.granted))
|
|
|
|
).map((e) => {
|
|
|
|
return {
|
|
|
|
expires: e.expires,
|
|
|
|
token: e.token,
|
|
|
|
uname: e.uname,
|
|
|
|
expiresString: now.to(dayjs(e.expires))
|
|
|
|
};
|
|
|
|
});
|
2024-12-26 21:24:04 +00:00
|
|
|
render(db, "admin", "admin panel", res, req, { codes });
|
2024-12-11 03:27:43 +00:00
|
|
|
});
|
|
|
|
|
2024-12-25 23:44:29 +00:00
|
|
|
app.post("/codes/delete", async (req, res) => {
|
|
|
|
if (
|
|
|
|
!req.session["loggedIn"] ||
|
|
|
|
!(req.session["status"] & UserStatus.MODERATOR)
|
|
|
|
) {
|
2024-12-11 03:27:43 +00:00
|
|
|
res.redirect("/");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await db.delete(inviteCodes).where(eq(inviteCodes.token, req.body.token));
|
|
|
|
req.flash("success", "Deleted.");
|
|
|
|
res.redirect("/mod");
|
2024-12-25 23:44:29 +00:00
|
|
|
});
|
|
|
|
app.post("/codes/create", async (req, res) => {
|
|
|
|
if (
|
|
|
|
!req.session["loggedIn"]
|
|
|
|
) {
|
|
|
|
res.redirect("/login");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!(req.session["status"] & UserStatus.MODERATOR)) {
|
|
|
|
const { codesUsed } = (
|
|
|
|
await db
|
|
|
|
.select({ codesUsed: count() })
|
|
|
|
.from(inviteCodes)
|
|
|
|
.where(
|
|
|
|
and(
|
|
|
|
eq(inviteCodes.user, req.session["uid"]),
|
|
|
|
eq(
|
|
|
|
sql`extract(month from granted)`,
|
|
|
|
sql`extract(month from current_date)`
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)[0];
|
|
|
|
if (codesUsed >= 5) {
|
|
|
|
req.flash("error", "You've generated the maximum of five codes this week. Your counter will reset next month.");
|
|
|
|
res.redirect("/dashboard");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const code = await createInviteCode(db, req.session["uid"], new Date(Date.now() + USER_REFERRAL_EXPIRATION));
|
|
|
|
req.flash("success", `Your code has been created as <b>${code}</b>. It expires in a week so use it ASAP!!!`);
|
|
|
|
res.redirect("/dashboard");
|
2024-12-11 03:27:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const expiration = new Date(req.body.expiration || 0);
|
|
|
|
if (req.body.expiration && expiration.getTime() <= Date.now()) {
|
|
|
|
req.flash("error", "Chosen expiration date is in the past.");
|
|
|
|
res.redirect("/mod");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const code = await createInviteCode(db, req.session["uid"], expiration);
|
|
|
|
|
|
|
|
req.flash("success", `Your code has been created as <b>${code}</b>.`);
|
|
|
|
res.redirect("/mod");
|
|
|
|
});
|
|
|
|
}
|