Skip to content

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

fix: delete note attachments from storage paths

Anibal Svarcas0e827cdb59cd
diff --git a/src/components/atomic-crm/providers/supabase/dataProvider.spec.ts b/src/components/atomic-crm/providers/supabase/dataProvider.spec.ts
index bde4afb0..7747005f 100644
--- a/src/components/atomic-crm/providers/supabase/dataProvider.spec.ts
+++ b/src/components/atomic-crm/providers/supabase/dataProvider.spec.ts
@@ -114,29 +114,79 @@ describe("supabase dataProvider note attachment deletion", () => {
});
});
- it("deletes deal note attachment using src when path is missing", async () => {
+ it("deletes deal note attachment by extracting path from src", async () => {
const { dataProvider } = await import("./dataProvider");
- const attachmentSrc =
- "http://127.0.0.1:54321/storage/v1/object/public/attachments/folder/my%20report.txt?download=1";
const previousData = {
id: 2,
attachments: [
{
title: "my report.txt",
- src: attachmentSrc,
+ src: "http://127.0.0.1:54321/storage/v1/object/public/attachments/folder/my%20report.txt?download=1",
},
],
};
await dataProvider.delete("deal_notes", { id: 2, previousData } as any);
- expect(mockStorageRemove).toHaveBeenCalledWith([attachmentSrc]);
+ expect(mockStorageRemove).toHaveBeenCalledWith(["folder/my report.txt"]);
expect(mockBaseDelete).toHaveBeenCalledWith("deal_notes", {
id: 2,
previousData,
});
});
+ it("fetches note when previousData is missing and deletes its attachments", async () => {
+ const { dataProvider } = await import("./dataProvider");
+ const fetchedNote = {
+ id: 5,
+ attachments: [
+ {
+ title: "from-fetch.txt",
+ src: "http://127.0.0.1:54321/storage/v1/object/public/attachments/from-fetch.txt",
+ path: "from-fetch.txt",
+ },
+ ],
+ };
+
+ mockBaseGetOne.mockResolvedValueOnce({ data: fetchedNote });
+
+ await dataProvider.delete("contact_notes", { id: 5 } as any);
+
+ expect(mockBaseGetOne).toHaveBeenCalledWith("contact_notes", { id: 5 });
+ expect(mockStorageRemove).toHaveBeenCalledWith(["from-fetch.txt"]);
+ expect(mockBaseDelete).toHaveBeenCalledWith("contact_notes", { id: 5 });
+ });
+
+ it("deduplicates attachment paths before calling storage remove", async () => {
+ const { dataProvider } = await import("./dataProvider");
+ const duplicatePath =
+ "http://127.0.0.1:54321/storage/v1/object/public/attachments/folder/my%20report.txt?download=1";
+ const previousData = {
+ id: 8,
+ attachments: [
+ {
+ title: "duplicate-1.txt",
+ src: duplicatePath,
+ path: "dup.txt",
+ },
+ {
+ title: "duplicate-2.txt",
+ src: duplicatePath,
+ path: "dup.txt",
+ },
+ ],
+ };
+
+ await dataProvider.delete("contact_notes", { id: 8, previousData } as any);
+
+ expect(mockStorageRemove).toHaveBeenCalledTimes(1);
+ expect(mockStorageRemove).toHaveBeenCalledWith(["dup.txt"]);
+ expect(mockBaseDelete).toHaveBeenCalledWith("contact_notes", {
+ id: 8,
+ previousData,
+ });
+ });
+
it("does not call storage remove when note has no attachments", async () => {
const { dataProvider } = await import("./dataProvider");
const previousData = {
diff --git a/src/components/atomic-crm/providers/supabase/dataProvider.ts b/src/components/atomic-crm/providers/supabase/dataProvider.ts
index 4cf38c09..254531b3 100644
--- a/src/components/atomic-crm/providers/supabase/dataProvider.ts
+++ b/src/components/atomic-crm/providers/supabase/dataProvider.ts
@@ -77,7 +77,11 @@ const deleteNoteAttachments = async (
.remove(paths);
if (error) {
- console.error("deleteAttachmentsError", error);
+ console.error("deleteAttachmentsError", {
+ resource,
+ noteId: params.id,
+ error,
+ });
throw new Error("Failed to delete note attachments");
}
@@ -396,7 +400,62 @@ const extractAttachmentPath = (attachment?: Partial<RAFile> | null) => {
return null;
}
- return attachment.src;
+ const storagePath = extractPathFromStorageUrl(attachment.src);
+ if (storagePath) {
+ return storagePath;
+ }
+
+ return extractRawPath(attachment.src);
+};
+
+const extractPathFromStorageUrl = (src: string) => {
+ const pathname = getPathname(src);
+ if (!pathname) {
+ return null;
+ }
+
+ const knownPrefixes = [
+ `/storage/v1/object/public/${ATTACHMENTS_BUCKET}/`,
+ `/storage/v1/object/sign/${ATTACHMENTS_BUCKET}/`,
+ `/storage/v1/object/authenticated/${ATTACHMENTS_BUCKET}/`,
+ `/storage/v1/object/${ATTACHMENTS_BUCKET}/`,
+ ];
+
+ for (const prefix of knownPrefixes) {
+ if (pathname.startsWith(prefix)) {
+ const path = pathname.slice(prefix.length);
+ return path.length > 0 ? safelyDecodePath(path) : null;
+ }
+ }
+
+ return null;
+};
+
+const getPathname = (value: string) => {
+ try {
+ return new URL(value, "http://localhost").pathname;
+ } catch {
+ return null;
+ }
+};
+
+const extractRawPath = (src: string) => {
+ if (src.includes("://")) {
+ return null;
+ }
+
+ const pathWithoutQuery = src.split("?")[0].split("#")[0];
+ const normalizedPath = pathWithoutQuery.replace(/^\/+/, "");
+
+ return normalizedPath.length > 0 ? safelyDecodePath(normalizedPath) : null;
+};
+
+const safelyDecodePath = (path: string) => {
+ try {
+ return decodeURIComponent(path);
+ } catch {
+ return path;
+ }
};
const uploadToBucket = async (fi: RAFile) => {