Skip to content
Merged
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
55 changes: 27 additions & 28 deletions src/models/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand All @@ -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));
}
}

Expand All @@ -63,22 +59,23 @@ 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 = []) {
const defaultIgnoredFields = ['_id', 'updatedAt'];
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));
Expand All @@ -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;
}
237 changes: 237 additions & 0 deletions tests/model/common-index.test.js
Original file line number Diff line number Diff line change
@@ -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();
});

});