Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Send push notifs on appeal action
  • Loading branch information
regalijan committed Oct 19, 2023
1 parent ffe7010 commit 1618627
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 27 deletions.
39 changes: 26 additions & 13 deletions functions/api/appeals/[id]/accept.ts
@@ -1,26 +1,39 @@
import { jsonError } from "../../../common.js";
import sendEmail from "../../../email.js";
import { sendPushNotification } from "../../../gcloud.js";

export async function onRequestPost(context: RequestContext) {
const { appeal } = context.data;
const emailResponse = await sendEmail(
appeal.user.email,
context.env.MAILGUN_API_KEY,
"Appeal Accepted",
"appeal_accepted",
{
note: context.data.body.feedback || "No note provided.",
},
);

if (!emailResponse.ok) {
console.log(await emailResponse.json());
return jsonError("Failed to accept appeal", 500);
if (appeal.fcm_token) {
await sendPushNotification(
context.env,
"Appeal Accepted",
context.data.body.feedback || "No additional details to display",
appeal.fcm_token,
);
} else {
const emailResponse = await sendEmail(
appeal.user.email,
context.env.MAILGUN_API_KEY,
"Appeal Accepted",
"appeal_accepted",
{
note: context.data.body.feedback || "No note provided.",
},
);

if (!emailResponse.ok) {
console.log(await emailResponse.json());
return jsonError("Failed to accept appeal", 500);
}
}

const { current_user: currentUser } = context.data;

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

Expand Down
41 changes: 27 additions & 14 deletions functions/api/appeals/[id]/deny.ts
@@ -1,25 +1,38 @@
import { jsonError } from "../../../common.js";
import sendEmail from "../../../email.js";
import { sendPushNotification } from "../../../gcloud.js";

export async function onRequestPost(context: RequestContext) {
const { appeal } = context.data;
if (appeal.fcm_token) {
await sendPushNotification(
context.env,
"Appeal Denied",
`Unfortunately, we have decided to deny your appeal for the following reason: ${
context.data.body.feedback || "No additional details"
}`,
appeal.fcm_token,
);
} else {
const emailResponse = await sendEmail(
appeal.user.email,
context.env.MAILGUN_API_KEY,
"Appeal Denied",
"appeal_denied",
{
note: context.data.body.feedback || "No note provided.",
},
);

const emailResponse = await sendEmail(
appeal.user.email,
context.env.MAILGUN_API_KEY,
"Appeal Denied",
"appeal_denied",
{
note: context.data.body.feedback || "No note provided.",
},
);

if (!emailResponse.ok) {
console.log(await emailResponse.json());
return jsonError("Failed to deny appeal", 500);
if (!emailResponse.ok) {
console.log(await emailResponse.json());
return jsonError("Failed to deny appeal", 500);
}
}

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

Expand Down

0 comments on commit 1618627

Please sign in to comment.