CSV importing and exporting fixes (#8824)

Fixes issue https://github.com/twentyhq/twenty/issues/5793 (and
duplicate https://github.com/twentyhq/twenty/issues/8822)

- Fix importing multi-select and array fields.
- Fix exporting and importing RAW_JSON fields.

---------

Co-authored-by: ad-elias <elias@autodiligence.com>
This commit is contained in:
eliasylonen
2024-12-05 18:44:53 +01:00
committed by GitHub
parent 815e5dfa16
commit f60ce384c6
10 changed files with 181 additions and 28 deletions

View File

@ -11,28 +11,36 @@ export const useExportProcessRecordsForCSV = (objectNameSingular: string) => {
});
const processRecordsForCSVExport = (records: ObjectRecord[]) => {
return records.map((record) => {
const currencyFields = objectMetadataItem.fields.filter(
(field) => field.type === FieldMetadataType.Currency,
);
return records.map((record) =>
objectMetadataItem.fields.reduce(
(processedRecord, field) => {
if (!isDefined(record[field.name])) {
return processedRecord;
}
const processedRecord = {
...record,
};
for (const currencyField of currencyFields) {
if (isDefined(record[currencyField.name])) {
processedRecord[currencyField.name] = {
amountMicros: convertCurrencyMicrosToCurrencyAmount(
record[currencyField.name].amountMicros,
),
currencyCode: record[currencyField.name].currencyCode,
} satisfies FieldCurrencyValue;
}
}
return processedRecord;
});
switch (field.type) {
case FieldMetadataType.Currency:
return {
...processedRecord,
[field.name]: {
amountMicros: convertCurrencyMicrosToCurrencyAmount(
record[field.name].amountMicros,
),
currencyCode: record[field.name].currencyCode,
} satisfies FieldCurrencyValue,
};
case FieldMetadataType.RawJson:
return {
...processedRecord,
[field.name]: JSON.stringify(record[field.name]),
};
default:
return processedRecord;
}
},
{ ...record },
),
);
};
return { processRecordsForCSVExport };