124 lines
3.2 KiB
TypeScript
124 lines
3.2 KiB
TypeScript
import { sql } from "drizzle-orm";
|
|
import { withAdmin } from "@/lib/api-handler";
|
|
import { apiError, apiOk } from "@/lib/api-response";
|
|
import { db } from "@/lib/db";
|
|
import { PERMS } from "@/lib/permissions";
|
|
|
|
interface UserPrefix {
|
|
id: number;
|
|
user_id: number;
|
|
text: string;
|
|
color: string;
|
|
icon: string;
|
|
effect: string;
|
|
active: number;
|
|
username?: string | null;
|
|
}
|
|
|
|
export const GET = withAdmin(
|
|
{ permission: PERMS.PREFIXES_VIEW },
|
|
async (request) => {
|
|
const url = request.nextUrl;
|
|
const q = url.searchParams.get("q") || "";
|
|
const page = Math.max(1, Number(url.searchParams.get("page") || 1));
|
|
const limit = 50;
|
|
const offset = (page - 1) * limit;
|
|
|
|
const whereFragment = q
|
|
? sql`WHERE up.text LIKE ${`%${q}%`} OR u.username LIKE ${`%${q}%`}`
|
|
: sql``;
|
|
|
|
const [prefixes] =
|
|
(await db.execute(sql`SELECT up.id, up.user_id, up.text, up.color, up.icon, up.effect, up.active, u.username
|
|
FROM custom_prefixes up
|
|
LEFT JOIN users u ON u.id = up.user_id
|
|
${whereFragment}
|
|
ORDER BY up.id DESC
|
|
LIMIT ${limit} OFFSET ${offset}`)) as unknown as [
|
|
UserPrefix[],
|
|
unknown,
|
|
];
|
|
|
|
const [countResult] =
|
|
(await db.execute(sql`SELECT COUNT(*) as total FROM custom_prefixes up
|
|
LEFT JOIN users u ON u.id = up.user_id
|
|
${whereFragment}`)) as unknown as [[{ total: bigint }], unknown];
|
|
|
|
const total = Number(countResult[0]?.total || 0);
|
|
|
|
return apiOk({
|
|
prefixes: prefixes.map((p) => ({
|
|
...p,
|
|
icon: p.icon || "",
|
|
effect: p.effect || "",
|
|
active: Boolean(p.active),
|
|
created_at: "",
|
|
})),
|
|
total,
|
|
page,
|
|
pages: Math.max(1, Math.ceil(total / limit)),
|
|
});
|
|
},
|
|
);
|
|
|
|
export const POST = withAdmin(
|
|
{ permission: PERMS.PREFIXES_EDIT },
|
|
async (request) => {
|
|
const body = await request.json();
|
|
const { username, text, color, icon, effect, active } = body;
|
|
|
|
if (!username || !text || !color) {
|
|
return apiError("Missing required fields: username, text, color");
|
|
}
|
|
|
|
const [users] = (await db.execute(
|
|
sql`SELECT id FROM users WHERE username = ${username} LIMIT 1`,
|
|
)) as unknown as [{ id: number }[], unknown];
|
|
|
|
if (!users || users.length === 0) {
|
|
return apiError("User not found");
|
|
}
|
|
|
|
const userId = users[0].id;
|
|
|
|
await db.execute(
|
|
sql`INSERT INTO custom_prefixes (user_id, text, color, icon, effect, active)
|
|
VALUES (${userId}, ${text}, ${color}, ${icon || ""}, ${effect || ""}, ${active ? 1 : 0})`,
|
|
);
|
|
|
|
return apiOk({ created: true });
|
|
},
|
|
);
|
|
|
|
export const PUT = withAdmin(
|
|
{ permission: PERMS.PREFIXES_EDIT },
|
|
async (request) => {
|
|
const body = await request.json();
|
|
const { id, text, color, icon, effect, active } = body;
|
|
|
|
if (!id) return apiError("Missing prefix id");
|
|
|
|
await db.execute(
|
|
sql`UPDATE custom_prefixes
|
|
SET text = ${text}, color = ${color}, icon = ${icon || ""}, effect = ${effect || ""}, active = ${active ? 1 : 0}
|
|
WHERE id = ${id}`,
|
|
);
|
|
|
|
return apiOk({ updated: id });
|
|
},
|
|
);
|
|
|
|
export const DELETE = withAdmin(
|
|
{ permission: PERMS.PREFIXES_EDIT },
|
|
async (request) => {
|
|
const body = await request.json();
|
|
const id = body.id;
|
|
|
|
if (!id) return apiError("Missing prefix id");
|
|
|
|
await db.execute(sql`DELETE FROM custom_prefixes WHERE id = ${id}`);
|
|
|
|
return apiOk({ deleted: id });
|
|
},
|
|
);
|