Greatly reduce repeated code

This commit is contained in:
2023-10-19 16:50:48 -04:00
parent 47e639be43
commit dd2d9f2672
34 changed files with 196 additions and 481 deletions

View File

@ -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);
}