mirror of
https://git.sr.ht/~roxwize/mipilin
synced 2025-01-31 02:53:36 +00:00
60 lines
1.9 KiB
TypeScript
60 lines
1.9 KiB
TypeScript
import { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
import { Express } from "express";
|
|
import { createInviteCode, render } from "./util.js";
|
|
import { inviteCodes, users } from "../db/schema.js";
|
|
import { desc, eq } from "drizzle-orm";
|
|
import dayjs from "dayjs";
|
|
|
|
export default function (app: Express, db: NodePgDatabase) {
|
|
app.get("/mod", async (req, res) => {
|
|
if (!req.session["loggedIn"] || !req.session["moderator"]) {
|
|
res.redirect("/");
|
|
return;
|
|
}
|
|
|
|
const now = dayjs();
|
|
const codes = (
|
|
await db
|
|
.select({ expires: inviteCodes.expires, token: inviteCodes.token, uname: users.name })
|
|
.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))
|
|
};
|
|
});
|
|
render(db, "admin", "Admin Panel", res, req, { codes });
|
|
});
|
|
|
|
app.post("/mod/codes/delete", async (req, res) => {
|
|
if (!req.session["loggedIn"] || !req.session["moderator"]) {
|
|
res.redirect("/");
|
|
return;
|
|
}
|
|
|
|
await db.delete(inviteCodes).where(eq(inviteCodes.token, req.body.token));
|
|
req.flash("success", "Deleted.");
|
|
res.redirect("/mod");
|
|
})
|
|
app.post("/mod/codes/create", async (req, res) => {
|
|
if (!req.session["loggedIn"] || !req.session["moderator"]) {
|
|
res.redirect("/");
|
|
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");
|
|
});
|
|
}
|