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);
const data: Record<string, any> | null = await context.env.D1.prepare(
"SELECT attachments FROM reports WHERE id = ?;",
)
.bind(id)
.first();
const data = await context.data.prisma.report.findUnique({
select: {
attachments: true,
},
where: {
id,
},
});
if (!data) return jsonError("No report with that ID found", 404);
data.attachments = JSON.parse(data.attachments);
const accessToken = await GetAccessToken(context.env);
const attachmentDeletePromises = [];
const existingAttachments = [...data.attachments];
const existingAttachments = [...(data.attachments as string[])];
for (const attachment of existingAttachments) {
if (!attachment.startsWith("t/")) data.attachments.push(`t/${attachment}`);
else data.attachments.push(attachment.replace("t/", ""));
for (let i = 0; i < existingAttachments.length; i++) {
if (!existingAttachments[i].startsWith("t/"))
existingAttachments[i] = existingAttachments[i].replace("t/", "");
}
for (const attachment of data.attachments)
for (const attachment of existingAttachments)
attachmentDeletePromises.push(
fetch(
`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 context.env.D1.prepare("DELETE FROM reports WHERE id = ?;")
.bind(id)
.run();
await context.data.prisma.report.delete({
where: {
id,
},
});
return new Response(null, {
status: 204,