Migrate report recall endpoint

This commit is contained in:
2026-04-11 04:29:15 -04:00
parent 1a891e5898
commit b60f211d7b

View File

@@ -15,26 +15,27 @@ export async function onRequestPost(context: RequestContext) {
) )
return jsonError("No processing report with that ID found", 404); return jsonError("No processing report with that ID found", 404);
const data: Record<string, any> | null = await context.env.D1.prepare( const data = await context.data.prisma.report.findUnique({
"SELECT attachments FROM reports WHERE id = ?;", select: {
) attachments: true,
.bind(id) },
.first(); where: {
id,
},
});
if (!data) return jsonError("No report with that ID found", 404); if (!data) return jsonError("No report with that ID found", 404);
data.attachments = JSON.parse(data.attachments);
const accessToken = await GetAccessToken(context.env); const accessToken = await GetAccessToken(context.env);
const attachmentDeletePromises = []; const attachmentDeletePromises = [];
const existingAttachments = [...data.attachments]; const existingAttachments = [...(data.attachments as string[])];
for (const attachment of existingAttachments) { for (let i = 0; i < existingAttachments.length; i++) {
if (!attachment.startsWith("t/")) data.attachments.push(`t/${attachment}`); if (!existingAttachments[i].startsWith("t/"))
else data.attachments.push(attachment.replace("t/", "")); existingAttachments[i] = existingAttachments[i].replace("t/", "");
} }
for (const attachment of data.attachments) for (const attachment of existingAttachments)
attachmentDeletePromises.push( attachmentDeletePromises.push(
fetch( fetch(
`https://storage.googleapis.com/storage/v1/b/portal-carcrushers-cc/o/${encodeURIComponent( `https://storage.googleapis.com/storage/v1/b/portal-carcrushers-cc/o/${encodeURIComponent(
@@ -50,9 +51,11 @@ export async function onRequestPost(context: RequestContext) {
); );
await Promise.allSettled(attachmentDeletePromises); await Promise.allSettled(attachmentDeletePromises);
await context.env.D1.prepare("DELETE FROM reports WHERE id = ?;") await context.data.prisma.report.delete({
.bind(id) where: {
.run(); id,
},
});
return new Response(null, { return new Response(null, {
status: 204, status: 204,