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

5425 - Introducing support for all Composite Fields Import #5470

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNonEmptyString } from '@sniptt/guards';
import { isNonEmptyString, isUndefined } from '@sniptt/guards';
import { useIcons } from 'twenty-ui';

import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
Expand All @@ -9,11 +9,26 @@ import { Field, SpreadsheetOptions } from '@/spreadsheet-import/types';
import { SnackBarVariant } from '@/ui/feedback/snack-bar-manager/components/SnackBar';
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
import { FieldMetadataType } from '~/generated-metadata/graphql';
import { convertCurrencyMicrosToCurrency } from '~/utils/convert-currency-amount';
import { isDefined } from '~/utils/isDefined';

const firstName = 'Firstname';
const lastName = 'Lastname';

const currencyCode = 'Currency Code';
const amountMicros = 'Amount';

const addressFields = {
addressStreet1: 'Address 1',
addressStreet2: 'Address 2',
addressCity: 'City',
addressPostcode: 'Post Code',
addressState: 'State',
addressCountry: 'Country',
addressLat: 'Latitude',
addressLng: 'Longitude',
};

export const useSpreadsheetRecordImport = (objectNameSingular: string) => {
const { openSpreadsheetImport } = useSpreadsheetImport<any>();
const { enqueueSnackBar } = useSnackBar();
Expand Down Expand Up @@ -72,6 +87,46 @@ export const useSpreadsheetRecordImport = (objectNameSingular: string) => {
field.label + ' (ID)',
),
});
} else if (field.type === FieldMetadataType.Currency) {
templateFields.push({
icon: getIcon(field.icon),
Copy link
Member

Choose a reason for hiding this comment

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

can we extract the templateFields array build to an util?
I'm also not a big fan of templateFields naming. I would say availableFields

Once extracted to an util, could you also add unit tests?

label: `${currencyCode} (${field.label})`,
key: `${currencyCode} (${field.name})`,
fieldType: {
type: 'input',
},
validations: getSpreadSheetValidation(
field.type,
`${currencyCode} (${field.label})`,
),
});
templateFields.push({
icon: getIcon(field.icon),
label: `${amountMicros} (${field.label})`,
key: `${amountMicros} (${field.name})`,
fieldType: {
type: 'input',
},
validations: getSpreadSheetValidation(
field.type,
`${amountMicros} (${field.label})`,
),
});
} else if (field.type === FieldMetadataType.Address) {
Object.entries(addressFields).forEach(([_, value]) => {
templateFields.push({
icon: getIcon(field.icon),
label: `${value} (${field.label})`,
key: `${value} (${field.name})`,
fieldType: {
type: 'input',
},
validations: getSpreadSheetValidation(
field.type,
`${value} (${field.label})`,
),
});
});
} else if (field.type === FieldMetadataType.Select) {
templateFields.push({
icon: getIcon(field.icon),
Expand Down Expand Up @@ -128,6 +183,10 @@ export const useSpreadsheetRecordImport = (objectNameSingular: string) => {
for (const field of fields) {
const value = record[field.name];
Copy link
Member

Choose a reason for hiding this comment

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

can we also extract this whole logic to an util and add unit tests?


if (isUndefined(value)) {
continue;
}

switch (field.type) {
case FieldMetadataType.Boolean:
if (value !== undefined) {
Expand All @@ -141,13 +200,74 @@ export const useSpreadsheetRecordImport = (objectNameSingular: string) => {
}
break;
case FieldMetadataType.Currency:
if (value !== undefined) {
if (
isDefined(
record[`${amountMicros} (${field.name})`] ||
record[`${currencyCode} (${field.name})`],
)
) {
Copy link

Choose a reason for hiding this comment

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

The condition isDefined(record[${amountMicros} (${field.name})] || record[${currencyCode} (${field.name})]) should be simplified to isDefined(record[${amountMicros} (${field.name})]) || isDefined(record[${currencyCode} (${field.name})]) for better readability and correctness.

fieldMapping[field.name] = {
amountMicros: convertCurrencyMicrosToCurrency(
Number(record[`${amountMicros} (${field.name})`]),
),
currencyCode:
record[`${currencyCode} (${field.name})`] || 'USD',
};
}
break;
case FieldMetadataType.Address: {
if (
isDefined(
record[`${addressFields.addressStreet1} (${field.name})`] ||
record[
`${addressFields.addressStreet2} (${field.name})`
] ||
record[`${addressFields.addressCity} (${field.name})`] ||
record[
`${addressFields.addressPostcode} (${field.name})`
] ||
record[`${addressFields.addressState} (${field.name})`] ||
record[
`${addressFields.addressCountry} (${field.name})`
] ||
record[`${addressFields.addressLat} (${field.name})`] ||
record[`${addressFields.addressLng} (${field.name})`],
)
) {
fieldMapping[field.name] = {
amountMicros: Number(value),
currencyCode: 'USD',
addressStreet1:
record[
`${addressFields.addressStreet1} (${field.name})`
] || '',
addressStreet2:
record[
`${addressFields.addressStreet2} (${field.name})`
] || '',
addressCity:
record[`${addressFields.addressCity} (${field.name})`] ||
'',
addressPostcode: Number(
record[
`${addressFields.addressPostcode} (${field.name})`
],
),
addressState:
record[`${addressFields.addressState} (${field.name})`] ||
'',
addressCountry:
record[
`${addressFields.addressCountry} (${field.name})`
] || '',
addressLat: Number(
record[`${addressFields.addressLat} (${field.name})`],
),
addressLng: Number(
record[`${addressFields.addressLng} (${field.name})`],
),
};
}
break;
}
case FieldMetadataType.Link:
if (value !== undefined) {
fieldMapping[field.name] = {
Expand Down
Loading