Skip to content

Commit

Permalink
fix: support _updatedAt from multiple formats
Browse files Browse the repository at this point in the history
  • Loading branch information
7sete7 committed Feb 20, 2025
1 parent e06ccdc commit 2ac043d
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 5 deletions.
50 changes: 50 additions & 0 deletions __test__/utils/dateUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { DateTime } from 'luxon';
import { getUpdatedDate } from '../../src/imports/utils/dateUtils';

describe('getUpdatedDate', () => {
const testISOString = '2024-02-20T10:30:00.000Z';
const testDate = new Date(testISOString);
const expectedDateTime = DateTime.fromISO(testISOString);

it('should handle undefined input', () => {
const result = getUpdatedDate(undefined);
expect(result).toBeUndefined();
});

it('should parse Date object correctly', () => {
const result = getUpdatedDate(testDate);
expect(result?.toISO()).toBe(expectedDateTime.toISO());
});

it('should parse ISO string correctly', () => {
const result = getUpdatedDate(testISOString);
expect(result?.toISO()).toBe(expectedDateTime.toISO());
});

it('should parse MongoDB style object with Date correctly', () => {
const result = getUpdatedDate({ $date: testDate });
expect(result?.toISO()).toBe(expectedDateTime.toISO());
});

it('should parse MongoDB style object with ISO string correctly', () => {
const result = getUpdatedDate({ $date: testISOString });
expect(result?.toISO()).toBe(expectedDateTime.toISO());
});

it('should handle invalid date string', () => {
const result = getUpdatedDate('invalid-date');
expect(result?.isValid).toBe(false);
});

it('should handle null value', () => {
// @ts-expect-error Testing null input even though types don't allow it
const result = getUpdatedDate(null);
expect(result).toBeUndefined();
});

it('should handle empty object', () => {
// @ts-expect-error Testing invalid object input
const result = getUpdatedDate({});
expect(result).toBeUndefined();
});
});
9 changes: 4 additions & 5 deletions src/imports/data/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { getNextUserFromQueue as getNext } from '../meta/getNextUserFromQueue';
import { validateAndProcessValueFor } from '../meta/validateAndProcessValueFor';
import { renderTemplate } from '../template';
import { convertStringOfFieldsSeparatedByCommaIntoObjectToFind } from '../utils/convertStringOfFieldsSeparatedByCommaIntoObjectToFind';
import { getUpdatedDate } from '../utils/dateUtils';
import { randomId } from '../utils/random';
import { errorReturn, successReturn } from '../utils/return';
import { handleTransactionError, retryMongoTransaction } from '../utils/transaction';
Expand Down Expand Up @@ -1116,16 +1117,14 @@ export async function update({ authTokenId, document, data, contextUser, tracing

// outdateRecords are records that user are trying to update but they are out of date
if (metaObject.ignoreUpdatedAt !== true && isRetry === false && isFromInterfaceUpload === false) {
const getUpdatedDate = id => "$date" in id._updatedAt ? DateTime.fromISO(id._updatedAt.$date) : DateTime.fromJSDate(new Date(id._updatedAt));

const outdateRecords = data.ids.filter(id => {
const record = originals[id._id];
if (record == null) {
return true;
}
const updatedDate = getUpdatedDate(id);
const updatedDate = getUpdatedDate(id._updatedAt);

if (DateTime.fromJSDate(record._updatedAt).diff(updatedDate).milliseconds !== 0) {
if (updatedDate == null || DateTime.fromJSDate(record._updatedAt).diff(updatedDate).milliseconds > 500) {
return true;
}
return false;
Expand All @@ -1140,7 +1139,7 @@ export async function update({ authTokenId, document, data, contextUser, tracing
$or: outdateRecords.map(record => ({
dataId: record._id,
createdAt: {
$gt: getUpdatedDate(record).toJSDate(),
$gt: getUpdatedDate(record._updatedAt)?.toJSDate(),
},
$or: mapOfFieldsToUpdateForHistoryQuery,
})),
Expand Down
37 changes: 37 additions & 0 deletions src/imports/utils/dateUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { DateTime } from 'luxon';
import { logger } from './logger';

interface DateWithDate {
$date: string | Date;
}

/**
* Parses a date value that can come in different formats into a DateTime object
* @param dateValue - The date value to parse which can be a string, Date object, object with $date property or undefined
* @returns DateTime object representing the parsed date or undefined if input is invalid
*/
export function getUpdatedDate(dateValue: string | Date | DateWithDate | undefined): DateTime | undefined {
try {
if (!dateValue) {
return undefined;
}

if (dateValue instanceof Date) {
return DateTime.fromJSDate(dateValue);
}

if (typeof dateValue === 'string') {
return DateTime.fromISO(dateValue);
}

if ('$date' in dateValue) {
const mongoDate = dateValue.$date;
return mongoDate instanceof Date ? DateTime.fromJSDate(mongoDate) : DateTime.fromISO(mongoDate);
}

return undefined;
} catch (error) {
logger.error(error, 'Error parsing _updatedAt date');
return undefined;
}
}

0 comments on commit 2ac043d

Please sign in to comment.