5425 - Introducing support for all Composite Fields Import (#5470)

Adding support for all Composite Fields while using the "import"
functionality. This includes:
- Currency
- Address

Edit : 
- Refactored a lot of types in the spreadsheet import module
- Renamed a lot of functions, hooks and types that were not
self-explanatory enough

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
Co-authored-by: Félix Malfait <felix.malfait@gmail.com>
Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
This commit is contained in:
Aryan Singh
2024-07-23 21:32:23 +05:30
committed by GitHub
parent 2cc0597ee4
commit 5c8fe027f9
46 changed files with 888 additions and 535 deletions

View File

@ -3,11 +3,11 @@ import { v4 } from 'uuid';
import {
Errors,
Meta,
ImportedStructuredRowMetadata,
} from '@/spreadsheet-import/steps/components/ValidationStep/types';
import {
Data,
Fields,
ImportedStructuredRow,
Info,
RowHook,
TableHook,
@ -16,11 +16,11 @@ import { isDefined } from '~/utils/isDefined';
import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull';
export const addErrorsAndRunHooks = <T extends string>(
data: (Data<T> & Partial<Meta>)[],
data: (ImportedStructuredRow<T> & Partial<ImportedStructuredRowMetadata>)[],
fields: Fields<T>,
rowHook?: RowHook<T>,
tableHook?: TableHook<T>,
): (Data<T> & Meta)[] => {
): (ImportedStructuredRow<T> & ImportedStructuredRowMetadata)[] => {
const errors: Errors = {};
const addHookError = (rowIndex: number, fieldKey: T, error: Info) => {
@ -41,8 +41,8 @@ export const addErrorsAndRunHooks = <T extends string>(
}
fields.forEach((field) => {
field.validations?.forEach((validation) => {
switch (validation.rule) {
field.fieldValidationDefinitions?.forEach((fieldValidationDefinition) => {
switch (fieldValidationDefinition.rule) {
case 'unique': {
const values = data.map((entry) => entry[field.key as T]);
@ -51,7 +51,7 @@ export const addErrorsAndRunHooks = <T extends string>(
values.forEach((value) => {
if (
validation.allowEmpty === true &&
fieldValidationDefinition.allowEmpty === true &&
(isUndefinedOrNull(value) || value === '' || !value)
) {
// If allowEmpty is set, we will not validate falsy fields such as undefined or empty string.
@ -70,8 +70,10 @@ export const addErrorsAndRunHooks = <T extends string>(
errors[index] = {
...errors[index],
[field.key]: {
level: validation.level || 'error',
message: validation.errorMessage || 'Field must be unique',
level: fieldValidationDefinition.level || 'error',
message:
fieldValidationDefinition.errorMessage ||
'Field must be unique',
},
};
}
@ -88,8 +90,10 @@ export const addErrorsAndRunHooks = <T extends string>(
errors[index] = {
...errors[index],
[field.key]: {
level: validation.level || 'error',
message: validation.errorMessage || 'Field is required',
level: fieldValidationDefinition.level || 'error',
message:
fieldValidationDefinition.errorMessage ||
'Field is required',
},
};
}
@ -97,7 +101,10 @@ export const addErrorsAndRunHooks = <T extends string>(
break;
}
case 'regex': {
const regex = new RegExp(validation.value, validation.flags);
const regex = new RegExp(
fieldValidationDefinition.value,
fieldValidationDefinition.flags,
);
data.forEach((entry, index) => {
const value = entry[field.key]?.toString();
@ -105,10 +112,10 @@ export const addErrorsAndRunHooks = <T extends string>(
errors[index] = {
...errors[index],
[field.key]: {
level: validation.level || 'error',
level: fieldValidationDefinition.level || 'error',
message:
validation.errorMessage ||
`Field did not match the regex /${validation.value}/${validation.flags} `,
fieldValidationDefinition.errorMessage ||
`Field did not match the regex /${fieldValidationDefinition.value}/${fieldValidationDefinition.flags} `,
},
};
}
@ -119,12 +126,17 @@ export const addErrorsAndRunHooks = <T extends string>(
data.forEach((entry, index) => {
const value = entry[field.key]?.toString();
if (isNonEmptyString(value) && !validation.isValid(value)) {
if (
isNonEmptyString(value) &&
!fieldValidationDefinition.isValid(value)
) {
errors[index] = {
...errors[index],
[field.key]: {
level: validation.level || 'error',
message: validation.errorMessage || 'Field is invalid',
level: fieldValidationDefinition.level || 'error',
message:
fieldValidationDefinition.errorMessage ||
'Field is invalid',
},
};
}
@ -140,7 +152,8 @@ export const addErrorsAndRunHooks = <T extends string>(
if (!('__index' in value)) {
value.__index = v4();
}
const newValue = value as Data<T> & Meta;
const newValue = value as ImportedStructuredRow<T> &
ImportedStructuredRowMetadata;
if (isDefined(errors[index])) {
return { ...newValue, __errors: errors[index] };