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:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user