Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/db/file/pushes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import _ from 'lodash';
import Datastore from '@seald-io/nedb';
import { Action } from '../../proxy/actions/Action';
import { toClass } from '../helper';
Expand Down Expand Up @@ -49,6 +48,16 @@ const defaultPushQuery: Partial<PushQuery> = {
type: 'push',
};

// Matches the JSON round trip in toClass: Date values become their toJSON() strings
const datesToJSON = <T>(value: T): T => {
if (value instanceof Date) return value.toJSON() as T;
if (value && typeof value === 'object') {
const obj = value as Record<string, unknown>;
for (const key of Object.keys(obj)) obj[key] = datesToJSON(obj[key]);
}
return value;
};

export const getPushes = (query: Partial<PushQuery>): Promise<Action[]> => {
if (!query) query = defaultPushQuery;
return new Promise((resolve, reject) => {
Expand All @@ -60,11 +69,8 @@ export const getPushes = (query: Partial<PushQuery>): Promise<Action[]> => {
if (err) {
reject(err);
} else {
resolve(
_.chain(docs)
.map((x) => toClass(x, Action.prototype))
.value(),
);
// neDB's find() already deep-copies docs except Date values, so only those need detaching
resolve(docs.map((x) => Object.setPrototypeOf(datesToJSON(x), Action.prototype)));
}
});
});
Expand Down
18 changes: 18 additions & 0 deletions test/db/file/pushes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,22 @@ describe('File DB - Pushes', () => {
expect(pushesModule.db.findOne).toHaveBeenCalledWith({ id: pushId }, expect.any(Function));
});
});
describe('getPushes', () => {
it('should return dates as strings detached from the stored document', async () => {
const id = 'test-push-dates';
const timestamp = new Date('2026-01-02T03:04:05.000Z');
await pushesModule.db.insertAsync({ id, type: 'push', rejection: { timestamp } });

try {
const [push] = (await pushesModule.getPushes({ id } as any)) as any[];
expect(push.rejection.timestamp).toBe('2026-01-02T03:04:05.000Z');

push.rejection.timestamp = 'changed';
const stored = await pushesModule.db.findOneAsync({ id });
expect(stored.rejection.timestamp.toISOString()).toBe('2026-01-02T03:04:05.000Z');
} finally {
await pushesModule.db.removeAsync({ id }, {});
}
});
});
});
Loading