97 lines
2.6 KiB
TypeScript
97 lines
2.6 KiB
TypeScript
import type { APIRoute } from 'astro';
|
|
import { z } from 'astro:schema';
|
|
import { API_SECRET } from 'astro:env/server';
|
|
import { db } from '@db/database.ts';
|
|
|
|
const postSchema = z.object({
|
|
reporter: z.string(),
|
|
reported: z.string().nullable(),
|
|
reason: z.string()
|
|
});
|
|
|
|
export const POST: APIRoute = async ({ request }) => {
|
|
if (API_SECRET && request.headers.get('authorization') !== `Basic ${API_SECRET}`) {
|
|
return new Response(null, { status: 401 });
|
|
}
|
|
|
|
let parsed;
|
|
try {
|
|
parsed = await postSchema.parseAsync(await request.json());
|
|
} catch (_) {
|
|
return new Response(null, { status: 400 });
|
|
}
|
|
|
|
const reporterTeam = await db.getTeamByUserUuid({ uuid: parsed.reporter });
|
|
if (!reporterTeam) return new Response(null, { status: 404 });
|
|
|
|
let reportedTeam = null;
|
|
if (parsed.reported) {
|
|
reportedTeam = await db.getTeamByUserUuid({ uuid: parsed.reported });
|
|
if (!reportedTeam) return new Response(null, { status: 404 });
|
|
}
|
|
|
|
const report = await db.addReport({
|
|
reporterTeamId: reporterTeam.team.id,
|
|
reportedTeamId: reportedTeam?.team.id,
|
|
reason: parsed.reason,
|
|
body: null
|
|
});
|
|
|
|
return new Response(JSON.stringify({ url: report.url }), { status: 200 });
|
|
};
|
|
|
|
const putSchema = z.object({
|
|
reporter: z.string().nullable(),
|
|
reported: z.string(),
|
|
reason: z.string(),
|
|
body: z.string().nullable(),
|
|
notice: z.string().nullable(),
|
|
statement: z.string().nullable(),
|
|
strike_reason_id: z.number()
|
|
});
|
|
|
|
export const PUT: APIRoute = async ({ request }) => {
|
|
if (API_SECRET && request.headers.get('authorization') !== `Basic ${API_SECRET}`) {
|
|
return new Response(null, { status: 401 });
|
|
}
|
|
|
|
let parsed;
|
|
try {
|
|
parsed = await putSchema.parseAsync(await request.json());
|
|
} catch (_) {
|
|
return new Response(null, { status: 400 });
|
|
}
|
|
|
|
let reporterTeam = null;
|
|
if (parsed.reported) {
|
|
reporterTeam = await db.getTeamByUserUuid({ uuid: parsed.reported });
|
|
if (!reporterTeam) return new Response(null, { status: 404 });
|
|
}
|
|
|
|
const reportedTeam = await db.getTeamByUserUuid({ uuid: parsed.reported });
|
|
if (!reportedTeam) return new Response(null, { status: 404 });
|
|
|
|
await db.transaction(async (tx) => {
|
|
const report = await tx.addReport({
|
|
reporterTeamId: reporterTeam?.team.id,
|
|
reportedTeamId: reportedTeam.team.id,
|
|
reason: parsed.reason,
|
|
body: parsed.body
|
|
});
|
|
|
|
await tx.editReportStatus({
|
|
reportId: report.id,
|
|
notice: parsed.notice,
|
|
statement: parsed.statement,
|
|
status: 'closed'
|
|
});
|
|
|
|
await tx.editStrike({
|
|
reportId: report.id,
|
|
strikeReasonId: parsed.strike_reason_id
|
|
});
|
|
});
|
|
|
|
return new Response(null, { status: 200 });
|
|
};
|