Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

Delete the uploaded attachments when answering a retryable 500

Jeromed76b700057c6
diff --git a/supabase/functions/postmark/extractAndUploadAttachments.ts b/supabase/functions/postmark/extractAndUploadAttachments.ts
index 7501fca7..56cd6206 100644
--- a/supabase/functions/postmark/extractAndUploadAttachments.ts
+++ b/supabase/functions/postmark/extractAndUploadAttachments.ts
@@ -95,3 +95,15 @@ const fixPublicUrl = (url: string) => {
const localUrl = jwtIssuer.replace("/auth/v1", "");
return url.replace("http://kong:8000", localUrl);
};
+
+export const removeUploadedAttachments = async (attachments: Attachment[]) => {
+ if (!attachments.length) {
+ return;
+ }
+ const { error } = await supabaseAdmin.storage
+ .from("attachments")
+ .remove(attachments.map((attachment) => attachment.path));
+ if (error) {
+ console.error("Could not remove the uploaded attachments", error);
+ }
+};
diff --git a/supabase/functions/postmark/index.ts b/supabase/functions/postmark/index.ts
index cb7312e2..fcf990b0 100644
--- a/supabase/functions/postmark/index.ts
+++ b/supabase/functions/postmark/index.ts
@@ -12,7 +12,10 @@ import {
import { extractMailContactData } from "./extractMailContactData.ts";
import { getExpectedAuthorization } from "./getExpectedAuthorization.ts";
import { getNoteContent } from "./getNoteContent.ts";
-import { extractAndUploadAttachments } from "./extractAndUploadAttachments.ts";
+import {
+ extractAndUploadAttachments,
+ removeUploadedAttachments,
+} from "./extractAndUploadAttachments.ts";
import { supabaseAdmin } from "../_shared/supabaseAdmin.ts";
const webhookUser = Deno.env.get("POSTMARK_WEBHOOK_USER");
@@ -178,6 +181,9 @@ Deno.serve(async (req) => {
// contact failed there is no note to duplicate, so a retry is safe.
// https://postmarkapp.com/developer/webhooks/inbound-webhook#errors-and-retries
const status = failedContacts.length === contacts.length ? 500 : 403;
+ if (status === 500) {
+ await removeUploadedAttachments(attachments);
+ }
return new Response(
`Could not add the note for: ${failedContacts.join(", ")}`,
{ status },