Files
car-crushers-portal/functions/api/inactivity/new.ts
Regalijan 5c17f87f89
All checks were successful
Test, Build, Deploy / Test, Build, and Deploy (push) Successful in 58s
Test, Build, Deploy / Create Sentry Release (push) Successful in 6s
Migrate the rest of the easy stuff
2026-04-11 04:32:09 -04:00

86 lines
2.1 KiB
TypeScript

import validateInactivity from "./validate.js";
export async function onRequestPost(context: RequestContext) {
const { departments, end, hiatus, reason, senderTokenId, start } =
context.data.body;
const validationFailureResponse = validateInactivity(
departments,
end,
hiatus,
reason,
start,
context.data.departments,
);
if (validationFailureResponse) return validationFailureResponse;
const inactivityId =
context.data.current_user.id +
(context.request.headers.get("cf-ray") as string).split("-")[0] +
Date.now().toString();
await context.data.prisma.inactivityNotice.create({
data: {
decisions: {},
departments,
end,
hiatus,
id: inactivityId,
reason,
start,
user: {
id: context.data.current_user.id,
email: context.data.current_user.email,
username: context.data.current_user.username,
},
},
});
if (typeof senderTokenId === "string") {
await context.data.prisma.pushNotification.create({
data: {
event_id: inactivityId,
event_type: "inactivity",
token: senderTokenId,
},
});
}
const departmentsToNotify = [];
const departmentRoles = [];
const { env } = context;
for (const department of departments) {
departmentsToNotify.push(env[`${department}_INACTIVITY_WEBHOOK`]);
departmentRoles.push(env[`${department}_INACTIVITY_ROLE`]);
}
const webhookPromises = [];
for (let i = 0; i < departmentsToNotify.length; i++)
webhookPromises.push(
fetch(departmentsToNotify[i], {
body: JSON.stringify({
content: `<@&${departmentRoles[i]}>`,
embeds: [
{
title: "Inactivity Notice Submitted",
color: 3756250,
description: `View this notice at https://carcrushers.cc/mod-queue?id=${inactivityId}&type=inactivity`,
},
],
}),
headers: {
"content-type": "application/json",
},
method: "POST",
}),
);
await Promise.allSettled(webhookPromises);
return new Response(null, {
status: 204,
});
}