Change formatResult to return string instead of Date object for DATE_TIME (#17407)

This PR modifies our broadly used `formatResult` util to counter act
TypeORM transforming any date time to a `Date` object.

Instead we return the ISO string for any `DATE_TIME`, this way we're not
transporting Date object from one function to another in the backend.

We do this because there was problems working with events utils that
take string date time in parameters and received Date objects.

As this is a recurring problem and because it's an opinionated choice
from TypeORM, we chose to switch to string only in our codebase, from
TypeORM's output to frontend.
This commit is contained in:
Lucas Bordeau
2026-01-23 19:22:01 +01:00
committed by GitHub
parent 9ecab8fb82
commit a07590eba5
2 changed files with 35 additions and 2 deletions
@@ -182,6 +182,39 @@ export function formatResult<T>(
newData[dateField.name] = rawUpdatedDate;
}
const fieldMetadataItemsOfTypeDateTimeOnly =
getFlatFieldsFromFlatObjectMetadata(
flatObjectMetadata,
flatFieldMetadataMaps,
).filter((field) => field.type === FieldMetadataType.DATE_TIME);
for (const dateTimeField of fieldMetadataItemsOfTypeDateTimeOnly) {
// @ts-expect-error legacy noImplicitAny
const rawUpdatedDateTime = newData[dateTimeField.name] as
| string
| Date
| null
| undefined;
if (!isDefined(rawUpdatedDateTime)) {
continue;
}
if (typeof rawUpdatedDateTime === 'string') {
// @ts-expect-error legacy noImplicitAny
newData[dateTimeField.name] = rawUpdatedDateTime;
} else if (rawUpdatedDateTime instanceof Date) {
const dateIsoString = rawUpdatedDateTime.toISOString();
// @ts-expect-error legacy noImplicitAny
newData[dateTimeField.name] = dateIsoString;
} else {
throw new Error(
`Invalid DATE_TIME field "${dateTimeField.name}", value: "${rawUpdatedDateTime}", it should be a string or Date instance, (current type : ${typeof rawUpdatedDateTime}).`,
);
}
}
return newData as T;
}