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)
This commit is contained in:
@ -1,5 +1,4 @@
|
||||
import { DateFormat } from '@/localization/constants/DateFormat';
|
||||
import { TimeFormat } from '@/localization/constants/TimeFormat';
|
||||
import { DateTime } from 'luxon';
|
||||
import { formatDateString } from '~/utils/string/formatDateString';
|
||||
|
||||
@ -7,7 +6,6 @@ describe('formatDateString', () => {
|
||||
const defaultParams = {
|
||||
timeZone: 'UTC',
|
||||
dateFormat: DateFormat.DAY_FIRST,
|
||||
timeFormat: TimeFormat.HOUR_24,
|
||||
};
|
||||
|
||||
it('should return empty string for null value', () => {
|
||||
@ -49,7 +47,7 @@ describe('formatDateString', () => {
|
||||
|
||||
it('should format date as datetime when displayAsRelativeDate is false', () => {
|
||||
const mockDate = '2023-01-01T12:00:00Z';
|
||||
const mockFormattedDate = '1 Jan, 2023 12:00';
|
||||
const mockFormattedDate = '1 Jan, 2023';
|
||||
|
||||
jest.mock('@/localization/utils/formatDateISOStringToDateTime', () => ({
|
||||
formatDateISOStringToDateTime: jest
|
||||
@ -68,7 +66,7 @@ describe('formatDateString', () => {
|
||||
|
||||
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';
|
||||
const mockFormattedDate = '1 Jan, 2023';
|
||||
|
||||
jest.mock('@/localization/utils/formatDateISOStringToDateTime', () => ({
|
||||
formatDateISOStringToDateTime: jest
|
||||
|
||||
@ -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);
|
||||
});
|
||||
});
|
||||
@ -1,25 +1,22 @@
|
||||
import { DateFormat } from '@/localization/constants/DateFormat';
|
||||
import { TimeFormat } from '@/localization/constants/TimeFormat';
|
||||
import { formatDateISOStringToDateTime } from '@/localization/utils/formatDateISOStringToDateTime';
|
||||
import { formatDateISOStringToDate } from '@/localization/utils/formatDateISOStringToDate';
|
||||
import { formatDateISOStringToRelativeDate } from '@/localization/utils/formatDateISOStringToRelativeDate';
|
||||
|
||||
export const formatDateString = ({
|
||||
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)
|
||||
: formatDateISOStringToDate(value, timeZone, dateFormat)
|
||||
: '';
|
||||
|
||||
return formattedDate;
|
||||
|
||||
@ -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;
|
||||
};
|
||||
Reference in New Issue
Block a user