Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Send notifications on inactivity notice closure
  • Loading branch information
regalijan committed Oct 20, 2023
1 parent 6ea7fae commit adde228
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions functions/api/inactivity/[id].ts
@@ -1,4 +1,6 @@
import { jsonError } from "../../common.js";
import sendEmail from "../../email.js";
import { sendPushNotification } from "../../gcloud.js";
import validateInactivityNotice from "./validate.js";

export async function onRequestDelete(context: RequestContext) {
Expand Down Expand Up @@ -65,11 +67,34 @@ export async function onRequestPost(context: RequestContext) {
if (Object.values(decisions).length === requestedNotice.departments.length) {
requestedNotice.open = false;

const approved = !Object.values(decisions).find((d) => !d);

await context.env.D1.prepare(
"UPDATE inactivity_notices SET open = 0 WHERE id = ?;",
)
.bind(context.params.id)
.run();

if (requestedNotice.fcm_token) {
await sendPushNotification(
context.env,
`Inactivity Request ${approved ? "Approved" : "Denied"}`,
accepted
? "Your inactivity request was approved."
: "Your inactivity request was denied, please reach to management if you require more details.",
requestedNotice.fcm_token,
);
} else {
await sendEmail(
requestedNotice.user.email,
context.env.MAILGUN_API_KEY,
`Inactivity Request ${approved ? "Approved" : "Denied"}`,
`inactivity_${approved ? "approved" : "denied"}`,
{ username: requestedNotice.user.username },
);
}

delete requestedNotice.user.email;
}

await context.env.DATA.put(
Expand Down

0 comments on commit adde228

Please sign in to comment.