A full-featured CRM built with React, shadcn/ui, and Supabase.
feat: Import data from CSV
erwanMarmelabd4cecc6b925b
diff --git a/.claude/skills/delete-initial-resource/delete-initial-resource.ts b/.claude/skills/delete-initial-resource/delete-initial-resource.tsindex 40afe07b..ee2e7c2b 100755--- a/.claude/skills/delete-initial-resource/delete-initial-resource.ts+++ b/.claude/skills/delete-initial-resource/delete-initial-resource.ts@@ -20,6 +20,10 @@ const initialResource = [] as const;const dependentFiles: ResourceFiles = {companies: [+ "src/components/atomic-crm/dataImport/useCompanyImport.ts",+ "src/components/atomic-crm/dataImport/companies_sample.csv",+ "src/components/atomic-crm/dataImport/useCompanyResolver.ts",+ "src/components/atomic-crm/dataImport/useImportableResources.ts","src/components/atomic-crm/types.ts","src/components/atomic-crm/consts.ts","src/components/atomic-crm/activity/ActivityLogCompanyCreated.tsx",@@ -75,6 +79,8 @@ const dependentFiles: ResourceFiles = {"test-data/contacts.csv",],contacts: [+ "src/components/atomic-crm/dataImport/useImportableResources.ts",+ "src/components/atomic-crm/dataImport/useCompanyResolver.ts","src/components/atomic-crm/types.ts","src/components/atomic-crm/consts.ts","src/components/atomic-crm/activity/ActivityLogContactCreated.tsx",@@ -144,6 +150,9 @@ const dependentFiles: ResourceFiles = {"src/test/StoryWrapper.tsx",],deals: [+ "src/components/atomic-crm/dataImport/useDealImport.ts",+ "src/components/atomic-crm/dataImport/deals_sample.csv",+ "src/components/atomic-crm/dataImport/useImportableResources.ts","src/components/atomic-crm/types.ts","src/components/atomic-crm/consts.ts","src/components/atomic-crm/dashboard/DealsChart.tsx",diff --git a/AGENTS.md b/AGENTS.mdindex 9b8fb88d..ce4d1868 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -160,8 +160,8 @@ When modifying contact or company data structures:1. Edit the relevant schema file in `supabase/schemas/` (table in `01_tables.sql`, views in `03_views.sql`, etc.)2. Generate a migration: `npx supabase db diff --local -f <name>`3. Apply it: `npx supabase migration up --local`-4. Update the sample CSV: `src/components/atomic-crm/contacts/contacts_export.csv`-5. Update the import function: `src/components/atomic-crm/contacts/useContactImport.tsx`+4. Update the CSV sample of the resource: `src/components/atomic-crm/contacts/contacts_export.csv`, `src/components/atomic-crm/dataImport/companies_sample.csv` or `src/components/atomic-crm/dataImport/deals_sample.csv`+5. Update the matching import function: `src/components/atomic-crm/contacts/useContactImport.tsx`, `src/components/atomic-crm/dataImport/useCompanyImport.ts` or `src/components/atomic-crm/dataImport/useDealImport.ts`6. If using FakeRest, update data generators in `src/components/atomic-crm/providers/fakerest/dataGenerator/`7. Don't forget to update the related view (`contacts_summary`, `companies_summary`) in `03_views.sql`8. Don't forget the export functionsdiff --git a/doc/src/content/docs/users/import-data.mdx b/doc/src/content/docs/users/import-data.mdxindex b2d952a8..f7119a24 100644--- a/doc/src/content/docs/users/import-data.mdx+++ b/doc/src/content/docs/users/import-data.mdx@@ -1,17 +1,22 @@---title: Importing and Exporting Data-description: This document explains how to import and export contacts in Atomic CRM.+description: This document explains how to import and export data in Atomic CRM.---Whether you are starting to use Atomic CRM, or want to migrate from another CRM, you can import your contacts, companies, tasks, notes, tags and deals in a few clicks. You can also export your contacts and companies in CSV format, and your contacts as vCard files.-## Import Contacts From CSV+## Import Data From CSV-Atomic CRM displays an import contact buttons in the initial user onboarding page, and in the contacts page. When clicking on it, a modal is displayed to upload a CSV file.+Atomic CRM imports contacts, companies and deals from CSV files. Two buttons open the same import dialog:++- **Import data**, on the initial user onboarding page, lets you choose the resource to import into.+- **Import CSV**, on the contacts, companies and deals pages, imports into that resource directly.<video src="https://github.com/user-attachments/assets/a1133cf0-0287-4fc0-b61b-5a0b88617cfc" controls className="w-full aspect-1304/814"></video>-An example of the expected CSV file is available in the contact import modal:+For each resource, the dialog offers a sample CSV file to download, showing the expected columns. Columns the CRM does not know are ignored, and columns you omit are left empty.++### Contacts```csvfirst_name,last_name,gender,title,background,first_seen,last_seen,has_newsletter,status,tags,linkedin_url,company,email_work,email_home,email_other,phone_work,phone_home,phone_other@@ -21,6 +26,26 @@ Jane,Doe,female,Designer,,2024-07-01T00:00:00+00:00,2024-07-01T11:54:49.95+00:00When importing contacts, companies and tags will be automatically matched if they exist on the system, or imported otherwise.+### Companies++```csv+name,sector,size,linkedin_url,website,phone_number,address,zipcode,city,state_abbr,country,description,revenue,tax_identifier+Acme,Information Technology,50,https://www.linkedin.com/company/acme,https://acme.example,555-0100,1 Main Street,10001,New York,NY,USA,Long-time partner on print projects,5M,US-123456789+Globex,Industrials,250,,https://globex.example,555-0111,42 Industrial Road,60601,Chicago,IL,USA,,20M,+```++The `name` column is required. `sector` is matched against the company sectors configured in your CRM, either by stored value or by the label displayed in the app; a sector matching none of them is left empty.++### Deals++```csv+name,company,category,stage,amount,expected_closing_date,description+New website,Acme,Website design,Opportunity,12000,2026-09-30,Redesign of the public website+Print campaign,Globex,Print project,Proposal Sent,4500,2026-10-15,+```++The `name` column is required. The `company` column holds a company name: an existing company is reused, an unknown one is created. `category` and `stage` are matched against the deal categories and stages configured in your CRM, by stored value or displayed label. Since a deal always belongs to a stage, an empty or unrecognized `stage` falls back to the first stage of your pipeline.+## Export Contacts To CSVIn the contacts and companies pages, an export button allows to download the list of contacts or companies in CSV format.@@ -153,7 +178,7 @@ Atomic CRM allows to import contacts, companies, tasks, notes, tags and deals fr}```-To display the import page, click on the User menu in the top right corner, and click on "Import data".+To display the import page, click on the User menu in the top right corner, and click on "Import from JSON".diff --git a/e2e/onboarding.spec.ts b/e2e/onboarding.spec.tsindex 7786268e..43342d18 100644--- a/e2e/onboarding.spec.ts+++ b/e2e/onboarding.spec.ts@@ -18,8 +18,9 @@ test("user onboarding", async ({ page, isMobile, menu, dismissToast }) => {await expect(page.getByText("Install Atomic CRM")).toBeVisible();await expect(page.getByText("Add your first contact")).toBeVisible();await expect(page.getByText("Add your first note")).toBeVisible();+ await expect(page.getByRole("button", { name: "Import data" })).toBeVisible();- await page.getByText("New Contact").click();+ await page.getByText("Add contact").click();await page.waitForLoadState("networkidle");await page.getByLabel("She/Her").click();await page.getByLabel("First name").fill("Jane");diff --git a/registry.json b/registry.jsonindex a8337a71..5d3399e1 100644--- a/registry.json+++ b/registry.json@@ -481,6 +481,10 @@"path": "src/components/atomic-crm/misc/ActiveFilterButton.tsx","type": "registry:component"},+ {+ "path": "src/components/atomic-crm/login/authConfig.ts",+ "type": "registry:component"+ },{"path": "src/components/atomic-crm/login/StartPage.tsx","type": "registry:component"@@ -601,6 +605,42 @@"path": "src/components/atomic-crm/deals/ContactList.tsx","type": "registry:component"},+ {+ "path": "src/components/atomic-crm/dataImport/useImportableResources.ts",+ "type": "registry:component"+ },+ {+ "path": "src/components/atomic-crm/dataImport/useDealImport.ts",+ "type": "registry:component"+ },+ {+ "path": "src/components/atomic-crm/dataImport/useCompanyResolver.ts",+ "type": "registry:component"+ },+ {+ "path": "src/components/atomic-crm/dataImport/useCompanyImport.ts",+ "type": "registry:component"+ },+ {+ "path": "src/components/atomic-crm/dataImport/types.ts",+ "type": "registry:component"+ },+ {+ "path": "src/components/atomic-crm/dataImport/parseCell.ts",+ "type": "registry:component"+ },+ {+ "path": "src/components/atomic-crm/dataImport/fetchRecordsWithCache.ts",+ "type": "registry:component"+ },+ {+ "path": "src/components/atomic-crm/dataImport/DataImportDialog.tsx",+ "type": "registry:component"+ },+ {+ "path": "src/components/atomic-crm/dataImport/DataImportButton.tsx",+ "type": "registry:component"+ },{"path": "src/components/atomic-crm/dashboard/Welcome.tsx","type": "registry:component"@@ -697,10 +737,6 @@"path": "src/components/atomic-crm/contacts/ContactInputs.tsx","type": "registry:component"},- {- "path": "src/components/atomic-crm/contacts/ContactImportButton.tsx",- "type": "registry:component"- },{"path": "src/components/atomic-crm/contacts/ContactEmpty.tsx","type": "registry:component"diff --git a/src/components/atomic-crm/companies/CompanyEmpty.tsx b/src/components/atomic-crm/companies/CompanyEmpty.tsxindex 14b75ed5..9af709ee 100644--- a/src/components/atomic-crm/companies/CompanyEmpty.tsx+++ b/src/components/atomic-crm/companies/CompanyEmpty.tsx@@ -1,6 +1,7 @@import { CreateButton } from "@/components/admin/create-button";import { useTranslate } from "ra-core";+import { DataImportButton } from "../dataImport/DataImportButton";import useAppBarHeight from "../misc/useAppBarHeight";export const CompanyEmpty = () => {@@ -33,6 +34,7 @@ export const CompanyEmpty = () => {</div><div className="flex space-x-2"><CreateButton label="resources.companies.action.create" />+ <DataImportButton resource="companies" /></div></div>);diff --git a/src/components/atomic-crm/companies/CompanyList.tsx b/src/components/atomic-crm/companies/CompanyList.tsxindex b6c5009f..51400e31 100644--- a/src/components/atomic-crm/companies/CompanyList.tsx+++ b/src/components/atomic-crm/companies/CompanyList.tsx@@ -5,6 +5,7 @@ import { List } from "@/components/admin/list";import { ListPagination } from "@/components/admin/list-pagination";import { SortButton } from "@/components/admin/sort-button";+import { DataImportButton } from "../dataImport/DataImportButton";import { TopToolbar } from "../layout/TopToolbar";import { CompanyEmpty } from "./CompanyEmpty";import { CompanyListFilter } from "./CompanyListFilter";@@ -48,6 +49,7 @@ const CompanyListActions = () => {return (<TopToolbar><SortButton fields={["name", "created_at", "nb_contacts"]} />+ <DataImportButton resource="companies" /><ExportButton /><CreateButtonlabel={translate("resources.companies.action.new", {diff --git a/src/components/atomic-crm/companies/sizes.ts b/src/components/atomic-crm/companies/sizes.tsindex a4495de3..a382919b 100644--- a/src/components/atomic-crm/companies/sizes.ts+++ b/src/components/atomic-crm/companies/sizes.ts@@ -5,3 +5,15 @@ export const sizes = [{ id: 250, name: "50-249 employees" },{ id: 500, name: "250 or more employees" },];++/**+ * Coerces an arbitrary imported headcount into one of the sizes above, which is+ * the only set the company forms and filters can render.+ */+export const mapSizeToCategory = (size: number): 1 | 10 | 50 | 250 | 500 => {+ if (size === 1) return 1;+ if (size < 10) return 10;+ if (size < 50) return 50;+ if (size < 250) return 250;+ return 500;+};diff --git a/src/components/atomic-crm/contacts/ContactEmpty.tsx b/src/components/atomic-crm/contacts/ContactEmpty.tsxindex d926cf8b..50919cf9 100644--- a/src/components/atomic-crm/contacts/ContactEmpty.tsx+++ b/src/components/atomic-crm/contacts/ContactEmpty.tsx@@ -5,7 +5,7 @@ import { Plus } from "lucide-react";import { useTranslate } from "ra-core";import useAppBarHeight from "../misc/useAppBarHeight";-import { ContactImportButton } from "./ContactImportButton";+import { DataImportButton } from "../dataImport/DataImportButton";import { ContactCreateSheet } from "./ContactCreateSheet";import { useIsMobile } from "@/hooks/use-mobile";@@ -48,7 +48,7 @@ export const ContactEmpty = () => {) : (<><CreateButton label="resources.contacts.action.new" />- <ContactImportButton />+ <DataImportButton resource="contacts" /></>)}</div>diff --git a/src/components/atomic-crm/contacts/ContactList.test.tsx b/src/components/atomic-crm/contacts/ContactList.test.tsxindex 695c2412..31e80f70 100644--- a/src/components/atomic-crm/contacts/ContactList.test.tsx+++ b/src/components/atomic-crm/contacts/ContactList.test.tsx@@ -128,6 +128,19 @@ describe("ContactList", () => {// close the notificationawait screen.getByRole("button", { name: /close/i }).click();});++ it("imports contacts from a dialog with no resource to pick", async () => {+ const screen = await render(<DesktopEmpty />);++ await screen.getByRole("button", { name: "Import CSV" }).click();++ await expect+ .element(screen.getByRole("heading", { name: "Import contacts" }))+ .toBeVisible();+ await expect+ .element(screen.getByLabelText("Resource"))+ .not.toBeInTheDocument();+ });});const getSelectionCheckboxes = (container: HTMLElement) =>diff --git a/src/components/atomic-crm/contacts/ContactList.tsx b/src/components/atomic-crm/contacts/ContactList.tsxindex cc897151..8854e5c0 100644--- a/src/components/atomic-crm/contacts/ContactList.tsx+++ b/src/components/atomic-crm/contacts/ContactList.tsx@@ -19,7 +19,7 @@ import { Card } from "@/components/ui/card";import type { Company, Contact, Sale, Tag } from "../types";import { BulkTagButton } from "./BulkTagButton";import { ContactEmpty } from "./ContactEmpty";-import { ContactImportButton } from "./ContactImportButton";+import { DataImportButton } from "../dataImport/DataImportButton";import {ContactListContent,ContactListContentMobile,@@ -87,7 +87,7 @@ const ContactBulkActionButtons = () => (const ContactListActions = () => (<TopToolbar><SortButton fields={["first_name", "last_name", "last_seen"]} />- <ContactImportButton />+ <DataImportButton resource="contacts" /><ExportButton exporter={exporter} /><CreateButton /></TopToolbar>diff --git a/src/components/atomic-crm/contacts/useContactImport.tsx b/src/components/atomic-crm/contacts/useContactImport.tsxindex 3e1aa53d..f2145333 100644--- a/src/components/atomic-crm/contacts/useContactImport.tsx+++ b/src/components/atomic-crm/contacts/useContactImport.tsx@@ -1,7 +1,9 @@-import { useDataProvider, useGetIdentity, type DataProvider } from "ra-core";+import { useDataProvider, useGetIdentity } from "ra-core";import { useCallback, useMemo } from "react";-import type { Company, Tag } from "../types";+import type { Tag } from "../types";+import { fetchRecordsWithCache } from "../dataImport/fetchRecordsWithCache";+import { useCompanyResolver } from "../dataImport/useCompanyResolver";export type ContactImportSchema = {first_name: string;@@ -30,28 +32,7 @@ export function useContactImport() {const user = useGetIdentity();const dataProvider = useDataProvider();- // company cache to avoid creating the same company multiple times and costly roundtrips- // Cache is dependent of dataProvider, so it's safe to use it as a dependency- const companiesCache = useMemo(- () => new Map<string, Company>(),- // eslint-disable-next-line react-hooks/exhaustive-deps- [dataProvider],- );- const getCompanies = useCallback(- async (names: string[]) =>- fetchRecordsWithCache<Company>(- "companies",- companiesCache,- names,- (name) => ({- name,- created_at: new Date().toISOString(),- sales_id: user?.identity?.id,- }),- dataProvider,- ),- [companiesCache, user?.identity?.id, dataProvider],- );+ const getCompanies = useCompanyResolver();// Tags cache to avoid creating the same tag multiple times and costly roundtrips// Cache is dependent of dataProvider, so it's safe to use it as a dependency@@ -155,50 +136,6 @@ export function useContactImport() {return processBatch;}-const fetchRecordsWithCache = async function <T>(- resource: string,- cache: Map<string, T>,- names: string[],- getCreateData: (name: string) => Partial<T>,- dataProvider: DataProvider,-) {- 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) {- const response = await dataProvider.getList(resource, {- filter: {- "name@in": `(${uncachedRecordNames- .map((name) => `"${name}"`)- .join(",")})`,- },- pagination: { page: 1, perPage: trimmedNames.length },- sort: { field: "id", order: "ASC" },- });- for (const record of response.data) {- cache.set(record.name.trim(), record);- }- }-- // create missing records in parallel- await Promise.all(- uncachedRecordNames.map(async (name) => {- if (cache.has(name)) return;- const response = await dataProvider.create(resource, {- data: getCreateData(name),- });- cache.set(name, response.data);- }),- );-- // now all records are in cache, return a map of all records- return trimmedNames.reduce((acc, name) => {- acc.set(name, cache.get(name) as T);- return acc;- }, new Map<string, T>());-};-const parseTags = (tags: string) =>tags?.split(",")diff --git a/src/components/atomic-crm/dashboard/DashboardStepper.stories.tsx b/src/components/atomic-crm/dashboard/DashboardStepper.stories.tsxnew file mode 100644index 00000000..c94c911e--- /dev/null+++ b/src/components/atomic-crm/dashboard/DashboardStepper.stories.tsx@@ -0,0 +1,19 @@+import type { Meta } from "@storybook/react-vite";++import { StoryWrapper } from "@/test/StoryWrapper";+import { DashboardStepper } from "./DashboardStepper";++const meta = {+ title: "Atomic CRM/Dashboard/Dashboard Stepper",+ parameters: {+ layout: "fullscreen",+ },+} satisfies Meta;++export default meta;++export const FirstStep = () => (+ <StoryWrapper>+ <DashboardStepper step={1} />+ </StoryWrapper>+);diff --git a/src/components/atomic-crm/dashboard/DashboardStepper.test.tsx b/src/components/atomic-crm/dashboard/DashboardStepper.test.tsxnew file mode 100644index 00000000..6bbf5142--- /dev/null+++ b/src/components/atomic-crm/dashboard/DashboardStepper.test.tsx@@ -0,0 +1,18 @@+import { describe, expect, it } from "vitest";+import { render } from "vitest-browser-react";++import { FirstStep } from "./DashboardStepper.stories";++describe("DashboardStepper", () => {+ it("opens the data import dialog from the onboarding header", async () => {+ const screen = await render(<FirstStep />);++ await expect+ .element(screen.getByRole("heading", { name: "What's next?" }))+ .toBeVisible();++ await screen.getByRole("button", { name: "Import data" }).click();++ await expect.element(screen.getByLabelText("Resource")).toBeVisible();+ });+});diff --git a/src/components/atomic-crm/dashboard/DashboardStepper.tsx b/src/components/atomic-crm/dashboard/DashboardStepper.tsxindex 7c6b552f..fbcab5d4 100644--- a/src/components/atomic-crm/dashboard/DashboardStepper.tsx+++ b/src/components/atomic-crm/dashboard/DashboardStepper.tsx@@ -1,4 +1,3 @@-import { CreateButton } from "@/components/admin/create-button";import { Button } from "@/components/ui/button";import { Card, CardContent } from "@/components/ui/card";import { Progress } from "@/components/ui/progress";@@ -10,7 +9,7 @@ import { Link } from "react-router";import { useIsMobile } from "@/hooks/use-mobile";import { ContactCreateSheet } from "../contacts/ContactCreateSheet";-import { ContactImportButton } from "../contacts/ContactImportButton";+import { DataImportButton } from "../dataImport/DataImportButton";import useAppBarHeight from "../misc/useAppBarHeight";import { NoteCreateSheet } from "../notes/NoteCreateSheet";@@ -26,6 +25,9 @@ export const DashboardStepper = ({const isMobile = useIsMobile();const [contactCreateOpen, setContactCreateOpen] = useState(false);const [noteCreateOpen, setNoteCreateOpen] = useState(false);+ const addContactLabel = translate("resources.contacts.action.add", {+ _: "Add contact",+ });return (<><ContactCreateSheet@@ -45,12 +47,15 @@ export const DashboardStepper = ({><Card className="w-full max-w-[600px]"><CardContent className="px-6">- <div className="flex items-center justify-between mb-8">- <h3 className="text-lg font-bold">- {translate("crm.dashboard.stepper.whats_next", {- _: "What's next?",- })}- </h3>+ <div className="flex flex-wrap items-center justify-between gap-y-4 mb-8">+ <div className="flex items-center gap-4">+ <h3 className="text-lg font-bold">+ {translate("crm.dashboard.stepper.whats_next", {+ _: "What's next?",+ })}+ </h3>+ <DataImportButton />+ </div><div className="w-[150px]"><Progress value={(step / 3) * 100} className="mb-2" /><div className="text-right text-sm">@@ -88,22 +93,17 @@ export const DashboardStepper = ({{isMobile ? (<ButtononClick={() => setContactCreateOpen(true)}- className="gap-2"- variant="outline"+ className="w-fit gap-2"><Plus className="h-4 w-4" />- {translate("resources.contacts.action.new", {- _: "New Contact",- })}+ {addContactLabel}</Button>) : (- <>- <CreateButton- label="resources.contacts.action.new"- resource="contacts"- />- <ContactImportButton />- </>+ <Button asChild className="w-fit">+ <Link role="button" to="/contacts/create">+ {addContactLabel}+ </Link>+ </Button>)}</div></div>diff --git a/src/components/atomic-crm/dataImport/DataImportButton.stories.tsx b/src/components/atomic-crm/dataImport/DataImportButton.stories.tsxnew file mode 100644index 00000000..23f3e694--- /dev/null+++ b/src/components/atomic-crm/dataImport/DataImportButton.stories.tsx@@ -0,0 +1,30 @@+import type { Meta } from "@storybook/react-vite";++import { StoryWrapper } from "@/test/StoryWrapper";+import { DataImportButton } from "./DataImportButton";+import type { ImportableResourceName } from "./useImportableResources";++const meta = {+ title: "Atomic CRM/Data Import/Data Import Button",+ parameters: {+ layout: "fullscreen",+ },+} satisfies Meta;++export default meta;++export const AllResources = () => (+ <StoryWrapper>+ <DataImportButton />+ </StoryWrapper>+);++export const SingleResource = ({+ resource,+}: {+ resource: ImportableResourceName;+}) => (+ <StoryWrapper>+ <DataImportButton resource={resource} />+ </StoryWrapper>+);diff --git a/src/components/atomic-crm/dataImport/DataImportButton.test.tsx b/src/components/atomic-crm/dataImport/DataImportButton.test.tsxnew file mode 100644index 00000000..75bee77f--- /dev/null+++ b/src/components/atomic-crm/dataImport/DataImportButton.test.tsx@@ -0,0 +1,190 @@+import { describe, expect, it } from "vitest";+import { render } from "vitest-browser-react";+import { useState } from "react";++import { createDataProvider } from "@/components/atomic-crm/providers/fakerest";+import { createCrmDb, StoryWrapper } from "@/test/StoryWrapper";+import type { DataProvider } from "ra-core";+import { AllResources, SingleResource } from "./DataImportButton.stories";+import type { ImportRow, ProcessImportBatch } from "./types";+import { useCompanyImport } from "./useCompanyImport";+import { useDealImport } from "./useDealImport";++const listAll = (dataProvider: DataProvider, resource: string) =>+ dataProvider.getList(resource, {+ filter: {},+ pagination: { page: 1, perPage: 25 },+ sort: { field: "id", order: "ASC" },+ });++/** Imports one batch on click, so a test controls when the import starts. */+const ImportHarness = ({+ rows,+ useImport,+}: {+ rows: ImportRow[];+ useImport: () => ProcessImportBatch;+}) => {+ const processBatch = useImport();+ const [status, setStatus] = useState("ready");+ return (+ <>+ <button+ onClick={() => {+ setStatus("running");+ processBatch(rows).then(+ () => setStatus("imported"),+ () => setStatus("failed"),+ );+ }}+ >+ run import+ </button>+ <span>{status}</span>+ </>+ );+};++/** Renders the harness on its own data provider, so each test owns its records. */+const renderImport = async (+ useImport: () => ProcessImportBatch,+ rows: ImportRow[],+) => {+ const dataProvider = createDataProvider({+ db: createCrmDb(),+ latency: 0,+ silent: true,+ });+ const screen = await render(+ <StoryWrapper dataProvider={dataProvider}>+ <ImportHarness useImport={useImport} rows={rows} />+ </StoryWrapper>,+ );+ return { dataProvider, screen };+};++describe("DataImportButton", () => {+ it("offers every importable resource in the dialog", async () => {+ const screen = await render(<AllResources />);++ await screen.getByRole("button", { name: "Import data" }).click();++ await expect+ .element(screen.getByRole("heading", { name: "Import data" }))+ .toBeVisible();++ await screen.getByLabelText("Resource").click();+ const options = screen.getByRole("listbox");++ await expect.element(options.getByText("Contacts")).toBeVisible();+ await expect.element(options.getByText("Companies")).toBeVisible();+ await expect.element(options.getByText("Deals")).toBeVisible();+ });++ it.each([+ ["contacts", "Import contacts"],+ ["companies", "Import companies"],+ ["deals", "Import deals"],+ ] as const)(+ "imports %s from a dialog with no resource to pick",+ async (resource, heading) => {+ const screen = await render(<SingleResource resource={resource} />);++ await screen.getByRole("button", { name: "Import CSV" }).click();++ await expect+ .element(screen.getByRole("heading", { name: heading }))+ .toBeVisible();+ await expect+ .element(screen.getByLabelText("Resource"))+ .not.toBeInTheDocument();+ },+ );++ it("offers the sample CSV of the imported resource", async () => {+ const screen = await render(<SingleResource resource="deals" />);++ await screen.getByRole("button", { name: "Import CSV" }).click();++ await expect+ .element(screen.getByRole("link", { name: "Download CSV sample" }))+ .toHaveAttribute("download", "crm_deals_sample.csv");+ });++ it("imports companies, mapping sector labels and numeric columns", async () => {+ const { dataProvider, screen } = await renderImport(useCompanyImport, [+ {+ name: "Acme",+ sector: "Information Technology",+ size: 50,+ city: "New York",+ website: "https://acme.example",+ },+ { name: "Globex", sector: "Not a sector", size: null, city: null },+ ]);++ await screen.getByRole("button", { name: "run import" }).click();+ await expect.element(screen.getByText("imported")).toBeVisible();++ const { data: companies } = await listAll(dataProvider, "companies");+ expect(companies).toHaveLength(2);+ expect(companies[0]).toMatchObject({+ city: "New York",+ name: "Acme",+ sector: "information-technology",+ size: 50,+ website: "https://acme.example",+ });+ // An unknown sector is dropped rather than stored as a free-text value+ expect(companies[1].sector).toBeUndefined();+ expect(companies[1].size).toBeUndefined();+ });++ it("coerces an arbitrary company size into a renderable bucket", async () => {+ const { dataProvider, screen } = await renderImport(useCompanyImport, [+ { name: "Acme", size: 42 },+ ]);++ await screen.getByRole("button", { name: "run import" }).click();+ await expect.element(screen.getByText("imported")).toBeVisible();++ const { data: companies } = await listAll(dataProvider, "companies");+ expect(companies[0].size).toBe(50);+ });++ it("imports deals, reusing one company and defaulting a missing stage", async () => {+ const { dataProvider, screen } = await renderImport(useDealImport, [+ {+ name: "New website",+ company: "Acme",+ category: "Website design",+ stage: "Proposal Sent",+ amount: "12000",+ expected_closing_date: "2026-09-30",+ },+ { name: "Print campaign", company: "Acme", stage: null },+ ]);++ await screen.getByRole("button", { name: "run import" }).click();+ await expect.element(screen.getByText("imported")).toBeVisible();++ const { data: companies } = await listAll(dataProvider, "companies");+ expect(companies).toHaveLength(1);+ expect(companies[0].name).toBe("Acme");++ const { data: deals } = await listAll(dataProvider, "deals");+ expect(deals).toHaveLength(2);+ expect(deals[0]).toMatchObject({+ amount: 12000,+ category: "website-design",+ company_id: companies[0].id,+ name: "New website",+ stage: "proposal-sent",+ });+ expect(deals[0].expected_closing_date).toBe("2026-09-30T00:00:00.000Z");+ // Both rows name the same company, which is created once and shared+ expect(deals[1].company_id).toBe(companies[0].id);+ // stage is required, so an empty cell falls back to the first stage+ expect(deals[1].stage).toBe("opportunity");+ });+});diff --git a/src/components/atomic-crm/dataImport/DataImportButton.tsx b/src/components/atomic-crm/dataImport/DataImportButton.tsxnew file mode 100644index 00000000..c154e770--- /dev/null+++ b/src/components/atomic-crm/dataImport/DataImportButton.tsx@@ -0,0 +1,49 @@+import { useState } from "react";+import { Upload } from "lucide-react";+import { useTranslate } from "ra-core";+import { Button } from "@/components/ui/button";++import { DataImportDialog } from "./DataImportDialog";+import type { ImportableResourceName } from "./useImportableResources";+import { useImportableResources } from "./useImportableResources";++/**+ * Imports a CSV file into the CRM. Given a `resource`, it imports into that one+ * and the dialog opens without a resource dropdown; otherwise the dialog offers+ * every importable resource.+ */+export const DataImportButton = ({+ resource,+}: {+ resource?: ImportableResourceName;+}) => {+ const translate = useTranslate();+ const [dialogOpen, setDialogOpen] = useState(false);+ const importableResources = useImportableResources();+ const resources = resource+ ? importableResources.filter(({ name }) => name === resource)+ : importableResources;++ // The resource may not be registered in this Admin, e.g. deals on mobile+ if (!resources.length) return null;++ return (+ <>+ <Button+ variant="outline"+ onClick={() => setDialogOpen(true)}+ className="flex items-center gap-2 cursor-pointer"+ >+ <Upload />{" "}+ {translate(+ resource ? "crm.data_import.button" : "crm.data_import.title",+ )}+ </Button>+ <DataImportDialog+ open={dialogOpen}+ onClose={() => setDialogOpen(false)}+ resources={resources}+ />+ </>+ );+};diff --git a/src/components/atomic-crm/contacts/ContactImportButton.tsx b/src/components/atomic-crm/dataImport/DataImportDialog.tsxsimilarity index 54%rename from src/components/atomic-crm/contacts/ContactImportButton.tsxrename to src/components/atomic-crm/dataImport/DataImportDialog.tsxindex ce6e5d65..f152bee0 100644--- a/src/components/atomic-crm/contacts/ContactImportButton.tsx+++ b/src/components/atomic-crm/dataImport/DataImportDialog.tsx@@ -1,7 +1,13 @@import { useEffect, useState } from "react";import type { MouseEvent } from "react";-import { Upload, Loader2 } from "lucide-react";-import { Form, useRefresh, useTranslate } from "ra-core";+import { Loader2 } from "lucide-react";+import {+ Form,+ useGetResourceLabel,+ useRefresh,+ useResourceTranslation,+ useTranslate,+} from "ra-core";import { Link } from "react-router";import { Alert, AlertDescription } from "@/components/ui/alert";import { Button } from "@/components/ui/button";@@ -11,63 +17,54 @@ import {DialogHeader,DialogTitle,} from "@/components/ui/dialog";+import { Label } from "@/components/ui/label";+import {+ Select,+ SelectContent,+ SelectItem,+ SelectTrigger,+ SelectValue,+} from "@/components/ui/select";import { FormToolbar } from "@/components/admin/simple-form";import { FileInput } from "@/components/admin/file-input";import { FileField } from "@/components/admin/file-field";import { usePapaParse } from "../misc/usePapaParse";-import type { ContactImportSchema } from "./useContactImport";-import { useContactImport } from "./useContactImport";-import * as sampleCsv from "./contacts_export.csv?raw";--export const ContactImportButton = () => {- const translate = useTranslate();- const [modalOpen, setModalOpen] = useState(false);-- const handleOpenModal = () => {- setModalOpen(true);- };-- const handleCloseModal = () => {- setModalOpen(false);- };+import type { ImportableResource, ImportRow } from "./types";- return (- <>- <Button- variant="outline"- onClick={handleOpenModal}- className="flex items-center gap-2 cursor-pointer"- >- <Upload /> {translate("resources.contacts.import.button")}- </Button>- <ContactImportDialog open={modalOpen} onClose={handleCloseModal} />- </>- );-};--const SAMPLE_URL = `data:text/csv;name=crm_contacts_sample.csv;charset=utf-8,${encodeURIComponent(- sampleCsv.default,-)}`;--type ContactImportModalProps = {+type DataImportDialogProps = {open: boolean;onClose(): void;+ /** Resources offered in the dropdown. A single one hides the dropdown. */+ resources: ImportableResource[];};-export function ContactImportDialog({+export function DataImportDialog({open,onClose,-}: ContactImportModalProps) {+ resources,+}: DataImportDialogProps) {const translate = useTranslate();+ const getResourceLabel = useGetResourceLabel();const refresh = useRefresh();- const processBatch = useContactImport();- const { importer, parseCsv, reset } = usePapaParse<ContactImportSchema>({+ const [resource, setResource] = useState(resources[0]);+ const [file, setFile] = useState<File | null>(null);++ // Importing a single resource names the dialog after it, falling back to the+ // generic heading for a resource that has no title of its own.+ const title = useResourceTranslation({+ resourceI18nKey:+ resources.length === 1+ ? `resources.${resource.name}.import.title`+ : undefined,+ baseI18nKey: "crm.data_import.title",+ });+ const { importer, parseCsv, reset } = usePapaParse<ImportRow>({batchSize: 10,- processBatch,+ processBatch: resource.processBatch,});- const [file, setFile] = useState<File | null>(null);+ const sampleUrl = `data:text/csv;name=${sampleFileName(resource.name)};charset=utf-8,${encodeURIComponent(resource.sampleCsv)}`;useEffect(() => {if (importer.state === "complete") {@@ -75,6 +72,13 @@ export function ContactImportDialog({}}, [importer.state, refresh]);+ const handleResourceChange = (name: string) => {+ const next = resources.find((candidate) => candidate.name === name);+ if (!next) return;+ setFile(null);+ setResource(next);+ };+const handleFileChange = (file: File | null) => {setFile(file);};@@ -86,6 +90,7 @@ export function ContactImportDialog({const handleClose = () => {reset();+ setFile(null);onClose();};@@ -96,26 +101,49 @@ export function ContactImportDialog({return (<Dialog open={open} onOpenChange={handleClose}>- <DialogContent className="max-w-2xl">- <Form className="flex flex-col gap-4">+ <DialogContent className="max-w-[calc(100%-2rem)] gap-6 p-6 sm:max-w-2xl sm:p-8">+ {/* Remount the form on resource change so no file survives the switch */}+ <Form key={resource.name} className="flex flex-col gap-6"><DialogHeader>- <DialogTitle>- {translate("resources.contacts.import.title")}- </DialogTitle>+ <DialogTitle>{title}</DialogTitle></DialogHeader>- <div className="flex flex-col space-y-2">+ <div className="flex flex-col space-y-6">+ {resources.length > 1 && (+ <div className="flex flex-col gap-2">+ <Label htmlFor="data-import-resource">+ {translate("crm.data_import.resource")}+ </Label>+ <Select+ value={resource.name}+ onValueChange={handleResourceChange}+ disabled={importer.state !== "idle"}+ >+ <SelectTrigger id="data-import-resource" className="w-full">+ <SelectValue />+ </SelectTrigger>+ <SelectContent>+ {resources.map(({ name }) => (+ <SelectItem key={name} value={name}>+ {getResourceLabel(name, 2)}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ </div>+ )}+{importer.state === "running" && (<div className="flex flex-col gap-2"><Alert><AlertDescription className="flex flex-row gap-4"><Loader2 className="h-5 w-5 animate-spin" />- {translate("resources.contacts.import.running")}+ {translate("crm.data_import.running")}</AlertDescription></Alert><div className="text-sm">- {translate("resources.contacts.import.progress", {+ {translate("crm.data_import.progress", {importCount: importer.importCount,rowCount: importer.rowCount,errorCount: importer.errorCount,@@ -123,9 +151,7 @@ export function ContactImportDialog({{importer.remainingTime !== null && (<>{" "}- {translate(- "resources.contacts.import.remaining_time",- )}{" "}+ {translate("crm.data_import.remaining_time")}{" "}<strong>{millisecondsToTime(importer.remainingTime)}</strong>@@ -134,7 +160,7 @@ export function ContactImportDialog({onClick={handleReset}className="text-red-600 underline hover:text-red-800">- {translate("resources.contacts.import.stop")}+ {translate("crm.data_import.stop")}</button></>)}@@ -145,7 +171,7 @@ export function ContactImportDialog({{importer.state === "error" && (<Alert variant="destructive"><AlertDescription>- {translate("resources.contacts.import.error")}+ {translate("crm.data_import.error")}</AlertDescription></Alert>)}@@ -153,7 +179,7 @@ export function ContactImportDialog({{importer.state === "complete" && (<Alert><AlertDescription>- {translate("resources.contacts.import.complete", {+ {translate("crm.data_import.complete", {importCount: importer.importCount,errorCount: importer.errorCount,})}@@ -165,21 +191,21 @@ export function ContactImportDialog({<><Alert><AlertDescription className="flex flex-col gap-4">- {translate("resources.contacts.import.sample_hint")}+ {translate("crm.data_import.sample_hint")}<Button asChild variant="outline" size="sm"><Link- to={SAMPLE_URL}- download={"crm_contacts_sample.csv"}+ to={sampleUrl}+ download={sampleFileName(resource.name)}>- {translate("resources.contacts.import.sample_download")}+ {translate("crm.data_import.sample_download")}</Link>- </Button>{" "}+ </Button></AlertDescription></Alert><FileInputsource="csv"- label="resources.contacts.import.csv_file"+ label="crm.data_import.csv_file"accept={{ "text/csv": [".csv"] }}onChange={handleFileChange}>@@ -190,11 +216,11 @@ export function ContactImportDialog({</div></Form>- <div className="flex justify-start pt-6">+ <div className="flex justify-start"><FormToolbar>{importer.state === "idle" ? (<Button onClick={startImport} disabled={!file}>- {translate("resources.contacts.import.button")}+ {translate("crm.data_import.button")}</Button>) : (<Button@@ -212,9 +238,12 @@ export function ContactImportDialog({);}-function millisecondsToTime(ms: number) {+const sampleFileName = (resourceName: string) =>+ `crm_${resourceName}_sample.csv`;++const millisecondsToTime = (ms: number) => {const seconds = Math.floor((ms / 1000) % 60);const minutes = Math.floor((ms / (60 * 1000)) % 60);return `${minutes}m ${seconds}s`;-}+};diff --git a/src/components/atomic-crm/dataImport/companies_sample.csv b/src/components/atomic-crm/dataImport/companies_sample.csvnew file mode 100644index 00000000..9ca093c7--- /dev/null+++ b/src/components/atomic-crm/dataImport/companies_sample.csv@@ -0,0 +1,3 @@+name,sector,size,linkedin_url,website,phone_number,address,zipcode,city,state_abbr,country,description,revenue,tax_identifier+Acme,Information Technology,50,https://www.linkedin.com/company/acme,https://acme.example,555-0100,1 Main Street,10001,New York,NY,USA,Long-time partner on print projects,5M,US-123456789+Globex,Industrials,250,,https://globex.example,555-0111,42 Industrial Road,60601,Chicago,IL,USA,,20M,diff --git a/src/components/atomic-crm/dataImport/deals_sample.csv b/src/components/atomic-crm/dataImport/deals_sample.csvnew file mode 100644index 00000000..a62dd7d7--- /dev/null+++ b/src/components/atomic-crm/dataImport/deals_sample.csv@@ -0,0 +1,3 @@+name,company,category,stage,amount,expected_closing_date,description+New website,Acme,Website design,Opportunity,12000,2026-09-30,Redesign of the public website+Print campaign,Globex,Print project,Proposal Sent,4500,2026-10-15,diff --git a/src/components/atomic-crm/dataImport/fetchRecordsWithCache.ts b/src/components/atomic-crm/dataImport/fetchRecordsWithCache.tsnew file mode 100644index 00000000..50a10773--- /dev/null+++ b/src/components/atomic-crm/dataImport/fetchRecordsWithCache.ts@@ -0,0 +1,50 @@+import type { DataProvider } from "ra-core";++/**+ * Resolve records by name, creating the missing ones, and memoize them in the+ * given cache to avoid creating the same record twice and to save roundtrips+ * across the batches of a single import.+ */+export const fetchRecordsWithCache = async function <T>(+ resource: string,+ cache: Map<string, T>,+ names: string[],+ getCreateData: (name: string) => Partial<T>,+ dataProvider: DataProvider,+) {+ 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) {+ const response = await dataProvider.getList(resource, {+ filter: {+ "name@in": `(${uncachedRecordNames+ .map((name) => `"${name}"`)+ .join(",")})`,+ },+ pagination: { page: 1, perPage: trimmedNames.length },+ sort: { field: "id", order: "ASC" },+ });+ for (const record of response.data) {+ cache.set(record.name.trim(), record);+ }+ }++ // create missing records in parallel+ await Promise.all(+ uncachedRecordNames.map(async (name) => {+ if (cache.has(name)) return;+ const response = await dataProvider.create(resource, {+ data: getCreateData(name),+ });+ cache.set(name, response.data);+ }),+ );++ // now all records are in cache, return a map of all records+ return trimmedNames.reduce((acc, name) => {+ acc.set(name, cache.get(name) as T);+ return acc;+ }, new Map<string, T>());+};diff --git a/src/components/atomic-crm/dataImport/parseCell.test.ts b/src/components/atomic-crm/dataImport/parseCell.test.tsnew file mode 100644index 00000000..e5c88436--- /dev/null+++ b/src/components/atomic-crm/dataImport/parseCell.test.ts@@ -0,0 +1,63 @@+import { describe, expect, it } from "vitest";++import { toConfiguredValue, toIsoDate, toNumber, toText } from "./parseCell";++const stages = [+ { value: "opportunity", label: "Opportunity" },+ { value: "proposal-sent", label: "Proposal Sent" },+];++describe("toText", () => {+ it("trims the cell content", () => {+ expect(toText(" Acme ")).toBe("Acme");+ });++ it("returns undefined for an empty or blank cell", () => {+ expect(toText(null)).toBeUndefined();+ expect(toText("")).toBeUndefined();+ expect(toText(" ")).toBeUndefined();+ });++ it("stringifies cells Papa Parse already converted", () => {+ expect(toText(10001)).toBe("10001");+ });+});++describe("toNumber", () => {+ it("converts a numeric cell", () => {+ expect(toNumber("12000")).toBe(12000);+ expect(toNumber(250)).toBe(250);+ });++ it("returns undefined for an empty or non-numeric cell", () => {+ expect(toNumber(null)).toBeUndefined();+ expect(toNumber("a lot")).toBeUndefined();+ });+});++describe("toIsoDate", () => {+ it("converts a date cell to an ISO string", () => {+ expect(toIsoDate("2026-09-30")).toBe("2026-09-30T00:00:00.000Z");+ });++ it("returns undefined for an empty or unparsable cell", () => {+ expect(toIsoDate(null)).toBeUndefined();+ expect(toIsoDate("next quarter")).toBeUndefined();+ });+});++describe("toConfiguredValue", () => {+ it("matches a configured value", () => {+ expect(toConfiguredValue("proposal-sent", stages)).toBe("proposal-sent");+ });++ it("matches a configured label, whatever its case", () => {+ expect(toConfiguredValue("Proposal Sent", stages)).toBe("proposal-sent");+ expect(toConfiguredValue("proposal sent", stages)).toBe("proposal-sent");+ });++ it("returns undefined when no option matches", () => {+ expect(toConfiguredValue("Archived", stages)).toBeUndefined();+ expect(toConfiguredValue(null, stages)).toBeUndefined();+ });+});diff --git a/src/components/atomic-crm/dataImport/parseCell.ts b/src/components/atomic-crm/dataImport/parseCell.tsnew file mode 100644index 00000000..ffd6f991--- /dev/null+++ b/src/components/atomic-crm/dataImport/parseCell.ts@@ -0,0 +1,43 @@+import type { LabeledValue } from "../types";+import type { ImportCell } from "./types";++/** Trimmed cell content, or undefined when the cell is empty. */+export const toText = (cell: ImportCell): string | undefined => {+ if (cell == null) return undefined;+ const text = String(cell).trim();+ return text === "" ? undefined : text;+};++/** Cell content as a number, or undefined when it is empty or not numeric. */+export const toNumber = (cell: ImportCell): number | undefined => {+ const text = toText(cell);+ if (text === undefined) return undefined;+ const value = Number(text);+ return Number.isFinite(value) ? value : undefined;+};++/** Cell content as an ISO 8601 date, or undefined when it is empty or invalid. */+export const toIsoDate = (cell: ImportCell): string | undefined => {+ const text = toText(cell);+ if (text === undefined) return undefined;+ const date = new Date(text);+ return Number.isNaN(date.getTime()) ? undefined : date.toISOString();+};++/**+ * Cell content matched against configured options, so a CSV may carry either the+ * stored value ("proposal-sent") or the label users see ("Proposal Sent").+ * Returns undefined when the cell is empty or matches no option.+ */+export const toConfiguredValue = (+ cell: ImportCell,+ options: LabeledValue[],+): string | undefined => {+ const text = toText(cell)?.toLowerCase();+ if (text === undefined) return undefined;+ return options.find(+ (option) =>+ option.value.toLowerCase() === text ||+ option.label.toLowerCase() === text,+ )?.value;+};diff --git a/src/components/atomic-crm/dataImport/types.ts b/src/components/atomic-crm/dataImport/types.tsnew file mode 100644index 00000000..542d9ec8--- /dev/null+++ b/src/components/atomic-crm/dataImport/types.ts@@ -0,0 +1,23 @@+import type { ImportableResourceName } from "./useImportableResources";++/**+ * A single parsed CSV cell. Papa Parse runs with `dynamicTyping`, so numeric and+ * boolean cells are already converted, an empty cell is null, and everything+ * else is a string.+ */+export type ImportCell = string | number | boolean | null;++/** One parsed CSV row, keyed by column header. */+export type ImportRow = Record<string, ImportCell>;++/** Creates the records of one batch of parsed CSV rows. */+export type ProcessImportBatch = (batch: ImportRow[]) => Promise<void>;++/** A resource the import dialog can import a CSV file into. */+export type ImportableResource = {+ /** Resource name, as registered in the Admin (e.g. "companies") */+ name: ImportableResourceName;+ /** CSV content offered as a downloadable template for this resource */+ sampleCsv: string;+ processBatch: ProcessImportBatch;+};diff --git a/src/components/atomic-crm/dataImport/useCompanyImport.ts b/src/components/atomic-crm/dataImport/useCompanyImport.tsnew file mode 100644index 00000000..a1821c84--- /dev/null+++ b/src/components/atomic-crm/dataImport/useCompanyImport.ts@@ -0,0 +1,56 @@+import { useCallback } from "react";+import { useDataProvider, useGetIdentity } from "ra-core";++import { mapSizeToCategory } from "../companies/sizes";+import { useConfigurationContext } from "../root/ConfigurationContext";+import { toConfiguredValue, toNumber, toText } from "./parseCell";+import type { ImportCell, ProcessImportBatch } from "./types";++/**+ * Creates a company per CSV row. Unknown columns are ignored, missing ones are+ * left empty — except `name`, which the database requires.+ */+export function useCompanyImport(): ProcessImportBatch {+ const { companySectors } = useConfigurationContext();+ const { identity } = useGetIdentity();+ const dataProvider = useDataProvider();++ return useCallback(+ async (batch) => {+ await Promise.all(+ batch.map((row) =>+ dataProvider.create("companies", {+ data: {+ name: toText(row.name),+ sector: toConfiguredValue(row.sector, companySectors),+ size: sizeOf(row.size),+ linkedin_url: toText(row.linkedin_url),+ website: toText(row.website),+ phone_number: toText(row.phone_number),+ address: toText(row.address),+ zipcode: toText(row.zipcode),+ city: toText(row.city),+ state_abbr: toText(row.state_abbr),+ country: toText(row.country),+ description: toText(row.description),+ revenue: toText(row.revenue),+ tax_identifier: toText(row.tax_identifier),+ sales_id: identity?.id,+ created_at: new Date().toISOString(),+ },+ }),+ ),+ );+ },+ [companySectors, dataProvider, identity?.id],+ );+}++/**+ * `size` is a bucket id, not a headcount, so an arbitrary CSV number is coerced+ * into the nearest bucket the company screens can render.+ */+const sizeOf = (cell: ImportCell) => {+ const size = toNumber(cell);+ return size === undefined ? undefined : mapSizeToCategory(size);+};diff --git a/src/components/atomic-crm/dataImport/useCompanyResolver.ts b/src/components/atomic-crm/dataImport/useCompanyResolver.tsnew file mode 100644index 00000000..e151350c--- /dev/null+++ b/src/components/atomic-crm/dataImport/useCompanyResolver.ts@@ -0,0 +1,38 @@+import { useCallback, useMemo } from "react";+import { useDataProvider, useGetIdentity } from "ra-core";++import type { Company } from "../types";+import { fetchRecordsWithCache } from "./fetchRecordsWithCache";++/**+ * Resolves companies by name, creating the missing ones. Both the contact and+ * the deal importer name companies in a text column, so they share one cache:+ * a company named twice in a CSV is created once.+ */+export function useCompanyResolver() {+ const { identity } = useGetIdentity();+ const dataProvider = useDataProvider();++ // Cache is dependent of dataProvider, so it's safe to use it as a dependency+ const companiesCache = useMemo(+ () => new Map<string, Company>(),+ // eslint-disable-next-line react-hooks/exhaustive-deps+ [dataProvider],+ );++ return useCallback(+ (names: string[]) =>+ fetchRecordsWithCache<Company>(+ "companies",+ companiesCache,+ names,+ (name) => ({+ name,+ created_at: new Date().toISOString(),+ sales_id: identity?.id,+ }),+ dataProvider,+ ),+ [companiesCache, dataProvider, identity?.id],+ );+}diff --git a/src/components/atomic-crm/dataImport/useDealImport.ts b/src/components/atomic-crm/dataImport/useDealImport.tsnew file mode 100644index 00000000..748a2e85--- /dev/null+++ b/src/components/atomic-crm/dataImport/useDealImport.ts@@ -0,0 +1,63 @@+import { useCallback } from "react";+import { useDataProvider, useGetIdentity } from "ra-core";++import { useConfigurationContext } from "../root/ConfigurationContext";+import { useCompanyResolver } from "./useCompanyResolver";+import { toConfiguredValue, toIsoDate, toNumber, toText } from "./parseCell";+import type { ProcessImportBatch } from "./types";++/**+ * Creates a deal per CSV row. Unknown columns are ignored, missing ones are+ * left empty — except `name`, which the database requires. The `company` column+ * holds a company name: matching companies are reused, unknown ones are created.+ */+export function useDealImport(): ProcessImportBatch {+ const { dealCategories, dealStages } = useConfigurationContext();+ const { identity } = useGetIdentity();+ const dataProvider = useDataProvider();+ const getCompanies = useCompanyResolver();++ return useCallback(+ async (batch) => {+ // Parse the company name once per row: it is needed to resolve the+ // companies up front, then again to link each deal to its own.+ const rows = batch.map((row) => ({+ row,+ companyName: toText(row.company),+ }));+ const companies = await getCompanies(+ rows+ .map(({ companyName }) => companyName)+ .filter((name): name is string => name !== undefined),+ );++ const now = new Date().toISOString();+ await Promise.all(+ rows.map(({ row, companyName }) => {+ return dataProvider.create("deals", {+ data: {+ name: toText(row.name),+ company_id: companyName+ ? companies.get(companyName)?.id+ : undefined,+ contact_ids: [],+ category: toConfiguredValue(row.category, dealCategories),+ // stage is required, so fall back to the first configured one+ stage:+ toConfiguredValue(row.stage, dealStages) ??+ dealStages[0]?.value,+ description: toText(row.description),+ amount: toNumber(row.amount),+ expected_closing_date: toIsoDate(row.expected_closing_date),+ sales_id: identity?.id,+ index: 0,+ created_at: now,+ updated_at: now,+ },+ });+ }),+ );+ },+ [dataProvider, dealCategories, dealStages, getCompanies, identity?.id],+ );+}diff --git a/src/components/atomic-crm/dataImport/useImportableResources.ts b/src/components/atomic-crm/dataImport/useImportableResources.tsnew file mode 100644index 00000000..1443b601--- /dev/null+++ b/src/components/atomic-crm/dataImport/useImportableResources.ts@@ -0,0 +1,45 @@+import { useResourceDefinitions } from "ra-core";++import type { ContactImportSchema } from "../contacts/useContactImport";+import { useContactImport } from "../contacts/useContactImport";+import companiesSampleCsv from "./companies_sample.csv?raw";+import dealsSampleCsv from "./deals_sample.csv?raw";+import contactsSampleCsv from "../contacts/contacts_export.csv?raw";+import type { ImportableResource } from "./types";+import { useCompanyImport } from "./useCompanyImport";+import { useDealImport } from "./useDealImport";++/** Every resource a CSV can be imported into, in the order the dialog offers them. */+export const IMPORTABLE_RESOURCES = ["contacts", "companies", "deals"] as const;++export type ImportableResourceName = (typeof IMPORTABLE_RESOURCES)[number];++/**+ * The importable resources, restricted to those the running Admin registers:+ * the mobile app has no deals screens, so it must not offer to import records+ * the user would never be able to see.+ */+export function useImportableResources(): ImportableResource[] {+ const processContacts = useContactImport();+ const processCompanies = useCompanyImport();+ const processDeals = useDealImport();+ const definitions = useResourceDefinitions();++ const resources: ImportableResource[] = [+ {+ name: "contacts",+ sampleCsv: contactsSampleCsv,+ // The contact importer predates the shared ImportRow type and declares+ // its own all-string schema; it reads the same parsed cells.+ processBatch: (batch) => processContacts(batch as ContactImportSchema[]),+ },+ {+ name: "companies",+ sampleCsv: companiesSampleCsv,+ processBatch: processCompanies,+ },+ { name: "deals", sampleCsv: dealsSampleCsv, processBatch: processDeals },+ ];++ return resources.filter(({ name }) => name in definitions);+}diff --git a/src/components/atomic-crm/deals/DealEmpty.tsx b/src/components/atomic-crm/deals/DealEmpty.tsxindex 69dcd72b..986d6b1a 100644--- a/src/components/atomic-crm/deals/DealEmpty.tsx+++ b/src/components/atomic-crm/deals/DealEmpty.tsx@@ -4,6 +4,7 @@ import type { ReactNode } from "react";import { CreateButton } from "@/components/admin/create-button";import { Progress } from "@/components/ui/progress";+import { DataImportButton } from "../dataImport/DataImportButton";import useAppBarHeight from "../misc/useAppBarHeight";import type { Contact } from "../types";import { DealCreate } from "./DealCreate";@@ -47,6 +48,7 @@ export const DealEmpty = ({ children }: { children?: ReactNode }) => {</div><div className="flex space-x-8"><CreateButton label="resources.deals.action.create" />+ <DataImportButton resource="deals" /></div><DealCreate open={!!matchCreate} />{children}diff --git a/src/components/atomic-crm/deals/DealList.tsx b/src/components/atomic-crm/deals/DealList.tsxindex 9012884b..4b8182a0 100644--- a/src/components/atomic-crm/deals/DealList.tsx+++ b/src/components/atomic-crm/deals/DealList.tsx@@ -11,6 +11,7 @@ import { FilterButton } from "@/components/admin/filter-form";import { SearchInput } from "@/components/admin/search-input";import { SelectInput } from "@/components/admin/select-input";+import { DataImportButton } from "../dataImport/DataImportButton";import { useConfigurationContext } from "../root/ConfigurationContext";import { TopToolbar } from "../layout/TopToolbar";import { DealArchivedList } from "./DealArchivedList";@@ -98,6 +99,7 @@ const DealLayout = () => {const DealActions = () => (<TopToolbar><FilterButton />+ <DataImportButton resource="deals" /><ExportButton /><CreateButton label="resources.deals.action.new" /></TopToolbar>diff --git a/src/components/atomic-crm/misc/useImportFromJson.ts b/src/components/atomic-crm/misc/useImportFromJson.tsindex 4b636e30..454469cd 100644--- a/src/components/atomic-crm/misc/useImportFromJson.ts+++ b/src/components/atomic-crm/misc/useImportFromJson.ts@@ -11,6 +11,7 @@ import mime from "mime/lite";import type { CrmDataProvider } from "../providers/types";import type { RAFile, Tag } from "../types";import { colors } from "../tags/colors";+import { mapSizeToCategory } from "../companies/sizes";import { useConfigurationContext } from "../root/ConfigurationContext";import { contactGender } from "../contacts/contactModel";@@ -806,10 +807,3 @@ const isTask = (data: any): data is TaskImport =>* Maps a company size number to the appropriate size category.* Categories: 1, 10, 50, 250, 500*/-const mapSizeToCategory = (size: number): 1 | 10 | 50 | 250 | 500 => {- if (size === 1) return 1;- if (size < 10) return 10;- if (size < 50) return 50;- if (size < 250) return 250;- return 500;-};diff --git a/src/components/atomic-crm/providers/commons/englishCrmMessages.ts b/src/components/atomic-crm/providers/commons/englishCrmMessages.tsindex de9e8443..813ef51b 100644--- a/src/components/atomic-crm/providers/commons/englishCrmMessages.ts+++ b/src/components/atomic-crm/providers/commons/englishCrmMessages.ts@@ -27,6 +27,9 @@ export const englishCrmMessages = {description: "It seems your company list is empty.",title: "No companies found",},+ import: {+ title: "Import companies",+ },field_categories: {contact: "Contact",additional_info: "Additional information",@@ -110,21 +113,6 @@ export const englishCrmMessages = {},import: {title: "Import contacts",- button: "Import CSV",- complete:- "Contacts import complete. Imported %{importCount} contacts, with %{errorCount} errors",- progress:- "Imported %{importCount} / %{rowCount} contacts, with %{errorCount} errors.",- error:- "Failed to import this file, please make sure your provided a valid CSV file.",- imported: "Imported",- remaining_time: "Estimated remaining time:",- running: "The import is running, please do not close this tab.",- sample_download: "Download CSV sample",- sample_hint: "Here is a sample CSV file you can use as a template",- stop: "Stop import",- csv_file: "CSV File",- contacts_label: "contact |||| contacts",},inputs: {genders: {@@ -232,6 +220,9 @@ export const englishCrmMessages = {description: "It seems your deal list is empty.",title: "No deals found",},+ import: {+ title: "Import deals",+ },invalid_date: "Invalid date",},notes: {@@ -438,8 +429,25 @@ export const englishCrmMessages = {},upcoming_tasks: "Upcoming Tasks",},+ data_import: {+ button: "Import CSV",+ complete:+ "Import complete. Imported %{importCount} records, with %{errorCount} errors",+ csv_file: "CSV File",+ error:+ "Failed to import this file, please make sure you provided a valid CSV file.",+ progress:+ "Imported %{importCount} / %{rowCount} records, with %{errorCount} errors.",+ remaining_time: "Estimated remaining time:",+ resource: "Resource",+ running: "The import is running, please do not close this tab.",+ sample_download: "Download CSV sample",+ sample_hint: "Here is a sample CSV file you can use as a template",+ stop: "Stop import",+ title: "Import data",+ },header: {- import_data: "Import data",+ import_data: "Import from JSON",},image_editor: {change: "Change",@@ -473,7 +481,7 @@ export const englishCrmMessages = {some_failed: "Some records were not imported.",table_caption: "Import status",},- title: "Import Data",+ title: "Import from JSON",},settings: {about: "About",diff --git a/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts b/src/components/atomic-crm/providers/commons/frenchCrmMessages.tsindex 5d30e91f..e2624815 100644--- a/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts+++ b/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts@@ -29,6 +29,9 @@ export const frenchCrmMessages = {description: "Il semble que la liste de vos entreprises soit vide.",title: "Aucune entreprise trouvée",},+ import: {+ title: "Importer des entreprises",+ },field_categories: {contact: "Contact",additional_info: "Informations supplémentaires",@@ -113,22 +116,6 @@ export const frenchCrmMessages = {},import: {title: "Importer des contacts",- button: "Importer un fichier CSV",- complete:- "Import des contacts terminé. %{importCount} contacts importés, %{errorCount} erreurs",- progress:- "%{importCount} / %{rowCount} contacts importés, avec %{errorCount} erreurs.",- error:- "Échec de l'importation de ce fichier. Veuillez vous assurer que vous avez fourni un fichier CSV valide.",- imported: "Importé",- remaining_time: "Temps restant estimé :",- running: "L'import est en cours, merci de ne pas fermer cet onglet.",- sample_download: "Télécharger un exemple CSV",- sample_hint:- "Voici un exemple de fichier CSV que vous pouvez utiliser comme modèle",- stop: "Arrêter l'importation",- csv_file: "Fichier CSV",- contacts_label: "contact |||| contacts",},inputs: {genders: {@@ -237,6 +224,9 @@ export const frenchCrmMessages = {description: "Il semble que votre liste d'affaires soit vide.",title: "Aucune affaire trouvée",},+ import: {+ title: "Importer des affaires",+ },invalid_date: "Date invalide",},notes: {@@ -445,8 +435,26 @@ export const frenchCrmMessages = {},upcoming_tasks: "Tâches à venir",},+ data_import: {+ button: "Importer un fichier CSV",+ complete:+ "Import terminé. %{importCount} enregistrements importés, %{errorCount} erreurs",+ csv_file: "Fichier CSV",+ error:+ "Échec de l'importation de ce fichier. Veuillez vous assurer que vous avez fourni un fichier CSV valide.",+ progress:+ "%{importCount} / %{rowCount} enregistrements importés, avec %{errorCount} erreurs.",+ remaining_time: "Temps restant estimé :",+ resource: "Ressource",+ running: "L'import est en cours, merci de ne pas fermer cet onglet.",+ sample_download: "Télécharger un exemple CSV",+ sample_hint:+ "Voici un exemple de fichier CSV que vous pouvez utiliser comme modèle",+ stop: "Arrêter l'importation",+ title: "Importer des données",+ },header: {- import_data: "Importer des données",+ import_data: "Importer depuis JSON",},image_editor: {change: "Changer",@@ -480,7 +488,7 @@ export const frenchCrmMessages = {some_failed: "Certains enregistrements n'ont pas été importés.",table_caption: "Statut d'importation",},- title: "Importer des données",+ title: "Importer depuis JSON",},settings: {about: "À propos",