Fix date type update (#9700)
This PR fixes a problem with how TypeORM handles date without time. A date without time that is stored in PostgreSQL database as `date` type gets returned as an ISO string date with a timezone that can shift its date part in an unwanted way. In short DB stores `2025-01-01`, TypeORM query builder returns `2024-12-31T23:00:00Z` which gets parsed as `2024-12-31` on the front end field. We don't want to handle timezone here because we are manipulating a date without its time part, so this PR adds a step that counteracts what TypeORM does and returns `2025-01-01T00:00:00.000Z` so that the front can parse it correctly. @Weiko We might want to check other places of the backend where date types are returned by TypeORM, we might have the same problem, this PR only fixes it for updateOne resolver return. - Fixed date persist on frontend which was shifting the date to a different day due to timezone issue - Fixed date returned by the backend update logic, which was shifting the date by the timezone offset (so this PR adds back the offset so that it stays at 00:00:00Z time)
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
import { isPlainObject } from '@nestjs/common/utils/shared.utils';
|
||||
|
||||
import { isNonEmptyString } from '@sniptt/guards';
|
||||
import { isDefined } from 'class-validator';
|
||||
import { FieldMetadataType } from 'twenty-shared';
|
||||
|
||||
import { FieldMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/field-metadata.interface';
|
||||
@ -12,10 +14,12 @@ import { ObjectMetadataMaps } from 'src/engine/metadata-modules/types/object-met
|
||||
import { computeRelationType } from 'src/engine/twenty-orm/utils/compute-relation-type.util';
|
||||
import { getCompositeFieldMetadataCollection } from 'src/engine/twenty-orm/utils/get-composite-field-metadata-collection';
|
||||
import { isRelationFieldMetadataType } from 'src/engine/utils/is-relation-field-metadata-type.util';
|
||||
import { isDate } from 'src/utils/date/isDate';
|
||||
import { isValidDate } from 'src/utils/date/isValidDate';
|
||||
|
||||
export function formatResult<T>(
|
||||
data: any,
|
||||
ObjectMetadataItemWithFieldMaps: ObjectMetadataItemWithFieldMaps,
|
||||
objectMetadataItemWithFieldMaps: ObjectMetadataItemWithFieldMaps,
|
||||
objectMetadataMaps: ObjectMetadataMaps,
|
||||
): T {
|
||||
if (!data) {
|
||||
@ -24,7 +28,7 @@ export function formatResult<T>(
|
||||
|
||||
if (Array.isArray(data)) {
|
||||
return data.map((item) =>
|
||||
formatResult(item, ObjectMetadataItemWithFieldMaps, objectMetadataMaps),
|
||||
formatResult(item, objectMetadataItemWithFieldMaps, objectMetadataMaps),
|
||||
) as T;
|
||||
}
|
||||
|
||||
@ -32,12 +36,12 @@ export function formatResult<T>(
|
||||
return data;
|
||||
}
|
||||
|
||||
if (!ObjectMetadataItemWithFieldMaps) {
|
||||
if (!objectMetadataItemWithFieldMaps) {
|
||||
throw new Error('Object metadata is missing');
|
||||
}
|
||||
|
||||
const compositeFieldMetadataCollection = getCompositeFieldMetadataCollection(
|
||||
ObjectMetadataItemWithFieldMaps,
|
||||
objectMetadataItemWithFieldMaps,
|
||||
);
|
||||
|
||||
const compositeFieldMetadataMap = new Map(
|
||||
@ -58,7 +62,7 @@ export function formatResult<T>(
|
||||
);
|
||||
|
||||
const relationMetadataMap = new Map(
|
||||
Object.values(ObjectMetadataItemWithFieldMaps.fieldsById)
|
||||
Object.values(objectMetadataItemWithFieldMaps.fieldsById)
|
||||
.filter(({ type }) => isRelationFieldMetadataType(type))
|
||||
.map((fieldMetadata) => [
|
||||
fieldMetadata.name,
|
||||
@ -76,7 +80,7 @@ export function formatResult<T>(
|
||||
);
|
||||
const newData: object = {};
|
||||
const objectMetadaItemFieldsByName =
|
||||
objectMetadataMaps.byId[ObjectMetadataItemWithFieldMaps.id]?.fieldsByName;
|
||||
objectMetadataMaps.byId[objectMetadataItemWithFieldMaps.id]?.fieldsByName;
|
||||
|
||||
for (const [key, value] of Object.entries(data)) {
|
||||
const compositePropertyArgs = compositeFieldMetadataMap.get(key);
|
||||
@ -87,7 +91,7 @@ export function formatResult<T>(
|
||||
if (isPlainObject(value)) {
|
||||
newData[key] = formatResult(
|
||||
value,
|
||||
ObjectMetadataItemWithFieldMaps,
|
||||
objectMetadataItemWithFieldMaps,
|
||||
objectMetadataMaps,
|
||||
);
|
||||
} else if (objectMetadaItemFieldsByName[key]) {
|
||||
@ -142,6 +146,56 @@ export function formatResult<T>(
|
||||
newData[parentField][compositeProperty.name] = value;
|
||||
}
|
||||
|
||||
const dateFieldMetadataCollection =
|
||||
objectMetadataItemWithFieldMaps.fields.filter(
|
||||
(field) => field.type === FieldMetadataType.DATE,
|
||||
);
|
||||
|
||||
// This is a temporary fix to handle a bug in the frontend where the date gets returned in the wrong timezone,
|
||||
// thus returning the wrong date.
|
||||
//
|
||||
// In short, for example :
|
||||
// - DB stores `2025-01-01`
|
||||
// - TypeORM .returning() returns `2024-12-31T23:00:00.000Z`
|
||||
// - we shift +1h (or whatever the timezone offset is on the server)
|
||||
// - we return `2025-01-01T00:00:00.000Z`
|
||||
//
|
||||
// See this PR for more details: https://github.com/twentyhq/twenty/pull/9700
|
||||
const serverOffsetInMillisecondsToCounterActTypeORMAutomaticTimezoneShift =
|
||||
new Date().getTimezoneOffset() * 60 * 1000;
|
||||
|
||||
for (const dateFieldMetadata of dateFieldMetadataCollection) {
|
||||
const rawUpdatedDate = newData[dateFieldMetadata.name] as
|
||||
| string
|
||||
| null
|
||||
| undefined
|
||||
| Date;
|
||||
|
||||
if (!isDefined(rawUpdatedDate)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isDate(rawUpdatedDate)) {
|
||||
if (isValidDate(rawUpdatedDate)) {
|
||||
const shiftedDate = new Date(
|
||||
rawUpdatedDate.getTime() -
|
||||
serverOffsetInMillisecondsToCounterActTypeORMAutomaticTimezoneShift,
|
||||
);
|
||||
|
||||
newData[dateFieldMetadata.name] = shiftedDate;
|
||||
}
|
||||
} else if (isNonEmptyString(rawUpdatedDate)) {
|
||||
const currentDate = new Date(newData[dateFieldMetadata.name]);
|
||||
|
||||
const shiftedDate = new Date(
|
||||
new Date(currentDate).getTime() -
|
||||
serverOffsetInMillisecondsToCounterActTypeORMAutomaticTimezoneShift,
|
||||
);
|
||||
|
||||
newData[dateFieldMetadata.name] = shiftedDate;
|
||||
}
|
||||
}
|
||||
|
||||
return newData as T;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user