From c295f3e4df7196ed9b0798f386c59f43b95b2ccc Mon Sep 17 00:00:00 2001 From: regalijan Date: Thu, 19 Oct 2023 16:50:15 -0400 Subject: [PATCH] Add inactivity deletion endpoint --- functions/api/inactivity/[id].ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 functions/api/inactivity/[id].ts diff --git a/functions/api/inactivity/[id].ts b/functions/api/inactivity/[id].ts new file mode 100644 index 0000000..ea46149 --- /dev/null +++ b/functions/api/inactivity/[id].ts @@ -0,0 +1,26 @@ +export async function onRequestDelete(context: RequestContext) { + const kvResult = await context.env.DATA.get( + `inactivity_${context.params.id}`, + ); + + if ( + !kvResult || + (JSON.parse(kvResult).user.id !== context.data.current_user.id && + !(context.data.current_user.permissions & (1 << 0))) + ) + return new Response('{"error":"No inactivity notice with that ID"}', { + headers: { + "content-type": "application/json", + }, + status: 404, + }); + + await context.env.DATA.delete(`inactivity_${context.params.id}`); + await context.env.D1.prepare("DELETE FROM inactivity_notices WHERE id = ?;") + .bind(context.params.id) + .run(); + + return new Response(null, { + status: 204, + }); +}