Skip to content

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

Don't duplicate companies and tags on import when existing names have duplicates

Bishop Kammeraadae3f10511c7a
diff --git a/src/components/atomic-crm/dataImport/fetchRecordsWithCache.test.ts b/src/components/atomic-crm/dataImport/fetchRecordsWithCache.test.ts
new file mode 100644
index 00000000..0b804981
--- /dev/null
+++ b/src/components/atomic-crm/dataImport/fetchRecordsWithCache.test.ts
@@ -0,0 +1,51 @@
+import type { DataProvider } from "ra-core";
+import { describe, expect, it, vi } from "vitest";
+
+import { fetchRecordsWithCache } from "./fetchRecordsWithCache";
+
+type Company = { id: number; name: string };
+
+const makeDataProvider = (records: Company[]) => {
+ let nextId = 100;
+ return {
+ getList: vi.fn(async (_resource, { filter, pagination, sort }) => {
+ const names = filter["name@in"]
+ .slice(1, -1)
+ .split(",")
+ .map((name: string) => name.slice(1, -1));
+ const matches = records
+ .filter((record) => names.includes(record.name))
+ .sort((a, b) => (sort.order === "ASC" ? a.id - b.id : b.id - a.id));
+ const start = (pagination.page - 1) * pagination.perPage;
+ return {
+ data: matches.slice(start, start + pagination.perPage),
+ total: matches.length,
+ };
+ }),
+ create: vi.fn(async (_resource, { data }) => ({
+ data: { id: nextId++, ...data },
+ })),
+ } as unknown as DataProvider;
+};
+
+describe("fetchRecordsWithCache", () => {
+ it("finds existing records when another name has duplicates", async () => {
+ const dataProvider = makeDataProvider([
+ { id: 1, name: "Acme" },
+ { id: 2, name: "Acme" },
+ { id: 3, name: "Beta" },
+ ]);
+
+ const records = await fetchRecordsWithCache<Company>(
+ "companies",
+ new Map(),
+ ["Acme", "Beta"],
+ (name) => ({ name }),
+ dataProvider,
+ );
+
+ expect(dataProvider.create).not.toHaveBeenCalled();
+ expect(records.get("Acme")?.id).toBe(1);
+ expect(records.get("Beta")?.id).toBe(3);
+ });
+});
diff --git a/src/components/atomic-crm/dataImport/fetchRecordsWithCache.ts b/src/components/atomic-crm/dataImport/fetchRecordsWithCache.ts
index 50a10773..60cf60cf 100644
--- a/src/components/atomic-crm/dataImport/fetchRecordsWithCache.ts
+++ b/src/components/atomic-crm/dataImport/fetchRecordsWithCache.ts
@@ -15,19 +15,28 @@ export const fetchRecordsWithCache = async function <T>(
const trimmedNames = [...new Set(names.map((name) => name.trim()))];
const uncachedRecordNames = trimmedNames.filter((name) => !cache.has(name));
- // check the backend for existing records
- if (uncachedRecordNames.length > 0) {
+ // check the backend for existing records, across pages since names are not
+ // unique and duplicates of one name can fill a page
+ const perPage = trimmedNames.length;
+ for (let page = 1; uncachedRecordNames.length > 0; page++) {
const response = await dataProvider.getList(resource, {
filter: {
"name@in": `(${uncachedRecordNames
.map((name) => `"${name}"`)
.join(",")})`,
},
- pagination: { page: 1, perPage: trimmedNames.length },
+ pagination: { page, perPage },
sort: { field: "id", order: "ASC" },
});
for (const record of response.data) {
- cache.set(record.name.trim(), record);
+ const name = record.name.trim();
+ if (!cache.has(name)) cache.set(name, record);
+ }
+ if (
+ response.data.length < perPage ||
+ uncachedRecordNames.every((name) => cache.has(name))
+ ) {
+ break;
}
}