Migrate the rest of the easy stuff
This commit is contained in:
@@ -1,18 +1,22 @@
|
||||
import { jsonError, jsonResponse } from "../../common.js";
|
||||
import sendEmail from "../../email.js";
|
||||
import { sendPushNotification } from "../../gcloud.js";
|
||||
import { type JsonArray, type JsonObject } from "@prisma/client/runtime/client";
|
||||
|
||||
export async function onRequestDelete(context: RequestContext) {
|
||||
const result = await context.env.D1.prepare(
|
||||
"SELECT json_extract(user, '*.id') AS uid FROM inactivity_notices WHERE id = ?;",
|
||||
)
|
||||
.bind(context.params.id)
|
||||
.first();
|
||||
const result = await context.data.prisma.inactivityNotice.findUnique({
|
||||
select: {
|
||||
user: true,
|
||||
},
|
||||
where: {
|
||||
id: context.params.id as string,
|
||||
},
|
||||
});
|
||||
|
||||
if (!result) return jsonError("No inactivity notice with that ID", 404);
|
||||
|
||||
if (
|
||||
result.uid !== context.data.current_user.id &&
|
||||
(result.user as JsonObject).id !== context.data.current_user.id &&
|
||||
!(context.data.current_user.permissions & (1 << 0))
|
||||
)
|
||||
return jsonError(
|
||||
@@ -39,26 +43,17 @@ export async function onRequestGet(context: RequestContext) {
|
||||
)
|
||||
return jsonError("Forbidden", 403);
|
||||
|
||||
const result: Record<
|
||||
string,
|
||||
string | number | { [k: string]: string }
|
||||
> | null = await context.env.D1.prepare(
|
||||
"SELECT * FROM inactivity_notices WHERE id = ?;",
|
||||
)
|
||||
.bind(context.params.id)
|
||||
.first();
|
||||
|
||||
if (!result) return jsonError("Inactivity notice does not exist", 404);
|
||||
|
||||
result.decisions = JSON.parse(result.decisions as string);
|
||||
result.departments = JSON.parse(result.departments as string);
|
||||
result.user = JSON.parse(result.user as string);
|
||||
const result = await context.data.prisma.inactivityNotice.findUnique({
|
||||
where: {
|
||||
id: context.params.id as string,
|
||||
},
|
||||
});
|
||||
|
||||
return jsonResponse(JSON.stringify(result));
|
||||
}
|
||||
|
||||
export async function onRequestPost(context: RequestContext) {
|
||||
const { accepted }: { accepted?: boolean } = context.data.body;
|
||||
const { accepted }: { accepted?: any } = context.data.body;
|
||||
|
||||
if (typeof accepted !== "boolean")
|
||||
return jsonError("'accepted' must be a boolean", 400);
|
||||
@@ -77,32 +72,45 @@ export async function onRequestPost(context: RequestContext) {
|
||||
if (!userAdminDepartments.length)
|
||||
return jsonError("You are not a manager of any departments", 403);
|
||||
|
||||
const requestedNotice: { [k: string]: any } | null =
|
||||
await context.env.D1.prepare(
|
||||
"SELECT decisions, departments, user FROM inactivity_notices WHERE id = ?;",
|
||||
)
|
||||
.bind(context.params.id)
|
||||
.first();
|
||||
const requestedNotice = await context.data.prisma.inactivityNotice.findUnique(
|
||||
{
|
||||
select: {
|
||||
decisions: true,
|
||||
departments: true,
|
||||
user: true,
|
||||
},
|
||||
where: {
|
||||
id: context.params.id as string,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
if (!requestedNotice)
|
||||
return jsonError("Inactivity notices does not exist", 404);
|
||||
|
||||
const decisions: { [dept: string]: boolean } = JSON.parse(
|
||||
requestedNotice.decisions,
|
||||
);
|
||||
const decisions = requestedNotice.decisions as { [k: string]: boolean };
|
||||
|
||||
for (const department of userAdminDepartments) {
|
||||
if (!JSON.parse(requestedNotice.departments).includes(department)) continue;
|
||||
if (!(requestedNotice.departments as JsonArray).includes(department))
|
||||
continue;
|
||||
decisions[department] = accepted;
|
||||
}
|
||||
|
||||
const applicableDepartments = JSON.parse(requestedNotice.departments).length;
|
||||
const applicableDepartments = (requestedNotice.departments as JsonArray)
|
||||
.length;
|
||||
const user = requestedNotice.user as JsonObject;
|
||||
|
||||
await context.env.D1.prepare(
|
||||
"UPDATE inactivity_notices SET decisions = ?, user = json_remove(user, '$.email') WHERE id = ?;",
|
||||
)
|
||||
.bind(JSON.stringify(decisions), context.params.id)
|
||||
.run();
|
||||
delete user.email;
|
||||
|
||||
await context.data.prisma.inactivityNotice.update({
|
||||
data: {
|
||||
decisions,
|
||||
user,
|
||||
},
|
||||
where: {
|
||||
id: context.params.id as string,
|
||||
},
|
||||
});
|
||||
|
||||
if (Object.values(decisions).length === applicableDepartments) {
|
||||
const approved =
|
||||
@@ -111,11 +119,16 @@ export async function onRequestPost(context: RequestContext) {
|
||||
const denied =
|
||||
Object.values(decisions).filter((d) => !d).length !==
|
||||
applicableDepartments;
|
||||
const fcmTokenResult: FCMTokenResult | null = await context.env.D1.prepare(
|
||||
"SELECT token FROM push_notifications WHERE event_id = ? AND event_type = 'inactivity';",
|
||||
)
|
||||
.bind(context.params.id)
|
||||
.first();
|
||||
const fcmTokenResult =
|
||||
await context.data.prisma.pushNotification.findUnique({
|
||||
select: {
|
||||
token: true,
|
||||
},
|
||||
where: {
|
||||
event_id: context.params.id as string,
|
||||
event_type: "inactivity",
|
||||
},
|
||||
});
|
||||
|
||||
if (fcmTokenResult) {
|
||||
let status = "Approved";
|
||||
@@ -132,16 +145,19 @@ export async function onRequestPost(context: RequestContext) {
|
||||
fcmTokenResult.token,
|
||||
);
|
||||
|
||||
await context.env.D1.prepare(
|
||||
"DELETE FROM push_notifications WHERE event_id = ? AND event_type = 'inactivity';",
|
||||
).bind(context.params.id);
|
||||
await context.data.prisma.pushNotification.delete({
|
||||
where: {
|
||||
event_id: context.params.id as string,
|
||||
event_type: "inactivity",
|
||||
},
|
||||
});
|
||||
} else {
|
||||
await sendEmail(
|
||||
requestedNotice.user.email,
|
||||
(requestedNotice.user as JsonObject).email as string,
|
||||
context.env.MAILGUN_API_KEY,
|
||||
`Inactivity Request ${approved ? "Approved" : "Denied"}`,
|
||||
`inactivity_${approved ? "approved" : "denied"}`,
|
||||
{ username: requestedNotice.user.username },
|
||||
{ username: (requestedNotice.user as JsonObject).username as string },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user