Skip to content

The open alternative to Salesforce, designed for AI.

fix(front): keep the calendar day when importing a date without time (#26410)

Belkouched000921a9129
diff --git a/packages/twenty-front/src/modules/object-record/spreadsheet-import/utils/__tests__/buildRecordFromImportedStructuredRow.test.ts b/packages/twenty-front/src/modules/object-record/spreadsheet-import/utils/__tests__/buildRecordFromImportedStructuredRow.test.ts
index e42d67b96b5..368e2f65bca 100644
--- a/packages/twenty-front/src/modules/object-record/spreadsheet-import/utils/__tests__/buildRecordFromImportedStructuredRow.test.ts
+++ b/packages/twenty-front/src/modules/object-record/spreadsheet-import/utils/__tests__/buildRecordFromImportedStructuredRow.test.ts
@@ -428,7 +428,7 @@ describe('buildRecordFromImportedStructuredRow', () => {
blocknote: 'Rich content in blocknote format',
markdown: 'Content in markdown format',
},
- dateField: '2023-12-25T00:00:00.000Z',
+ dateField: '2023-12-25',
dateTimeField: '2023-12-25T10:30:00.000Z',
ratingField: '4',
});
@@ -491,6 +491,43 @@ describe('buildRecordFromImportedStructuredRow', () => {
});
});
+ it.each([
+ ['an ISO date-only value', '2023-12-25', '2023-12-25'],
+ ['a US-style date without time', '12/25/2023', '2023-12-25'],
+ [
+ 'a timezone-bearing value on its UTC day',
+ '2023-12-25T00:30:00Z',
+ '2023-12-25',
+ ],
+ [
+ 'a timezone-bearing value with a late UTC time',
+ '2023-12-25T23:30:00Z',
+ '2023-12-25',
+ ],
+ [
+ 'an offset-bearing value on its UTC day',
+ '2023-12-25T00:30:00-08:00',
+ '2023-12-25',
+ ],
+ ['an ISO date-time without a zone', '2023-12-25T10:30:00', '2023-12-25'],
+ [
+ 'an impossible date normalized rather than rejected',
+ '2023-02-30',
+ '2023-03-02',
+ ],
+ ])(
+ 'keeps the calendar day when importing %s into a DATE field',
+ (_label, importedDate, expectedDate) => {
+ const result = buildRecordFromImportedStructuredRow({
+ importedStructuredRow: { dateField: importedDate },
+ fieldMetadataItems: fields,
+ spreadsheetImportFields: [],
+ });
+
+ expect(result.dateField).toBe(expectedDate);
+ },
+ );
+
it('should successfully build a record from imported structured row with relation composite subfield', () => {
const importedStructuredRow: ImportedStructuredRow = {
'emailField (relationField)': '[email protected]',
diff --git a/packages/twenty-front/src/modules/object-record/spreadsheet-import/utils/buildRecordFromImportedStructuredRow.ts b/packages/twenty-front/src/modules/object-record/spreadsheet-import/utils/buildRecordFromImportedStructuredRow.ts
index 5331d1ec5d8..ca63818eeca 100644
--- a/packages/twenty-front/src/modules/object-record/spreadsheet-import/utils/buildRecordFromImportedStructuredRow.ts
+++ b/packages/twenty-front/src/modules/object-record/spreadsheet-import/utils/buildRecordFromImportedStructuredRow.ts
@@ -15,6 +15,8 @@ import {
isEmptyObject,
getLinkUrlNormalizer,
normalizeUrlOrigin,
+ parseToPlainDateOrThrow,
+ turnJSDateToPlainDate,
} from 'twenty-shared/utils';
import { z } from 'zod';
import { FieldMetadataType, RelationType } from '~/generated-metadata/graphql';
@@ -108,6 +110,14 @@ const buildRelationConnectFieldRecord = (
: { connect: { where: relationConnectFieldValue } };
};
+const computeDateOnlyImportedValue = (value: string): string => {
+ try {
+ return parseToPlainDateOrThrow(value).toString();
+ } catch {
+ return turnJSDateToPlainDate(new Date(value)).toString();
+ }
+};
+
export const buildRecordFromImportedStructuredRow = ({
fieldMetadataItems,
importedStructuredRow,
@@ -378,6 +388,14 @@ export const buildRecordFromImportedStructuredRow = ({
}
break;
case FieldMetadataType.DATE:
+ if (
+ isDefined(importedFieldValue) &&
+ isNonEmptyString(importedFieldValue)
+ ) {
+ recordToBuild[field.name] =
+ computeDateOnlyImportedValue(importedFieldValue);
+ }
+ break;
case FieldMetadataType.DATE_TIME:
if (
isDefined(importedFieldValue) &&