-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include Date fields in aggregate operations on dates (#9479)
Follow-up on https://github.com/twentyhq/twenty/pull/9444/files - I had forgotten to include Date field types (in addition to DateTime)
- Loading branch information
Showing
13 changed files
with
176 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
.../src/modules/object-record/utils/convertAggregateOperationToExtendedAggregateOperation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
packages/twenty-front/src/utils/string/__tests__/formatDateTimeString.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { DateFormat } from '@/localization/constants/DateFormat'; | ||
import { TimeFormat } from '@/localization/constants/TimeFormat'; | ||
import { DateTime } from 'luxon'; | ||
import { formatDateTimeString } from '~/utils/string/formatDateTimeString'; | ||
|
||
describe('formatDateTimeString', () => { | ||
const defaultParams = { | ||
timeZone: 'UTC', | ||
dateFormat: DateFormat.DAY_FIRST, | ||
timeFormat: TimeFormat.HOUR_24, | ||
}; | ||
|
||
it('should return empty string for null value', () => { | ||
const result = formatDateTimeString({ | ||
...defaultParams, | ||
value: null, | ||
}); | ||
|
||
expect(result).toBe(''); | ||
}); | ||
|
||
it('should return empty string for undefined value', () => { | ||
const result = formatDateTimeString({ | ||
...defaultParams, | ||
value: undefined, | ||
}); | ||
|
||
expect(result).toBe(''); | ||
}); | ||
|
||
it('should format date as relative when displayAsRelativeDate is true', () => { | ||
const mockDate = DateTime.now().minus({ months: 2 }).toISO(); | ||
const mockRelativeDate = '2 months ago'; | ||
|
||
jest.mock('@/localization/utils/formatDateISOStringToRelativeDate', () => ({ | ||
formatDateISOStringToRelativeDate: jest | ||
.fn() | ||
.mockReturnValue(mockRelativeDate), | ||
})); | ||
|
||
const result = formatDateTimeString({ | ||
...defaultParams, | ||
value: mockDate, | ||
displayAsRelativeDate: true, | ||
}); | ||
|
||
expect(result).toBe(mockRelativeDate); | ||
}); | ||
|
||
it('should format date as datetime when displayAsRelativeDate is false', () => { | ||
const mockDate = '2023-01-01T12:00:00Z'; | ||
const mockFormattedDate = '1 Jan, 2023 12:00'; | ||
|
||
jest.mock('@/localization/utils/formatDateISOStringToDateTime', () => ({ | ||
formatDateISOStringToDateTime: jest | ||
.fn() | ||
.mockReturnValue(mockFormattedDate), | ||
})); | ||
|
||
const result = formatDateTimeString({ | ||
...defaultParams, | ||
value: mockDate, | ||
displayAsRelativeDate: false, | ||
}); | ||
|
||
expect(result).toBe(mockFormattedDate); | ||
}); | ||
|
||
it('should format date as datetime by default when displayAsRelativeDate is not provided', () => { | ||
const mockDate = '2023-01-01T12:00:00Z'; | ||
const mockFormattedDate = '1 Jan, 2023 12:00'; | ||
|
||
jest.mock('@/localization/utils/formatDateISOStringToDateTime', () => ({ | ||
formatDateISOStringToDateTime: jest | ||
.fn() | ||
.mockReturnValue(mockFormattedDate), | ||
})); | ||
|
||
const result = formatDateTimeString({ | ||
...defaultParams, | ||
value: mockDate, | ||
}); | ||
|
||
expect(result).toBe(mockFormattedDate); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/twenty-front/src/utils/string/formatDateTimeString.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { DateFormat } from '@/localization/constants/DateFormat'; | ||
import { TimeFormat } from '@/localization/constants/TimeFormat'; | ||
import { formatDateISOStringToDateTime } from '@/localization/utils/formatDateISOStringToDateTime'; | ||
import { formatDateISOStringToRelativeDate } from '@/localization/utils/formatDateISOStringToRelativeDate'; | ||
|
||
export const formatDateTimeString = ({ | ||
value, | ||
timeZone, | ||
dateFormat, | ||
timeFormat, | ||
displayAsRelativeDate, | ||
}: { | ||
timeZone: string; | ||
dateFormat: DateFormat; | ||
timeFormat: TimeFormat; | ||
value?: string | null; | ||
displayAsRelativeDate?: boolean; | ||
}) => { | ||
const formattedDate = value | ||
? displayAsRelativeDate | ||
? formatDateISOStringToRelativeDate(value) | ||
: formatDateISOStringToDateTime(value, timeZone, dateFormat, timeFormat) | ||
: ''; | ||
|
||
return formattedDate; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './constants/TwentyCompaniesBaseUrl'; | ||
export * from './constants/TwentyIconsBaseUrl'; | ||
export * from './utils/fieldMetadata/isFieldMetadataDateKind'; | ||
export * from './utils/image/getImageAbsoluteURI'; | ||
export * from './utils/strings'; | ||
|
12 changes: 12 additions & 0 deletions
12
packages/twenty-shared/src/utils/fieldMetadata/isFieldMetadataDateKind.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { FieldMetadataType } from 'src/types/FieldMetadataType'; | ||
|
||
export const isFieldMetadataDateKind = ( | ||
fieldMetadataType: FieldMetadataType, | ||
): fieldMetadataType is | ||
| FieldMetadataType.DATE | ||
| FieldMetadataType.DATE_TIME => { | ||
return ( | ||
fieldMetadataType === FieldMetadataType.DATE || | ||
fieldMetadataType === FieldMetadataType.DATE_TIME | ||
); | ||
}; |