Greatly reduce repeated code
This commit is contained in:
@ -1,28 +1,19 @@
|
||||
import { jsonError } from "../../common.js";
|
||||
import validateInactivityNotice from "./validate.js";
|
||||
|
||||
function jsonResponse(body: string, status = 200): Response {
|
||||
return new Response(body, {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status,
|
||||
});
|
||||
}
|
||||
|
||||
export async function onRequestDelete(context: RequestContext) {
|
||||
const kvResult = await context.env.DATA.get(
|
||||
`inactivity_${context.params.id}`,
|
||||
);
|
||||
|
||||
if (!kvResult)
|
||||
return jsonResponse('{"error":"No inactivity notice with that ID"}', 404);
|
||||
if (!kvResult) return jsonError("No inactivity notice with that ID", 404);
|
||||
|
||||
if (
|
||||
JSON.parse(kvResult).user.id !== context.data.current_user.id &&
|
||||
!(context.data.current_user.permissions & (1 << 0))
|
||||
)
|
||||
return jsonResponse(
|
||||
'{"error":"You do not have permission to delete this inactivity notice"}',
|
||||
return jsonError(
|
||||
"You do not have permission to delete this inactivity notice",
|
||||
403,
|
||||
);
|
||||
|
||||
@ -36,18 +27,46 @@ export async function onRequestDelete(context: RequestContext) {
|
||||
});
|
||||
}
|
||||
|
||||
export async function onRequestPost(context: RequestContext) {
|
||||
const { accepted }: { accepted?: boolean } = context.data.body;
|
||||
|
||||
if (typeof accepted !== "boolean")
|
||||
return jsonError("'accepted' must be a boolean", 400);
|
||||
|
||||
const adminDepartments: { [k: string]: number } = {
|
||||
DM: 1 << 11,
|
||||
ET: 1 << 4,
|
||||
FM: 1 << 7,
|
||||
WM: 1 << 6,
|
||||
};
|
||||
|
||||
const userAdminDepartments = Object.values(adminDepartments).filter(
|
||||
(dept) => context.data.current_user.permissions & dept,
|
||||
);
|
||||
|
||||
if (!userAdminDepartments.length)
|
||||
return jsonError("You are not a manager of any departments", 403);
|
||||
|
||||
const requestedNotice = await context.env.DATA.get(
|
||||
`inactivity_${context.params.id as string}`,
|
||||
{ type: "json" },
|
||||
);
|
||||
|
||||
if (!requestedNotice)
|
||||
return jsonError("Inactivity notices does not exist", 404);
|
||||
}
|
||||
|
||||
export async function onRequestPut(context: RequestContext) {
|
||||
const kvResult: InactivityNoticeProps | null = await context.env.DATA.get(
|
||||
`inactivity_${context.params.id}`,
|
||||
{ type: "json" },
|
||||
);
|
||||
|
||||
if (!kvResult)
|
||||
return jsonResponse('{"error":"No inactivity notice with that ID"}', 404);
|
||||
if (!kvResult) return jsonError("No inactivity notice with that ID", 404);
|
||||
|
||||
if (kvResult.user.id !== context.data.current_user.id)
|
||||
return jsonResponse(
|
||||
'{"error":"You do not have permission to modify this inactivity notice"}',
|
||||
return jsonError(
|
||||
"You do not have permission to modify this inactivity notice",
|
||||
403,
|
||||
);
|
||||
|
||||
@ -58,7 +77,7 @@ export async function onRequestPut(context: RequestContext) {
|
||||
.run();
|
||||
|
||||
if (!Boolean(d1entry.results.at(0)?.open))
|
||||
return jsonResponse("Cannot modify a closed inactivity notice", 403);
|
||||
return jsonError("Cannot modify a closed inactivity notice", 403);
|
||||
|
||||
const { departments, end, reason, start } = context.data.body;
|
||||
|
||||
@ -84,4 +103,8 @@ export async function onRequestPut(context: RequestContext) {
|
||||
expirationTtl: 63072000,
|
||||
},
|
||||
);
|
||||
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
});
|
||||
}
|
||||
|
@ -1,15 +1,8 @@
|
||||
function makeResponse(body: string, status: number): Response {
|
||||
return new Response(body, {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status,
|
||||
});
|
||||
}
|
||||
import { jsonError } from "../../common.js";
|
||||
|
||||
export async function onRequest(context: RequestContext) {
|
||||
if (!context.data.current_user)
|
||||
return makeResponse('{"error":"You are not logged in"}', 401);
|
||||
return jsonError("You are not logged in", 401);
|
||||
|
||||
const { permissions } = context.data.current_user;
|
||||
const departments = {
|
||||
|
@ -1,11 +1,4 @@
|
||||
function errorResponse(error: string, status = 400): Response {
|
||||
return new Response(JSON.stringify({ error }), {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status,
|
||||
});
|
||||
}
|
||||
import { jsonError } from "../../common.js";
|
||||
|
||||
export default function (
|
||||
selectedDepartments: string[],
|
||||
@ -14,8 +7,7 @@ export default function (
|
||||
start: any,
|
||||
userDepartments?: string[],
|
||||
): void | Response {
|
||||
if (!userDepartments)
|
||||
return errorResponse("Not part of any departments", 403);
|
||||
if (!userDepartments) return jsonError("Not part of any departments", 403);
|
||||
|
||||
if (
|
||||
!Array.isArray(selectedDepartments) ||
|
||||
@ -24,10 +16,10 @@ export default function (
|
||||
typeof reason !== "string" ||
|
||||
typeof start !== "string"
|
||||
)
|
||||
return errorResponse("Invalid notice");
|
||||
return jsonError("Invalid notice", 400);
|
||||
|
||||
if (!selectedDepartments.every((dept) => userDepartments.includes(dept)))
|
||||
return errorResponse(
|
||||
return jsonError(
|
||||
"Cannot file an inactivity notice in a department you are not part of",
|
||||
403,
|
||||
);
|
||||
@ -45,5 +37,5 @@ export default function (
|
||||
startDate.getFullYear() > now.getFullYear() + 1 ||
|
||||
endDate.valueOf() < startDate.valueOf()
|
||||
)
|
||||
return errorResponse("Dates are invalid");
|
||||
return jsonError("Dates are invalid", 400);
|
||||
}
|
||||
|
Reference in New Issue
Block a user