39 lines
941 B
TypeScript
39 lines
941 B
TypeScript
import { jsonError } from "../../../../common.js";
|
|
|
|
export async function onRequestPost(context: RequestContext) {
|
|
const { D1 } = context.env;
|
|
const { event } = context.data;
|
|
|
|
try {
|
|
const completionTimeRow = await D1.prepare(
|
|
"SELECT performed_at FROM events WHERE id = ?;",
|
|
)
|
|
.bind(event.id)
|
|
.first();
|
|
|
|
if (typeof completionTimeRow?.performed_at !== "number")
|
|
return jsonError(
|
|
"The event is already marked as complete or forgotten",
|
|
400,
|
|
);
|
|
|
|
await D1.batch([
|
|
D1.prepare("UPDATE events SET performed_at = ? WHERE id = ?;").bind(
|
|
Date.now(),
|
|
event.id,
|
|
),
|
|
D1.prepare(
|
|
"UPDATE et_members SET points = points + 10 WHERE id = ?;",
|
|
).bind(event.created_by),
|
|
]);
|
|
} catch (e) {
|
|
console.log(e);
|
|
|
|
return jsonError("Failed to complete batch transaction", 500);
|
|
}
|
|
|
|
return new Response(null, {
|
|
status: 204,
|
|
});
|
|
}
|