mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-27 19:00:42 -04:00
fix(api): handle string numbers in normalizeDate (#64188)
This commit is contained in:
@@ -193,6 +193,10 @@ describe('normalize', () => {
|
||||
'Unexpected date value: {"date":"123"}'
|
||||
);
|
||||
});
|
||||
|
||||
test('should handle string numbers', () => {
|
||||
expect(normalizeDate('1696118400000')).toEqual(1696118400000);
|
||||
});
|
||||
});
|
||||
|
||||
describe('normalizeChallengeType', () => {
|
||||
|
||||
@@ -76,9 +76,16 @@ export const normalizeDate = (date?: Prisma.JsonValue): number => {
|
||||
typeof date.$date === 'string'
|
||||
) {
|
||||
return new Date(date.$date).getTime();
|
||||
} else {
|
||||
throw Error('Unexpected date value: ' + JSON.stringify(date));
|
||||
} else if (typeof date === 'string') {
|
||||
const parsed = Number(date);
|
||||
if (!isNaN(parsed)) {
|
||||
// Number() handles invalid strings e.g. '2023-10-01T00:00:00Z'
|
||||
// parseInt() handles floats
|
||||
return parseInt(String(parsed));
|
||||
}
|
||||
}
|
||||
|
||||
throw Error('Unexpected date value: ' + JSON.stringify(date));
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user