Fix new strike endpoint

This commit is contained in:
Regalijan 2024-12-05 21:38:40 -05:00
parent cae9af5359
commit c454d2a9df
Signed by: regalijan
GPG Key ID: 5D4196DA269EF520

View File

@ -2,6 +2,7 @@ import { jsonError, jsonResponse } from "../../../common.js";
export async function onRequestPost(context: RequestContext) { export async function onRequestPost(context: RequestContext) {
const { reason, user } = context.data.body; const { reason, user } = context.data.body;
const { D1 } = context.env;
if (typeof reason !== "string") return jsonError("Invalid reason", 400); if (typeof reason !== "string") return jsonError("Invalid reason", 400);
@ -9,7 +10,10 @@ export async function onRequestPost(context: RequestContext) {
typeof user !== "string" || typeof user !== "string" ||
user.length > 20 || user.length > 20 ||
user.length < 17 || user.length < 17 ||
user.match(/\D/) user.match(/\D/) ||
!(await D1.prepare("SELECT id FROM et_members WHERE id = ?;")
.bind(user)
.first())
) )
return jsonError("Invalid user id", 400); return jsonError("Invalid user id", 400);
@ -17,11 +21,14 @@ export async function onRequestPost(context: RequestContext) {
const id = crypto.randomUUID().replaceAll("-", ""); const id = crypto.randomUUID().replaceAll("-", "");
const actingUser = context.data.current_user.id; const actingUser = context.data.current_user.id;
await context.env.D1.prepare( await D1.batch([
"INSERT INTO et_strikes (created_at, created_by, id, reason, user) VALUES (?1, ?2, ?3, ?4, ?5); UPDATE et_members SET points = points - 100 WHERE id = ?5;", D1.prepare(
) "INSERT INTO et_strikes (created_at, created_by, id, reason, user) VALUES (?, ?, ?, ?, ?); UPDATE et_members SET points = points - 100 WHERE id = ?5;",
.bind(now, actingUser, id, reason, user) ).bind(now, actingUser, id, reason, user),
.run(); D1.prepare(
"UPDATE et_members SET points = points - 100 WHERE id = ?;",
).bind(user),
]);
return jsonResponse( return jsonResponse(
JSON.stringify({ JSON.stringify({