The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Share CSV formatting and composite field labels (#25986)
Weiko49215bfa3b9a
diff --git a/packages/twenty-front/src/modules/object-record/record-index/export/hooks/__tests__/useRecordIndexExportRecords.test.ts b/packages/twenty-front/src/modules/object-record/record-index/export/hooks/__tests__/useRecordIndexExportRecords.test.tsindex b357391930e..d8d373c4b83 100644--- a/packages/twenty-front/src/modules/object-record/record-index/export/hooks/__tests__/useRecordIndexExportRecords.test.ts+++ b/packages/twenty-front/src/modules/object-record/record-index/export/hooks/__tests__/useRecordIndexExportRecords.test.ts@@ -1,6 +1,6 @@import { type FieldMetadata } from '@/object-record/record-field/ui/types/FieldMetadata';import { type ColumnDefinition } from '@/object-record/record-table/types/ColumnDefinition';-import { CSV_INJECTION_PREVENTION_ZWJ } from '@/spreadsheet-import/constants/CsvInjectionPreventionZwj';+import { CSV_INJECTION_PREVENTION_ZWJ } from 'twenty-shared/constants';import {csvDownloader,diff --git a/packages/twenty-front/src/modules/object-record/record-index/export/hooks/useRecordIndexExportRecords.ts b/packages/twenty-front/src/modules/object-record/record-index/export/hooks/useRecordIndexExportRecords.tsindex 2f3c15196e0..0a46bd00ded 100644--- a/packages/twenty-front/src/modules/object-record/record-index/export/hooks/useRecordIndexExportRecords.ts+++ b/packages/twenty-front/src/modules/object-record/record-index/export/hooks/useRecordIndexExportRecords.ts@@ -11,12 +11,14 @@ import {} from '@/object-record/record-index/export/hooks/useRecordIndexLazyFetchRecords';import { type ColumnDefinition } from '@/object-record/record-table/types/ColumnDefinition';import { type ObjectRecord } from '@/object-record/types/ObjectRecord';-import { COMPOSITE_FIELD_SUB_FIELD_LABELS } from '@/settings/data-model/constants/CompositeFieldSubFieldLabel';-import { formatValueForCSV } from '@/spreadsheet-import/utils/formatValueForCSV';-import { sanitizeValueForCSVExport } from '@/spreadsheet-import/utils/sanitizeValueForCSVExport';import { t } from '@lingui/core/macro';import { saveAs } from 'file-saver';-import { isDefined } from 'twenty-shared/utils';+import { COMPOSITE_FIELD_SUB_FIELD_LABELS } from 'twenty-shared/constants';+import {+ formatValueForCSV,+ isDefined,+ sanitizeValueForCSVExport,+} from 'twenty-shared/utils';import { FieldMetadataType, RelationType } from '~/generated-metadata/graphql';import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull';diff --git a/packages/twenty-front/src/modules/object-record/spreadsheet-import/utils/spreadsheetImportGetCompositeSubFieldKey.ts b/packages/twenty-front/src/modules/object-record/spreadsheet-import/utils/spreadsheetImportGetCompositeSubFieldKey.tsindex 3174ffc9033..7a6f65fd179 100644--- a/packages/twenty-front/src/modules/object-record/spreadsheet-import/utils/spreadsheetImportGetCompositeSubFieldKey.ts+++ b/packages/twenty-front/src/modules/object-record/spreadsheet-import/utils/spreadsheetImportGetCompositeSubFieldKey.ts@@ -1,6 +1,6 @@import { type FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';import { isCompositeFieldType } from '@/object-record/object-filter-dropdown/utils/isCompositeFieldType';-import { COMPOSITE_FIELD_SUB_FIELD_LABELS } from '@/settings/data-model/constants/CompositeFieldSubFieldLabel';+import { COMPOSITE_FIELD_SUB_FIELD_LABELS } from 'twenty-shared/constants';export const getCompositeSubFieldKey = (fieldMetadataItem: FieldMetadataItem,diff --git a/packages/twenty-front/src/modules/object-record/spreadsheet-import/utils/spreadsheetImportGetRelationConnectSubFieldLabel.ts b/packages/twenty-front/src/modules/object-record/spreadsheet-import/utils/spreadsheetImportGetRelationConnectSubFieldLabel.tsindex a3d3bb9d2c7..11318c81bcc 100644--- a/packages/twenty-front/src/modules/object-record/spreadsheet-import/utils/spreadsheetImportGetRelationConnectSubFieldLabel.ts+++ b/packages/twenty-front/src/modules/object-record/spreadsheet-import/utils/spreadsheetImportGetRelationConnectSubFieldLabel.ts@@ -1,6 +1,6 @@import { type FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';import { isCompositeFieldType } from '@/object-record/object-filter-dropdown/utils/isCompositeFieldType';-import { COMPOSITE_FIELD_SUB_FIELD_LABELS } from '@/settings/data-model/constants/CompositeFieldSubFieldLabel';+import { COMPOSITE_FIELD_SUB_FIELD_LABELS } from 'twenty-shared/constants';import { isDefined } from 'twenty-shared/utils';export const getRelationConnectSubFieldLabel = (diff --git a/packages/twenty-front/src/modules/object-record/spreadsheet-import/utils/spreadsheetImportGetUnicityTableHook.ts b/packages/twenty-front/src/modules/object-record/spreadsheet-import/utils/spreadsheetImportGetUnicityTableHook.tsindex 263d219030a..22da19e147b 100644--- a/packages/twenty-front/src/modules/object-record/spreadsheet-import/utils/spreadsheetImportGetUnicityTableHook.ts+++ b/packages/twenty-front/src/modules/object-record/spreadsheet-import/utils/spreadsheetImportGetUnicityTableHook.ts@@ -3,7 +3,7 @@ import { type FieldMetadataItem } from '@/object-metadata/types/FieldMetadataIteimport { type EnrichedObjectMetadataItem } from '@/object-metadata/types/EnrichedObjectMetadataItem';import { isCompositeFieldType } from '@/object-record/object-filter-dropdown/utils/isCompositeFieldType';import { getCompositeSubFieldKey } from '@/object-record/spreadsheet-import/utils/spreadsheetImportGetCompositeSubFieldKey';-import { COMPOSITE_FIELD_SUB_FIELD_LABELS } from '@/settings/data-model/constants/CompositeFieldSubFieldLabel';+import { COMPOSITE_FIELD_SUB_FIELD_LABELS } from 'twenty-shared/constants';import { SETTINGS_COMPOSITE_FIELD_TYPE_CONFIGS } from '@/settings/data-model/constants/SettingsCompositeFieldTypeConfigs';import {type ImportedStructuredRow,diff --git a/packages/twenty-front/src/modules/settings/data-model/constants/SettingsCompositeFieldTypeConfigs.ts b/packages/twenty-front/src/modules/settings/data-model/constants/SettingsCompositeFieldTypeConfigs.tsindex 86218e6d555..7c44810fce7 100644--- a/packages/twenty-front/src/modules/settings/data-model/constants/SettingsCompositeFieldTypeConfigs.ts+++ b/packages/twenty-front/src/modules/settings/data-model/constants/SettingsCompositeFieldTypeConfigs.ts@@ -8,10 +8,10 @@ import {type FieldPhonesValue,type FieldRichTextValue,} from '@/object-record/record-field/ui/types/FieldMetadata';-import { COMPOSITE_FIELD_SUB_FIELD_LABELS } from '@/settings/data-model/constants/CompositeFieldSubFieldLabel';import { type SettingsFieldTypeConfig } from '@/settings/data-model/constants/SettingsNonCompositeFieldTypeConfigs';import { type CompositeFieldType } from '@/settings/data-model/types/CompositeFieldType';import {+ COMPOSITE_FIELD_SUB_FIELD_LABELS,COMPOSITE_FIELD_TYPE_SUB_FIELDS_NAMES,CurrencyCode,} from 'twenty-shared/constants';diff --git a/packages/twenty-front/src/modules/spreadsheet-import/steps/components/UploadStep/hooks/useDownloadFakeRecords.ts b/packages/twenty-front/src/modules/spreadsheet-import/steps/components/UploadStep/hooks/useDownloadFakeRecords.tsindex a2bc81d3b4f..bdb15c8e483 100644--- a/packages/twenty-front/src/modules/spreadsheet-import/steps/components/UploadStep/hooks/useDownloadFakeRecords.ts+++ b/packages/twenty-front/src/modules/spreadsheet-import/steps/components/UploadStep/hooks/useDownloadFakeRecords.ts@@ -3,8 +3,10 @@ import { spreadsheetImportFilterAvailableFieldMetadataItems } from '@/object-recimport { getCompositeSubFieldLabelWithFieldLabel } from '@/object-record/spreadsheet-import/utils/spreadsheetImportGetCompositeSubFieldLabelWithFieldLabel';import { SETTINGS_COMPOSITE_FIELD_TYPE_CONFIGS } from '@/settings/data-model/constants/SettingsCompositeFieldTypeConfigs';import { SETTINGS_NON_COMPOSITE_FIELD_TYPE_CONFIGS } from '@/settings/data-model/constants/SettingsNonCompositeFieldTypeConfigs';-import { formatValueForCSV } from '@/spreadsheet-import/utils/formatValueForCSV';-import { sanitizeValueForCSVExport } from '@/spreadsheet-import/utils/sanitizeValueForCSVExport';+import {+ formatValueForCSV,+ sanitizeValueForCSVExport,+} from 'twenty-shared/utils';import { saveAs } from 'file-saver';import { FieldMetadataType } from 'twenty-shared/types';diff --git a/packages/twenty-front/src/modules/spreadsheet-import/utils/__tests__/csvSecurity.test.ts b/packages/twenty-front/src/modules/spreadsheet-import/utils/__tests__/csvSecurity.test.tsindex 6792ca5e518..6c44a5e39e9 100644--- a/packages/twenty-front/src/modules/spreadsheet-import/utils/__tests__/csvSecurity.test.ts+++ b/packages/twenty-front/src/modules/spreadsheet-import/utils/__tests__/csvSecurity.test.ts@@ -1,8 +1,10 @@-import { CSV_DANGEROUS_CHARACTERS } from '@/spreadsheet-import/constants/CsvDangerousCharacters';-import { CSV_INJECTION_PREVENTION_ZWJ } from '@/spreadsheet-import/constants/CsvInjectionPreventionZwj';+import {+ CSV_DANGEROUS_CHARACTERS,+ CSV_INJECTION_PREVENTION_ZWJ,+} from 'twenty-shared/constants';import { cleanZWJFromImportedValue } from '@/spreadsheet-import/utils/cleanZWJFromImportedValue';import { containsCSVProtectionZWJ } from '@/spreadsheet-import/utils/containsCSVProtectionZWJ';-import { sanitizeValueForCSVExport } from '@/spreadsheet-import/utils/sanitizeValueForCSVExport';+import { sanitizeValueForCSVExport } from 'twenty-shared/utils';describe('csvSecurity', () => {describe('CSV_DANGEROUS_CHARACTERS regex', () => {diff --git a/packages/twenty-front/src/modules/spreadsheet-import/utils/__tests__/formatValueForCSV.test.ts b/packages/twenty-front/src/modules/spreadsheet-import/utils/__tests__/formatValueForCSV.test.tsindex 45b5a13b0ac..5b323979709 100644--- a/packages/twenty-front/src/modules/spreadsheet-import/utils/__tests__/formatValueForCSV.test.ts+++ b/packages/twenty-front/src/modules/spreadsheet-import/utils/__tests__/formatValueForCSV.test.ts@@ -1,4 +1,4 @@-import { formatValueForCSV } from '@/spreadsheet-import/utils/formatValueForCSV';+import { formatValueForCSV } from 'twenty-shared/utils';describe('formatValueForCSV', () => {it('should format values with commas, quotes, newlines and carriage returns', () => {diff --git a/packages/twenty-front/src/modules/spreadsheet-import/utils/cleanZWJFromImportedValue.ts b/packages/twenty-front/src/modules/spreadsheet-import/utils/cleanZWJFromImportedValue.tsindex d32181a04cf..c8aa1f3ba19 100644--- a/packages/twenty-front/src/modules/spreadsheet-import/utils/cleanZWJFromImportedValue.ts+++ b/packages/twenty-front/src/modules/spreadsheet-import/utils/cleanZWJFromImportedValue.ts@@ -1,4 +1,4 @@-import { CSV_INJECTION_PREVENTION_ZWJ } from '@/spreadsheet-import/constants/CsvInjectionPreventionZwj';+import { CSV_INJECTION_PREVENTION_ZWJ } from 'twenty-shared/constants';export const cleanZWJFromImportedValue = (value: string): string => {if (typeof value !== 'string') return value;diff --git a/packages/twenty-front/src/modules/spreadsheet-import/utils/containsCSVProtectionZWJ.ts b/packages/twenty-front/src/modules/spreadsheet-import/utils/containsCSVProtectionZWJ.tsindex c5e7b816131..4fa320aecae 100644--- a/packages/twenty-front/src/modules/spreadsheet-import/utils/containsCSVProtectionZWJ.ts+++ b/packages/twenty-front/src/modules/spreadsheet-import/utils/containsCSVProtectionZWJ.ts@@ -1,4 +1,4 @@-import { CSV_INJECTION_PREVENTION_ZWJ } from '@/spreadsheet-import/constants/CsvInjectionPreventionZwj';+import { CSV_INJECTION_PREVENTION_ZWJ } from 'twenty-shared/constants';export const containsCSVProtectionZWJ = (value: string): boolean => {return (diff --git a/packages/twenty-front/src/modules/spreadsheet-import/utils/sanitizeValueForCSVExport.ts b/packages/twenty-front/src/modules/spreadsheet-import/utils/sanitizeValueForCSVExport.tsdeleted file mode 100644index 58b5ce71b23..00000000000--- a/packages/twenty-front/src/modules/spreadsheet-import/utils/sanitizeValueForCSVExport.ts+++ /dev/null@@ -1,14 +0,0 @@-import { CSV_DANGEROUS_CHARACTERS } from '@/spreadsheet-import/constants/CsvDangerousCharacters';-import { CSV_INJECTION_PREVENTION_ZWJ } from '@/spreadsheet-import/constants/CsvInjectionPreventionZwj';--export const sanitizeValueForCSVExport = (value: any): string => {- if (value == null) return '';-- const stringValue = typeof value === 'string' ? value : String(value);-- if (CSV_DANGEROUS_CHARACTERS.test(stringValue)) {- return CSV_INJECTION_PREVENTION_ZWJ + stringValue;- }-- return stringValue;-};diff --git a/packages/twenty-front/src/modules/settings/data-model/constants/CompositeFieldSubFieldLabel.ts b/packages/twenty-shared/src/constants/CompositeFieldSubFieldLabels.tssimilarity index 82%rename from packages/twenty-front/src/modules/settings/data-model/constants/CompositeFieldSubFieldLabel.tsrename to packages/twenty-shared/src/constants/CompositeFieldSubFieldLabels.tsindex 6a372075d98..be40792d2ff 100644--- a/packages/twenty-front/src/modules/settings/data-model/constants/CompositeFieldSubFieldLabel.ts+++ b/packages/twenty-shared/src/constants/CompositeFieldSubFieldLabels.ts@@ -1,8 +1,11 @@-import { type CompositeFieldType } from '@/settings/data-model/types/CompositeFieldType';-import { FieldMetadataType } from 'twenty-shared/types';+import { type COMPOSITE_FIELD_TYPE_SUB_FIELDS_NAMES } from '@/constants/CompositeFieldTypeSubFieldsNames';+import { FieldMetadataType } from '@/types/FieldMetadataType';export const COMPOSITE_FIELD_SUB_FIELD_LABELS: {- [key in CompositeFieldType]: Record<string, string>;+ [key in keyof typeof COMPOSITE_FIELD_TYPE_SUB_FIELDS_NAMES]: Record<+ string,+ string+ >;} = {[FieldMetadataType.CURRENCY]: {amountMicros: 'Amount',diff --git a/packages/twenty-front/src/modules/spreadsheet-import/constants/CsvDangerousCharacters.ts b/packages/twenty-shared/src/constants/CsvDangerousCharacters.tssimilarity index 100%rename from packages/twenty-front/src/modules/spreadsheet-import/constants/CsvDangerousCharacters.tsrename to packages/twenty-shared/src/constants/CsvDangerousCharacters.tsdiff --git a/packages/twenty-front/src/modules/spreadsheet-import/constants/CsvInjectionPreventionZwj.ts b/packages/twenty-shared/src/constants/CsvInjectionPreventionZwj.tssimilarity index 100%rename from packages/twenty-front/src/modules/spreadsheet-import/constants/CsvInjectionPreventionZwj.tsrename to packages/twenty-shared/src/constants/CsvInjectionPreventionZwj.tsdiff --git a/packages/twenty-shared/src/constants/index.ts b/packages/twenty-shared/src/constants/index.tsindex df0d7d9f925..cb1585be740 100644--- a/packages/twenty-shared/src/constants/index.ts+++ b/packages/twenty-shared/src/constants/index.ts@@ -13,7 +13,10 @@ export { ALLOWED_FULL_NAME_SORT_SUBFIELDS } from './AllowedFullNameSortSubfieldsexport { BACKEND_BATCH_REQUEST_MAX_COUNT } from './BackendBatchRequestMaxCount';export { CalendarStartDay } from './CalendarStartDay';export { COMMAND_MENU_CONFIRMATION_MODAL_RESULT_BROWSER_EVENT_NAME } from './CommandMenuConfirmationModalResultBrowserEventName';+export { COMPOSITE_FIELD_SUB_FIELD_LABELS } from './CompositeFieldSubFieldLabels';export { COMPOSITE_FIELD_TYPE_SUB_FIELDS_NAMES } from './CompositeFieldTypeSubFieldsNames';+export { CSV_DANGEROUS_CHARACTERS } from './CsvDangerousCharacters';+export { CSV_INJECTION_PREVENTION_ZWJ } from './CsvInjectionPreventionZwj';export { CurrencyCode } from './CurrencyCode';export { CURRENCY_CODE_LABELS } from './CurrencyCodeLabels';export { DATE_TYPE_FORMAT } from './DateTypeFormat';diff --git a/packages/twenty-front/src/modules/spreadsheet-import/utils/formatValueForCSV.ts b/packages/twenty-shared/src/utils/csv/formatValueForCSV.tssimilarity index 81%rename from packages/twenty-front/src/modules/spreadsheet-import/utils/formatValueForCSV.tsrename to packages/twenty-shared/src/utils/csv/formatValueForCSV.tsindex 4edf19b2967..d62b37c0657 100644--- a/packages/twenty-front/src/modules/spreadsheet-import/utils/formatValueForCSV.ts+++ b/packages/twenty-shared/src/utils/csv/formatValueForCSV.ts@@ -3,10 +3,10 @@ import { isString } from '@sniptt/guards';// Formats values for CSV output by wrapping in quotes when needed and escaping internal quotes.// This handles CSV formatting requirements (commas, quotes, newlines) but NOT security issues.// For security (CSV injection prevention), use sanitizeValueForCSVExport() BEFORE this function.-export const formatValueForCSV = (value: any) => {+export const formatValueForCSV = (value: unknown) => {if (value == null) return '';- const stringValue = isString(value) ? value : JSON.stringify(value);+ const stringValue = isString(value) ? value : (JSON.stringify(value) ?? '');if (stringValue.includes(',') ||diff --git a/packages/twenty-shared/src/utils/csv/sanitizeValueForCSVExport.ts b/packages/twenty-shared/src/utils/csv/sanitizeValueForCSVExport.tsnew file mode 100644index 00000000000..0c3347331f1--- /dev/null+++ b/packages/twenty-shared/src/utils/csv/sanitizeValueForCSVExport.ts@@ -0,0 +1,14 @@+import { CSV_DANGEROUS_CHARACTERS } from '@/constants/CsvDangerousCharacters';+import { CSV_INJECTION_PREVENTION_ZWJ } from '@/constants/CsvInjectionPreventionZwj';++export const sanitizeValueForCSVExport = (value: unknown): string => {+ if (value == null) return '';++ const stringValue = typeof value === 'string' ? value : String(value);++ if (CSV_DANGEROUS_CHARACTERS.test(stringValue)) {+ return CSV_INJECTION_PREVENTION_ZWJ + stringValue;+ }++ return stringValue;+};diff --git a/packages/twenty-shared/src/utils/index.ts b/packages/twenty-shared/src/utils/index.tsindex 8887d77aa22..1815159b1ea 100644--- a/packages/twenty-shared/src/utils/index.ts+++ b/packages/twenty-shared/src/utils/index.ts@@ -30,6 +30,8 @@ export { evaluateConditionalAvailabilityExpression } from './command-menu-items/export { resolveObjectMetadataLabel } from './command-menu-items/resolveObjectMetadataLabel';export { safeGetNestedProperty } from './command-menu-items/safeGetNestedProperty';export { computeDiffBetweenObjects } from './compute-diff-between-objects';+export { formatValueForCSV } from './csv/formatValueForCSV';+export { sanitizeValueForCSVExport } from './csv/sanitizeValueForCSVExport';export {NON_ISO_DATE_FORMATS,ACCEPTED_DATE_FORMATS,