diff --git a/app/src/api/services/__tests__/editHistory.test.ts b/app/src/api/services/__tests__/editHistory.test.ts new file mode 100644 index 00000000..5db92f5b --- /dev/null +++ b/app/src/api/services/__tests__/editHistory.test.ts @@ -0,0 +1,43 @@ +import { mocked } from 'ts-jest/utils'; + +import { EditHistoryEntry } from '../../../frontend/models/edit-history-entry'; +import * as editHistoryData from '../../dataAccess/editHistory'; // manually mocked +import { getGlobalEditHistory } from '../editHistory'; + +jest.mock('../../dataAccess/editHistory'); + +const mockedEditHistoryData = editHistoryData as typeof import('../../dataAccess/__mocks__/editHistory'); + +describe('getGlobalEditHistory()', () => { + mockedEditHistoryData.__setHistory( + [...Array(20).keys()].map(i => ({ + revision_id: (100 + i) + '', + revision_timestamp: new Date(2019, 10, 1, 17, 20 + i).toISOString(), + username: 'testuser', + building_id: 1234567, + forward_patch: {}, + reverse_patch: {} + })) + ); + + describe('without before/after parameters', () => { + it.each( + [ + [0, []], + [3, ['119', '118', '117']], + [6, ['119', '118', '117', '116', '115', '114']] + ] + )('should return the N latest records in descending order', async (count: number, ids: string[]) => { + const historyResult = await getGlobalEditHistory(null, null, count); + + expect(historyResult.history.map(h => h.revision_id)).toEqual(ids); + }); + }); + + describe('with after ID parameter', () => { + }); + + describe('with before ID parameter', () => { + + }); +}); diff --git a/app/src/api/services/editHistory.ts b/app/src/api/services/editHistory.ts index e0891621..33c00698 100644 --- a/app/src/api/services/editHistory.ts +++ b/app/src/api/services/editHistory.ts @@ -1,20 +1,28 @@ -import db from '../../db'; +import { EditHistoryEntry } from '../../frontend/models/edit-history-entry'; +import { getHistoryAfterId, getHistoryBeforeId, getIdNewerThan, getIdOlderThan, getLatestHistory } from '../dataAccess/editHistory'; -async function getGlobalEditHistory() { - try { - return await db.manyOrNone( - `SELECT log_id as revision_id, forward_patch, reverse_patch, date_trunc('minute', log_timestamp) as revision_timestamp, username, building_id - FROM logs, users - WHERE logs.user_id = users.user_id - AND log_timestamp >= now() - interval '7 days' - ORDER BY log_timestamp DESC` - ); - } catch (error) { - console.error(error); - return []; +async function getGlobalEditHistory(beforeId?: string, afterId?: string, count: number = 100) { + // limited set of records. Expected to be already ordered from newest to oldest + let editHistoryRecords: EditHistoryEntry[]; + + if(afterId != undefined) { + editHistoryRecords = await getHistoryAfterId(afterId, count); + } else if (beforeId != undefined) { + editHistoryRecords = await getHistoryBeforeId(beforeId, count); + } else { + editHistoryRecords = await getLatestHistory(count); } -} + const newer = getIdNewerThan(editHistoryRecords[0]?.revision_id); + const older = getIdOlderThan(editHistoryRecords[editHistoryRecords.length-1]?.revision_id); + return { + history: editHistoryRecords, + paging: { + has_newer: newer != undefined, + has_older: older != undefined + } + }; +} export { getGlobalEditHistory