Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Emails and Phones in Spreadsheet import #7312

Merged
merged 2 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ export const CommandMenu = () => {
'emails',
['primaryEmail'],
),
{ phone: { ilike: `%${commandMenuSearch}%` } },
])
: undefined,
limit: 3,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {
FieldAddressValue,
FieldCurrencyValue,
FieldEmailsValue,
FieldFullNameValue,
FieldLinksValue,
FieldPhonesValue,
} from '@/object-record/record-field/types/FieldMetadata';
import { CompositeFieldLabels } from '@/object-record/spreadsheet-import/types/CompositeFieldLabels';
import { FieldMetadataType } from '~/generated-metadata/graphql';
Expand Down Expand Up @@ -30,6 +32,13 @@ export const COMPOSITE_FIELD_IMPORT_LABELS = {
primaryLinkUrlLabel: 'Link URL',
primaryLinkLabelLabel: 'Link Label',
} satisfies Partial<CompositeFieldLabels<FieldLinksValue>>,
[FieldMetadataType.Emails]: {
primaryEmailLabel: 'Email',
} satisfies Partial<CompositeFieldLabels<FieldEmailsValue>>,
[FieldMetadataType.Phones]: {
primaryPhoneCountryCodeLabel: 'Phone country code',
primaryPhoneNumberLabel: 'Phone number',
} satisfies Partial<CompositeFieldLabels<FieldPhonesValue>>,
[FieldMetadataType.Actor]: {
sourceLabel: 'Source',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const useBuildAvailableFieldsForImport = () => {
) => {
const availableFieldsForImport: AvailableFieldForImport[] = [];

// Todo: refactor this to avoid this else if syntax with duplicated code
for (const fieldMetadataItem of fieldMetadataItems) {
if (fieldMetadataItem.type === FieldMetadataType.FullName) {
const { firstNameLabel, lastNameLabel } =
Expand Down Expand Up @@ -155,6 +156,42 @@ export const useBuildAvailableFieldsForImport = () => {
fieldMetadataItem.label,
),
});
} else if (fieldMetadataItem.type === FieldMetadataType.Emails) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider extracting this repeated logic into a separate function to reduce duplication

Object.entries(
COMPOSITE_FIELD_IMPORT_LABELS[FieldMetadataType.Emails],
).forEach(([_, fieldLabel]) => {
availableFieldsForImport.push({
icon: getIcon(fieldMetadataItem.icon),
label: `${fieldLabel} (${fieldMetadataItem.label})`,
key: `${fieldLabel} (${fieldMetadataItem.name})`,
fieldType: {
type: 'input',
},
fieldValidationDefinitions:
getSpreadSheetFieldValidationDefinitions(
fieldMetadataItem.type,
`${fieldLabel} (${fieldMetadataItem.label})`,
),
});
});
} else if (fieldMetadataItem.type === FieldMetadataType.Phones) {
Object.entries(
COMPOSITE_FIELD_IMPORT_LABELS[FieldMetadataType.Phones],
).forEach(([_, fieldLabel]) => {
availableFieldsForImport.push({
icon: getIcon(fieldMetadataItem.icon),
label: `${fieldLabel} (${fieldMetadataItem.label})`,
key: `${fieldLabel} (${fieldMetadataItem.name})`,
fieldType: {
type: 'input',
},
fieldValidationDefinitions:
getSpreadSheetFieldValidationDefinitions(
fieldMetadataItem.type,
`${fieldLabel} (${fieldMetadataItem.label})`,
),
});
});
} else {
availableFieldsForImport.push({
icon: getIcon(fieldMetadataItem.icon),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
import {
FieldAddressValue,
FieldEmailsValue,
FieldLinksValue,
FieldPhonesValue,
} from '@/object-record/record-field/types/FieldMetadata';
import { COMPOSITE_FIELD_IMPORT_LABELS } from '@/object-record/spreadsheet-import/constants/CompositeFieldImportLabels';
import { ImportedStructuredRow } from '@/spreadsheet-import/types';
Expand Down Expand Up @@ -31,6 +33,8 @@ export const buildRecordFromImportedStructuredRow = (
CURRENCY: { amountMicrosLabel, currencyCodeLabel },
FULL_NAME: { firstNameLabel, lastNameLabel },
LINKS: { primaryLinkLabelLabel, primaryLinkUrlLabel },
EMAILS: { primaryEmailLabel },
PHONES: { primaryPhoneNumberLabel, primaryPhoneCountryCodeLabel },
} = COMPOSITE_FIELD_IMPORT_LABELS;

for (const field of fields) {
Expand Down Expand Up @@ -129,14 +133,48 @@ export const buildRecordFromImportedStructuredRow = (
}
break;
}
case FieldMetadataType.Link:
if (importedFieldValue !== undefined) {
case FieldMetadataType.Phones: {
if (
isDefined(
importedStructuredRow[
`${primaryPhoneCountryCodeLabel} (${field.name})`
] ||
importedStructuredRow[
`${primaryPhoneNumberLabel} (${field.name})`
],
)
) {
recordToBuild[field.name] = {
label: field.name,
url: importedFieldValue || null,
};
primaryPhoneCountryCode: castToString(
importedStructuredRow[
`${primaryPhoneCountryCodeLabel} (${field.name})`
],
),
primaryPhoneNumber: castToString(
importedStructuredRow[
`${primaryPhoneNumberLabel} (${field.name})`
],
),
additionalPhones: null,
} satisfies FieldPhonesValue;
}
break;
}
case FieldMetadataType.Emails: {
if (
isDefined(
importedStructuredRow[`${primaryEmailLabel} (${field.name})`],
)
) {
recordToBuild[field.name] = {
primaryEmail: castToString(
importedStructuredRow[`${primaryEmailLabel} (${field.name})`],
),
additionalEmails: null,
} satisfies FieldEmailsValue;
}
break;
}
case FieldMetadataType.Relation:
if (
isDefined(importedFieldValue) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export const sanitizeRecordInput = ({
return undefined;
}

// Todo: we should check that the fieldValue is a valid value
// (e.g. a string for a string field, following the right composite structure for composite fields)
return [fieldName, fieldValue];
})
.filter(isDefined),
Expand Down
Loading