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,3 +1,5 @@
import { jsonError } from "../../common.js";
export async function onRequestPost(context: RequestContext) {
const { learned, whyBanned, whyUnban } = context.data.body;
@ -12,25 +14,11 @@ export async function onRequestPost(context: RequestContext) {
!whyUnban.length ||
whyUnban.length > 2000
)
return new Response(
'{"error":"One or more fields are missing or invalid"}',
{
headers: {
"content-type": "application/json",
},
status: 400,
},
);
return jsonError("One or more fields are missing or invalid", 400);
const { current_user: currentUser } = context.data;
if (!currentUser.email)
return new Response('{"error":"No email for this session"}', {
headers: {
"content-type": "application/json",
},
status: 403,
});
if (!currentUser.email) return jsonError("No email for this session", 403);
const existingAppeals = await context.env.DATA.list({
prefix: `appeal_${currentUser.id}`,
@ -45,12 +33,7 @@ export async function onRequestPost(context: RequestContext) {
(appeal) => (appeal.metadata as { [k: string]: any })?.open,
)
)
return new Response('{"error":"Appeal already submitted"}', {
headers: {
"content-type": "application/json",
},
status: 403,
});
return jsonError("Appeal already submitted", 403);
if (
await context.env.D1.prepare("SELECT * FROM appeal_bans WHERE user = ?;")