Skip to content

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

wip: add email type

Jean-Baptiste Kaiser7c6822bbeff8
diff --git a/src/contacts/ContactAside.tsx b/src/contacts/ContactAside.tsx
index febe7ec4..403f6cbb 100644
--- a/src/contacts/ContactAside.tsx
+++ b/src/contacts/ContactAside.tsx
@@ -9,7 +9,6 @@ import {
EditButton,
EmailField,
FunctionField,
- RecordContextProvider,
ReferenceField,
ReferenceManyField,
SelectField,
@@ -44,26 +43,29 @@ export const ContactAside = ({ link = 'edit' }: { link?: 'edit' | 'show' }) => {
</Box>
<Typography variant="subtitle2">Personal info</Typography>
<Divider sx={{ mb: 2 }} />
- <ArrayField source="email">
+ <ArrayField source="email_jsonb">
<SingleFieldList linkType={false} gap={0}>
- <WithRecord
- render={email => (
- <RecordContextProvider value={{ email }}>
- <Stack
- direction="row"
- alignItems="center"
- gap={1}
- minHeight={24}
- >
- <EmailIcon
- color="disabled"
- fontSize="small"
- />
- <EmailField source="email" />
- </Stack>
- </RecordContextProvider>
- )}
- />
+ <Stack
+ direction="row"
+ alignItems="center"
+ gap={1}
+ minHeight={24}
+ >
+ <EmailIcon color="disabled" fontSize="small" />
+ <Box>
+ <EmailField source="email" />{' '}
+ <WithRecord
+ render={row =>
+ row.type !== 'Other' && (
+ <TextField
+ source="type"
+ color="textSecondary"
+ />
+ )
+ }
+ />
+ </Box>
+ </Stack>
</SingleFieldList>
</ArrayField>
{record.has_newsletter && (
diff --git a/src/contacts/ContactInputs.tsx b/src/contacts/ContactInputs.tsx
index d77379d1..c3d29d4a 100644
--- a/src/contacts/ContactInputs.tsx
+++ b/src/contacts/ContactInputs.tsx
@@ -35,8 +35,8 @@ export const ContactInputs = () => {
return (
<Stack gap={2} p={1}>
<Avatar />
- <Stack gap={4} direction={isMobile ? 'column' : 'row'}>
- <Stack gap={4} flex={1}>
+ <Stack gap={3} direction={isMobile ? 'column' : 'row'}>
+ <Stack gap={4} flex={4}>
<ContactIdentityInputs />
<ContactPositionInputs />
</Stack>
@@ -44,7 +44,7 @@ export const ContactInputs = () => {
orientation={isMobile ? 'horizontal' : 'vertical'}
flexItem
/>
- <Stack gap={4} flex={1}>
+ <Stack gap={4} flex={5}>
<ContactPersonalInformationInputs />
<ContactMiscInputs />
</Stack>
@@ -150,15 +150,31 @@ const ContactPersonalInformationInputs = () => {
return (
<Stack>
<Typography variant="h6">Personal info</Typography>
- <ArrayInput source="email" label="Email addresses">
- <SimpleFormIterator inline>
+ <ArrayInput
+ source="email_jsonb"
+ label="Email addresses"
+ helperText={false}
+ >
+ <SimpleFormIterator inline disableReordering>
<TextInput
- source=""
+ source="email"
helperText={false}
validate={email()}
onPaste={handleEmailPaste}
onBlur={handleEmailBlur}
- hiddenLabel
+ />
+ <SelectInput
+ source="type"
+ helperText={false}
+ optionText="id"
+ choices={[
+ { id: 'Work' },
+ { id: 'Home' },
+ { id: 'Other' },
+ ]}
+ defaultValue="Work"
+ fullWidth={false}
+ sx={{ width: 100, minWidth: 100 }}
/>
</SimpleFormIterator>
</ArrayInput>
@@ -172,9 +188,11 @@ const ContactPersonalInformationInputs = () => {
source="phone_1_type"
label="Type"
helperText={false}
- optionText={choice => choice.id}
+ optionText="id"
choices={[{ id: 'Work' }, { id: 'Home' }, { id: 'Other' }]}
- defaultValue={'Work'}
+ defaultValue="Work"
+ fullWidth={false}
+ sx={{ width: 130, minWidth: 130 }}
/>
</Stack>
<Stack gap={1} flexDirection="row">
@@ -187,9 +205,11 @@ const ContactPersonalInformationInputs = () => {
source="phone_2_type"
label="Type"
helperText={false}
- optionText={choice => choice.id}
+ optionText="id"
choices={[{ id: 'Work' }, { id: 'Home' }, { id: 'Other' }]}
- defaultValue={'Work'}
+ defaultValue="Work"
+ fullWidth={false}
+ sx={{ width: 130, minWidth: 130 }}
/>
</Stack>
<TextInput
diff --git a/src/providers/commons/getContactAvatar.spec.ts b/src/providers/commons/getContactAvatar.spec.ts
index 45fcba2a..141480bd 100644
--- a/src/providers/commons/getContactAvatar.spec.ts
+++ b/src/providers/commons/getContactAvatar.spec.ts
@@ -5,7 +5,7 @@
// This test file uses the node environment because the getContactAvatar uses the TextEncoder API. This API is not available in the version of JSDOM used by Jest in CI.
// As CI uses Node18 and not the latest LTS, the crypto module is not available in the global scope.
-import { Contact } from '../../types';
+import { Contact, EmailAndType } from '../../types';
import { getContactAvatar, hash } from './getContactAvatar';
// eslint-disable-next-line no-global-assign
@@ -16,27 +16,33 @@ Object.defineProperty(globalThis, 'crypto', {
});
it('should return gravatar URL for [email protected]', async () => {
- const email = ['[email protected]'];
- const record: Partial<Contact> = { email };
+ const email: EmailAndType[] = [
+ { email: '[email protected]', type: 'Work' },
+ ];
+ const record: Partial<Contact> = { email_jsonb: email };
const avatarUrl = await getContactAvatar(record);
- const hashedEmail = await hash(email[0]);
+ const hashedEmail = await hash(email[0].email);
expect(avatarUrl).toBe(
`https://www.gravatar.com/avatar/${hashedEmail}?d=404`
);
});
it('should return favicon URL if gravatar does not exist', async () => {
- const email = ['[email protected]'];
- const record: Partial<Contact> = { email };
+ const email: EmailAndType[] = [
+ { email: '[email protected]', type: 'Work' },
+ ];
+ const record: Partial<Contact> = { email_jsonb: email };
const avatarUrl = await getContactAvatar(record);
expect(avatarUrl).toBe('https://gravatar.com/favicon.ico');
});
it('should not return favicon URL if not domain not allowed', async () => {
- const email = ['[email protected]'];
- const record: Partial<Contact> = { email };
+ const email: EmailAndType[] = [
+ { email: '[email protected]', type: 'Work' },
+ ];
+ const record: Partial<Contact> = { email_jsonb: email };
const avatarUrl = await getContactAvatar(record);
expect(avatarUrl).toBeNull();
@@ -50,27 +56,32 @@ it('should return null if no email is provided', async () => {
});
it('should return null if an empty array is provided', async () => {
- const email: string[] = [];
- const record: Partial<Contact> = { email };
+ const email: EmailAndType[] = [];
+ const record: Partial<Contact> = { email_jsonb: email };
const avatarUrl = await getContactAvatar(record);
expect(avatarUrl).toBeNull();
});
it('should return null if email has no gravatar or validate domain', async () => {
- const email = ['[email protected]'];
- const record: Partial<Contact> = { email };
+ const email: EmailAndType[] = [
+ { email: '[email protected]', type: 'Work' },
+ ];
+ const record: Partial<Contact> = { email_jsonb: email };
const avatarUrl = await getContactAvatar(record);
expect(avatarUrl).toBeNull();
});
it('should return gravatar URL for 2nd email if 1st email has no gravatar nor valid domain', async () => {
- const email = ['[email protected]', '[email protected]'];
- const record: Partial<Contact> = { email };
+ const email: EmailAndType[] = [
+ { email: '[email protected]', type: 'Work' },
+ { email: '[email protected]', type: 'Work' },
+ ];
+ const record: Partial<Contact> = { email_jsonb: email };
const avatarUrl = await getContactAvatar(record);
- const hashedEmail = await hash(email[1]);
+ const hashedEmail = await hash(email[1].email);
expect(avatarUrl).toBe(
`https://www.gravatar.com/avatar/${hashedEmail}?d=404`
);
diff --git a/src/providers/commons/getContactAvatar.ts b/src/providers/commons/getContactAvatar.ts
index 93370bbd..417ba181 100644
--- a/src/providers/commons/getContactAvatar.ts
+++ b/src/providers/commons/getContactAvatar.ts
@@ -40,11 +40,11 @@ async function getFaviconUrl(domain: string): Promise<string | null> {
export async function getContactAvatar(
record: Partial<Contact>
): Promise<string | null> {
- if (!record.email || !record.email.length) {
+ if (!record.email_jsonb || !record.email_jsonb.length) {
return null;
}
- for (const email of record.email) {
+ for (const { email } of record.email_jsonb) {
// Step 1: Try to get Gravatar image
const gravatarUrl = await getGravatarUrl(email);
try {
diff --git a/src/providers/fakerest/dataGenerator/contacts.ts b/src/providers/fakerest/dataGenerator/contacts.ts
index 24bb59bd..dbd51af2 100644
--- a/src/providers/fakerest/dataGenerator/contacts.ts
+++ b/src/providers/fakerest/dataGenerator/contacts.ts
@@ -33,7 +33,12 @@ export const generateContacts = (db: Db, size = 500): Required<Contact>[] => {
const gender = random.arrayElement(defaultContactGender).value;
const first_name = name.firstName(gender as any);
const last_name = name.lastName();
- const email = [internet.email(first_name, last_name)];
+ const email = [
+ {
+ email: internet.email(first_name, last_name),
+ type: 'Work' as const,
+ },
+ ];
const avatar = {
src: has_avatar
? 'https://marmelab.com/posters/avatar-' +
@@ -67,7 +72,7 @@ export const generateContacts = (db: Db, size = 500): Required<Contact>[] => {
title: title.charAt(0).toUpperCase() + title.substr(1),
company_id: company.id,
company_name: company.name,
- email,
+ email_jsonb: email,
phone_1_number: phone.phoneNumber(),
phone_1_type: random.arrayElement(['Work', 'Home', 'Other']),
phone_2_number: phone.phoneNumber(),
diff --git a/src/providers/fakerest/dataProvider.ts b/src/providers/fakerest/dataProvider.ts
index 193e41aa..ad3c062b 100644
--- a/src/providers/fakerest/dataProvider.ts
+++ b/src/providers/fakerest/dataProvider.ts
@@ -62,7 +62,7 @@ async function processContactAvatar(
params: CreateParams<Contact> | UpdateParams<Contact>
): Promise<CreateParams<Contact> | UpdateParams<Contact>> {
const { data } = params;
- if (data.avatar || !data.email || !data.email.length) {
+ if (data.avatar || !data.email_jsonb || !data.email_jsonb.length) {
return params;
}
const avatarUrl = await getContactAvatar(data);
diff --git a/src/providers/supabase/dataProvider.ts b/src/providers/supabase/dataProvider.ts
index cc55d832..7ee126c0 100644
--- a/src/providers/supabase/dataProvider.ts
+++ b/src/providers/supabase/dataProvider.ts
@@ -70,7 +70,7 @@ async function processContactAvatar(
params: CreateParams<Contact> | UpdateParams<Contact>
): Promise<CreateParams<Contact> | UpdateParams<Contact>> {
const { data } = params;
- if (data.avatar || !data.email || !data.email.length) {
+ if (data.avatar || !data.email_jsonb || !data.email_jsonb.length) {
return params;
}
const avatarUrl = await getContactAvatar(data);
@@ -343,7 +343,7 @@ const applyFullTextSearch = (columns: string[]) => (params: GetListParams) => {
column === 'email'
? {
...acc,
- [`${column}@cs`]: `{${q}}`,
+ [`email_fts@ilike`]: q,
}
: {
...acc,
diff --git a/src/types.ts b/src/types.ts
index f72cdbdb..bcd14dc7 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -70,12 +70,17 @@ export type Company = {
nb_deals?: number;
} & Pick<RaRecord, 'id'>;
+export type EmailAndType = {
+ email: string;
+ type: 'Work' | 'Home' | 'Other';
+};
+
export type Contact = {
first_name: string;
last_name: string;
title: string;
company_id: Identifier;
- email: string[];
+ email_jsonb: EmailAndType[];
avatar?: Partial<RAFile>;
linkedin_url?: string | null;
first_seen: string;
diff --git a/supabase/functions/postmark/addNoteToContact.ts b/supabase/functions/postmark/addNoteToContact.ts
index 1650362e..15c5b3cd 100644
--- a/supabase/functions/postmark/addNoteToContact.ts
+++ b/supabase/functions/postmark/addNoteToContact.ts
@@ -40,7 +40,7 @@ export const addNoteToContact = async ({
await supabaseAdmin
.from('contacts')
.select('*')
- .overlaps('email', [email])
+ .overlaps('email', [email]) // FIXME
.maybeSingle();
if (fetchContactError)
return new Response(
diff --git a/supabase/migrations/20250109152531_email_array.sql b/supabase/migrations/20250109152531_email_jsonb.sql
similarity index 100%
rename from supabase/migrations/20250109152531_email_array.sql
rename to supabase/migrations/20250109152531_email_jsonb.sql