Files
app
components
data
functions
api
admin-apps
appeals
auth
data-transfers
events-team
game-appeals
game-bans
gme
inactivity
[id].ts
_middleware.ts
new.ts
validate.ts
infractions
me
mod-queue
reports
short-links
uploads
[[path]].ts
coconut.ts
events.ts
webview-captcha.ts
_middleware.ts
common.ts
email.ts
gcloud.ts
permissions.ts
roblox-open-cloud.ts
upload.ts
public
.gitignore
.node-version
.prettierignore
OFL.txt
README.md
emotion-server.js
index.css
index.d.ts
package-lock.json
package.json
remix.config.js
server.ts
theme.ts
tsconfig.json
car-crushers-portal/functions/api/inactivity/new.ts

86 lines
2.3 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.env.D1.prepare(
"INSERT INTO inactivity_notices (created_at, departments, end, hiatus, id, reason, start, user) VALUES (?, ?, ?, ?, ?, ?, ?, ?);",
)
.bind(
Date.now(),
JSON.stringify(departments),
end,
typeof hiatus === "boolean" ? Number(hiatus) : 0,
inactivityId,
reason,
start,
JSON.stringify({
id: context.data.current_user.id,
email: context.data.current_user.email,
username: context.data.current_user.username,
}),
)
.run();
if (typeof senderTokenId === "string") {
await context.env.D1.prepare(
"INSERT INTO push_notifications (created_at, event_id, event_type) VALUES (?, ?, ?);",
)
.bind(Date.now(), inactivityId, "inactivity")
.run();
}
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,
});
}