A full-featured CRM built with React, shadcn/ui, and Supabase.
Improve merge logic
François Zaninottofeef3bce8f13
diff --git a/registry.json b/registry.jsonindex 246f9355..016c4855 100644--- a/registry.json+++ b/registry.json@@ -280,6 +280,10 @@"path": "src/components/atomic-crm/providers/commons/activity.ts","type": "registry:component"},+ {+ "path": "src/components/atomic-crm/providers/commons/mergeContacts.ts",+ "type": "registry:component"+ },{"path": "src/components/atomic-crm/notes/utils.ts","type": "registry:component"@@ -496,10 +500,6 @@"path": "src/components/atomic-crm/contacts/useContactImport.tsx","type": "registry:component"},- {- "path": "src/components/atomic-crm/contacts/mergeContacts.ts",- "type": "registry:component"- },{"path": "src/components/atomic-crm/contacts/index.tsx","type": "registry:component"diff --git a/src/components/atomic-crm/contacts/ContactMergeButton.tsx b/src/components/atomic-crm/contacts/ContactMergeButton.tsxindex 1d195a07..4110c431 100644--- a/src/components/atomic-crm/contacts/ContactMergeButton.tsx+++ b/src/components/atomic-crm/contacts/ContactMergeButton.tsx@@ -11,6 +11,7 @@ import {useRedirect,} from "ra-core";import type { Identifier } from "ra-core";+import { useMutation } from "@tanstack/react-query";import {Dialog,DialogContent,@@ -25,7 +26,6 @@ import { AutocompleteInput } from "@/components/admin/autocomplete-input";import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";import type { Contact } from "../types";import { contactOptionText } from "../misc/ContactOption";-import { mergeContacts } from "./mergeContacts";export const ContactMergeButton = () => {const [mergeDialogOpen, setMergeDialogOpen] = useState(false);@@ -63,6 +63,12 @@ const ContactMergeDialog = ({ open, onClose }: ContactMergeDialogProps) => {null,);const [isMerging, setIsMerging] = useState(false);+ const { mutateAsync } = useMutation({+ mutationKey: ["contacts", "merge", { loserId: loserContact?.id, winnerId }],+ mutationFn: async () => {+ return dataProvider.mergeContacts(loserContact?.id, winnerId);+ },+ });// Find potential contacts with matching first and last nameconst { data: matchingContacts } = useGetList(@@ -71,7 +77,7 @@ const ContactMergeDialog = ({ open, onClose }: ContactMergeDialogProps) => {filter: {first_name: loserContact?.first_name,last_name: loserContact?.last_name,- "id@neq": loserContact?.id, // Exclude current contact+ "id@neq": `${loserContact?.id}`, // Exclude current contact},pagination: { page: 1, perPage: 10 },},@@ -125,7 +131,7 @@ const ContactMergeDialog = ({ open, onClose }: ContactMergeDialogProps) => {try {setIsMerging(true);- await mergeContacts(loserContact.id, winnerId, dataProvider);+ await mutateAsync();setIsMerging(false);notify("Contacts merged successfully", { type: "success" });redirect(`/contacts/${winnerId}/show`);@@ -167,7 +173,7 @@ const ContactMergeDialog = ({ open, onClose }: ContactMergeDialogProps) => {<ReferenceInputsource="winner_id"reference="contacts"- filter={{ id_neq: loserContact.id }}+ filter={{ "id@neq": loserContact.id }}><AutocompleteInputlabel=""diff --git a/src/components/atomic-crm/contacts/mergeContacts.ts b/src/components/atomic-crm/providers/commons/mergeContacts.tssimilarity index 98%rename from src/components/atomic-crm/contacts/mergeContacts.tsrename to src/components/atomic-crm/providers/commons/mergeContacts.tsindex 2dc0be21..727494a0 100644--- a/src/components/atomic-crm/contacts/mergeContacts.ts+++ b/src/components/atomic-crm/providers/commons/mergeContacts.ts@@ -1,6 +1,6 @@import type { Identifier, DataProvider } from "ra-core";-import type { Contact, Task, Deal, ContactNote } from "../types";+import type { Contact, Task, Deal, ContactNote } from "../../types";/*** Merge one contact (loser) into another contact (winner).diff --git a/src/components/atomic-crm/providers/fakerest/dataProvider.ts b/src/components/atomic-crm/providers/fakerest/dataProvider.tsindex 93c74a5e..34fa98df 100644--- a/src/components/atomic-crm/providers/fakerest/dataProvider.ts+++ b/src/components/atomic-crm/providers/fakerest/dataProvider.ts@@ -20,6 +20,7 @@ import type {import { getActivityLog } from "../commons/activity";import { getCompanyAvatar } from "../commons/getCompanyAvatar";import { getContactAvatar } from "../commons/getContactAvatar";+import { mergeContacts } from "../commons/mergeContacts";import type { CrmDataProvider } from "../types";import { authProvider, USER_STORAGE_KEY } from "./authProvider";import generateData from "./dataGenerator";@@ -138,12 +139,12 @@ const dataProviderWithCustomMethod: CrmDataProvider = {getActivityLog: async (companyId?: Identifier) => {return getActivityLog(dataProvider, companyId);},- async signUp({+ signUp: async({email,password,first_name,last_name,- }: SignUpData): Promise<{ id: string; email: string; password: string }> {+ }: SignUpData): Promise<{ id: string; email: string; password: string }> => {const user = await baseDataProvider.create("sales", {data: {email,@@ -157,7 +158,7 @@ const dataProviderWithCustomMethod: CrmDataProvider = {password,};},- async salesCreate({ ...data }: SalesFormData): Promise<Sale> {+ salesCreate: async({ ...data }: SalesFormData): Promise<Sale> => {const user = await dataProvider.create("sales", {data: {...data,@@ -169,10 +170,10 @@ const dataProviderWithCustomMethod: CrmDataProvider = {...user.data,};},- async salesUpdate(+ salesUpdate: async(id: Identifier,data: Partial<Omit<SalesFormData, "password">>,- ): Promise<Partial<Omit<SalesFormData, "password">>> {+ ): Promise<Partial<Omit<SalesFormData, "password">>> => {const { data: previousData } = await dataProvider.getOne<Sale>("sales", {id,});@@ -188,7 +189,7 @@ const dataProviderWithCustomMethod: CrmDataProvider = {});return sale;},- async isInitialized(): Promise<boolean> {+ isInitialized: async(): Promise<boolean> => {const sales = await dataProvider.getList<Sale>("sales", {filter: {},pagination: { page: 1, perPage: 1 },@@ -222,6 +223,9 @@ const dataProviderWithCustomMethod: CrmDataProvider = {return true;},+ mergeContacts: async (sourceId: Identifier, targetId: Identifier) => {+ return mergeContacts(sourceId, targetId, baseDataProvider);+ }};async function updateCompany(diff --git a/src/components/atomic-crm/providers/supabase/dataProvider.ts b/src/components/atomic-crm/providers/supabase/dataProvider.tsindex 724307bd..b4e1bf1c 100644--- a/src/components/atomic-crm/providers/supabase/dataProvider.ts+++ b/src/components/atomic-crm/providers/supabase/dataProvider.ts@@ -21,6 +21,7 @@ import type {import { getActivityLog } from "../commons/activity";import { getCompanyAvatar } from "../commons/getCompanyAvatar";import { getContactAvatar } from "../commons/getContactAvatar";+import { mergeContacts } from "../commons/mergeContacts";import { getIsInitialized } from "./authProvider";import { supabase } from "./supabase";@@ -218,6 +219,10 @@ const dataProviderWithCustomMethods = {async isInitialized() {return getIsInitialized();},+ async mergeContacts (sourceId: Identifier, targetId: Identifier) {+ // FIXME this should be done in a lambda function using a transaction instead+ return mergeContacts(sourceId, targetId, baseDataProvider);+ },} satisfies DataProvider;export type CrmDataProvider = typeof dataProviderWithCustomMethods;