From 0905c86d3e139a1d87c6a13272782dd966993622 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Tue, 5 Sep 2023 23:01:16 -0700 Subject: [PATCH] Refactored recordHistory function --- src/models/common/index.js | 55 ++++--- tests/model/common-index.test.js | 237 +++++++++++++++++++++++++++++++ 2 files changed, 264 insertions(+), 28 deletions(-) create mode 100644 tests/model/common-index.test.js diff --git a/src/models/common/index.js b/src/models/common/index.js index 8f8293c..6b43724 100644 --- a/src/models/common/index.js +++ b/src/models/common/index.js @@ -12,11 +12,9 @@ function checkDifference(diff, documents, defaultIgnoredFields, if (typeof vals.oldValue === 'object' || typeof vals.newValue === 'object') { validateObjects(diff, documents, vals, keyArr, defaultIgnoredFields, keys, pos); - } else { - if (!Object.is(vals.oldValue, vals.newValue)) { - diff.oldValues.set(keys, vals.oldValue); - diff.newValues.set(keys, vals.newValue); - } + } else if (!Object.is(vals.oldValue, vals.newValue)) { + diff.oldValues.set(keys, vals.oldValue); + diff.newValues.set(keys, vals.newValue); } } } @@ -41,11 +39,9 @@ function validateObjects(diff, documents, vals, keyArr, }; checkDifference(diff, documents, defaultIgnoredFields, keyArr, keys, pos+1); - } else { - if (!Object.is(extractValue(vals.oldValue), extractValue(vals.newValue))) { - diff.oldValues.set(keys, extractValue(vals.oldValue)); - diff.newValues.set(keys, extractValue(vals.newValue)); - } + } else if (!Object.is(extractValue(vals.oldValue), extractValue(vals.newValue))) { + diff.oldValues.set(keys, extractValue(vals.oldValue)); + diff.newValues.set(keys, extractValue(vals.newValue)); } } @@ -63,13 +59,13 @@ function getValue(document, field) { if (document instanceof Map) { return typeof document.get(field) === 'boolean' ? String(document.get(field)) : document.get(field); - } else { - return typeof document[field] === 'boolean' ? - String(document[field]) : document[field]; } - } else { - return ''; + + return typeof document[field] === 'boolean' ? + String(document[field]) : document[field]; } + + return ''; } export async function recordHistory(modifiedField, oldDocument, newDocument, domainId, ignoredFields = []) { @@ -77,8 +73,9 @@ export async function recordHistory(modifiedField, oldDocument, newDocument, dom const diff = { oldValues: new Map(), newValues: new Map() }; const documents = { oldDocument, newDocument }; - if (!await checkHistory(domainId)) - return; + if (!await checkHistory(domainId) || process.env.HISTORY_ACTIVATED !== 'true') { + return undefined; + } if (ignoredFields.length) { ignoredFields.forEach(field => defaultIgnoredFields.push(field)); @@ -90,16 +87,18 @@ export async function recordHistory(modifiedField, oldDocument, newDocument, dom keyArr, keys.replace(/\./g, '/'), 0); }); - const history = new History({ - domainId, - elementId: newDocument._id, - oldValue: diff.oldValues, - newValue: diff.newValues, - updatedBy: newDocument.updatedBy, - date: Date.now() - }); - - if (diff.newValues.size > 0 && process.env.HISTORY_ACTIVATED === 'true') { - await history.save(); + if (diff.newValues.size > 0) { + const history = new History({ + domainId, + elementId: newDocument._id, + oldValue: diff.oldValues, + newValue: diff.newValues, + updatedBy: newDocument.updatedBy, + date: Date.now() + }); + + return history.save(); } + + return undefined; } \ No newline at end of file diff --git a/tests/model/common-index.test.js b/tests/model/common-index.test.js new file mode 100644 index 0000000..4b2ef72 --- /dev/null +++ b/tests/model/common-index.test.js @@ -0,0 +1,237 @@ +require('../../src/db/mongoose'); + +import mongoose from 'mongoose'; +import { recordHistory } from '../../src/models/common/index'; +import { setupDatabase } from '../fixtures/db_history'; + +afterAll(async () => { + await new Promise(resolve => setTimeout(resolve, 1000)); + await mongoose.disconnect(); +}); + +describe('Test recordHistory function', () => { + beforeAll(async () => await setupDatabase()); + + beforeEach(() => { + process.env.HISTORY_ACTIVATED = 'true'; + }); + + test('MODEL_COMMON - Should record history diff', async () => { + // Given + const _id = new mongoose.Types.ObjectId(); + const oldDocument = { + _id, + name: 'name', + description: 'test', + createdAt: '2020-07-11T08:00:00.000Z' + }; + + const newDocument = { + _id, + name: 'new name', + description: 'test', + createdAt: '2020-07-11T08:00:00.000Z', + updatedAt: '2020-07-12T08:00:00.000Z', + updatedBy: 'Automated test' + }; + + const domainId = new mongoose.Types.ObjectId(); + const ignoredFields = ['_id', 'updatedAt']; + const modifiedField = ['name']; + + // Test + const result = await recordHistory(modifiedField, oldDocument, newDocument, domainId, ignoredFields); + expect(result).toEqual( + expect.objectContaining({ + domainId, + elementId: _id, + oldValue: expect.any(Map), + newValue: expect.any(Map), + updatedBy: 'Automated test' + }) + ); + + expect(result.oldValue.get('name')).toEqual('name'); + expect(result.newValue.get('name')).toEqual('new name'); + }); + + test('MODEL_COMMON - Should NOT record history diff - HISTORY_ACTIVATED disabled', async () => { + // Given + process.env.HISTORY_ACTIVATED = 'false'; + + const _id = new mongoose.Types.ObjectId(); + const oldDocument = { + _id, + name: 'name', + description: 'test', + createdAt: '2020-07-11T08:00:00.000Z' + }; + + const newDocument = { + _id, + name: 'new name', + description: 'test', + createdAt: '2020-07-11T08:00:00.000Z', + updatedAt: '2020-07-12T08:00:00.000Z', + updatedBy: 'Automated test' + }; + + const domainId = new mongoose.Types.ObjectId(); + const ignoredFields = ['_id', 'updatedAt']; + const modifiedField = ['name']; + + // Test + const result = await recordHistory(modifiedField, oldDocument, newDocument, domainId, ignoredFields); + expect(result).toBeUndefined(); + }); + + test('MODEL_COMMON - Should NOT record history diff - "name" key not identified in the modifiedField', async () => { + // Given + const _id = new mongoose.Types.ObjectId(); + const oldDocument = { + _id, + name: 'name', + createdAt: '2020-07-11T08:00:00.000Z' + }; + + const newDocument = { + _id, + name: 'new name', + createdAt: '2020-07-11T08:00:00.000Z', + updatedAt: '2020-07-12T08:00:00.000Z', + updatedBy: 'Automated test' + }; + + const domainId = new mongoose.Types.ObjectId(); + const ignoredFields = ['_id', 'updatedAt']; + const modifiedField = ['description']; + + // Test + const result = await recordHistory(modifiedField, oldDocument, newDocument, domainId, ignoredFields); + expect(result).toBeUndefined(); + }); + + test('MODEL_COMMON - Should NOT record history diff - "name" key identified in the ignoredFields', async () => { + // Given + const _id = new mongoose.Types.ObjectId(); + const oldDocument = { + _id, + name: 'name', + createdAt: '2020-07-11T08:00:00.000Z' + }; + + const newDocument = { + _id, + name: 'new name', + createdAt: '2020-07-11T08:00:00.000Z', + updatedAt: '2020-07-12T08:00:00.000Z', + updatedBy: 'Automated test' + }; + + const domainId = new mongoose.Types.ObjectId(); + const ignoredFields = ['_id', 'updatedAt', 'name']; + const modifiedField = ['name']; + + // Test + const result = await recordHistory(modifiedField, oldDocument, newDocument, domainId, ignoredFields); + expect(result).toBeUndefined(); + }); + + test('MODEL_COMMON - Should record history diff - "name" key removed', async () => { + // Given + const _id = new mongoose.Types.ObjectId(); + const oldDocument = { + _id, + name: 'name', + createdAt: '2020-07-11T08:00:00.000Z' + }; + + const newDocument = { + _id, + createdAt: '2020-07-11T08:00:00.000Z', + updatedAt: '2020-07-12T08:00:00.000Z', + updatedBy: 'Automated test' + }; + + const domainId = new mongoose.Types.ObjectId(); + const ignoredFields = ['_id', 'updatedAt']; + const modifiedField = ['name']; + + // Test + const result = await recordHistory(modifiedField, oldDocument, newDocument, domainId, ignoredFields); + expect(result).toEqual( + expect.objectContaining({ + domainId, + elementId: _id, + oldValue: expect.any(Map), + newValue: expect.any(Map), + updatedBy: 'Automated test' + }) + ); + + expect(result.oldValue.get('name')).toEqual('name'); + expect(result.newValue.get('name')).toEqual(''); + }); + + test('MODEL_COMMON - Should record history diff - "name" key added', async () => { + // Given + const _id = new mongoose.Types.ObjectId(); + const oldDocument = { + _id, + createdAt: '2020-07-11T08:00:00.000Z' + }; + + const newDocument = { + _id, + name: 'new name', + createdAt: '2020-07-11T08:00:00.000Z', + updatedAt: '2020-07-12T08:00:00.000Z', + updatedBy: 'Automated test' + }; + + const domainId = new mongoose.Types.ObjectId(); + const ignoredFields = ['_id', 'updatedAt']; + const modifiedField = ['name']; + + // Test + const result = await recordHistory(modifiedField, oldDocument, newDocument, domainId, ignoredFields); + expect(result).toEqual( + expect.objectContaining({ + domainId, + elementId: _id, + oldValue: expect.any(Map), + newValue: expect.any(Map), + updatedBy: 'Automated test' + }) + ); + + expect(result.oldValue.get('name')).toEqual(''); + expect(result.newValue.get('name')).toEqual('new name'); + }); + + test('MODEL_COMMON - Should NOT record history diff - "name" key removed and ignored', async () => { + // Given + const _id = new mongoose.Types.ObjectId(); + const oldDocument = { + _id, + name: 'name', + createdAt: '2020-07-11T08:00:00.000Z' + }; + + const newDocument = { + _id, + createdAt: '2020-07-11T08:00:00.000Z', + updatedAt: '2020-07-12T08:00:00.000Z', + updatedBy: 'Automated test' + }; + + const domainId = new mongoose.Types.ObjectId(); + const ignoredFields = ['_id', 'updatedAt', 'name']; + const modifiedField = ['name']; + + // Test + const result = await recordHistory(modifiedField, oldDocument, newDocument, domainId, ignoredFields); + expect(result).toBeUndefined(); + }); + +}); \ No newline at end of file