-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
base: main
Are you sure you want to change the base?
Changes from all commits
9534d07
14cd8e2
b9557aa
7d6f07d
934ba95
3c9613e
455f3c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; | ||
|
@@ -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(); | ||
|
@@ -72,6 +87,46 @@ export const useSpreadsheetRecordImport = (objectNameSingular: string) => { | |
field.label + ' (ID)', | ||
), | ||
}); | ||
} else if (field.type === FieldMetadataType.Currency) { | ||
templateFields.push({ | ||
icon: getIcon(field.icon), | ||
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), | ||
|
@@ -128,6 +183,10 @@ export const useSpreadsheetRecordImport = (objectNameSingular: string) => { | |
for (const field of fields) { | ||
const value = record[field.name]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -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})`], | ||
) | ||
) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition |
||
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] = { | ||
|
There was a problem hiding this comment.
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?