Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved history access to specialized service impl #366

Merged
merged 1 commit into from
Jan 18, 2023
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
13 changes: 5 additions & 8 deletions src/routers/config-strategy.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import express from 'express';
import { check, query } from 'express-validator';
import History from '../models/history';
import { EnvType } from '../models/environment';
import { validate, verifyInputUpdateParameters } from '../middleware/validators';
import { strategyRequirements, StrategiesType } from '../models/config-strategy';
import { auth } from '../middleware/auth';
import { verifyOwnership, sortBy } from '../helpers';
import { ActionTypes, RouterTypes } from '../models/permission';
import { getConfigById } from '../services/config';
import { getHistory, deleteHistory } from '../services/history';
import * as Services from '../services/config-strategy';
import { responseException } from '../exceptions';

Expand Down Expand Up @@ -75,12 +75,9 @@ router.get('/configstrategy/history/:id', auth, [
], validate, async (req, res) => {
try {
const configStrategy = await Services.getStrategyById(req.params.id);
const history = await History.find({ domainId: configStrategy.domain, elementId: configStrategy._id })
.select('oldValue newValue updatedBy date -_id')
.sort(sortBy(req.query))
.limit(parseInt(req.query.limit || 10))
.skip(parseInt(req.query.skip || 0))
.exec();

const query = 'oldValue newValue updatedBy date -_id';
const history = await getHistory(query, configStrategy.domain, configStrategy._id, req.query);

await verifyOwnership(req.admin, configStrategy, configStrategy.domain, ActionTypes.READ, RouterTypes.STRATEGY);

Expand All @@ -97,7 +94,7 @@ router.delete('/configstrategy/history/:id', auth, [
const configStrategy = await Services.getStrategyById(req.params.id);
await verifyOwnership(req.admin, configStrategy, configStrategy.domain, ActionTypes.DELETE, RouterTypes.ADMIN);

await History.deleteMany({ domainId: configStrategy.domain, elementId: configStrategy._id }).exec();
await deleteHistory(configStrategy.domain, configStrategy._id);
res.send(configStrategy);
} catch (e) {
responseException(res, e, 500);
Expand Down
13 changes: 5 additions & 8 deletions src/routers/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import express from 'express';
import { check, query } from 'express-validator';
import { relayOptions } from '../models/config';
import History from '../models/history';
import { auth } from '../middleware/auth';
import { ActionTypes, RouterTypes } from '../models/permission';
import { responseException } from '../exceptions';
Expand All @@ -10,6 +9,7 @@ import {
verifyInputUpdateParameters } from '../middleware/validators';
import { sortBy, verifyOwnership } from '../helpers';
import * as Services from '../services/config';
import { getHistory, deleteHistory } from '../services/history';
import { getGroupConfigById } from '../services/group-config';
import { SwitcherKeys } from '../external/switcher-api-facade';

Expand Down Expand Up @@ -80,12 +80,9 @@ router.get('/config/history/:id', auth, [
], validate, async (req, res) => {
try {
const config = await Services.getConfigById(req.params.id);
const history = await History.find({ domainId: config.domain, elementId: config._id })
.select('oldValue newValue updatedBy date -_id')
.sort(sortBy(req.query))
.limit(parseInt(req.query.limit || 10))
.skip(parseInt(req.query.skip || 0))
.exec();

const query = 'oldValue newValue updatedBy date -_id';
const history = await getHistory(query, config.domain, config._id, req.query);

await verifyOwnership(req.admin, config, config.domain, ActionTypes.READ, RouterTypes.CONFIG);

Expand All @@ -102,7 +99,7 @@ router.delete('/config/history/:id', auth, [
const config = await Services.getConfigById(req.params.id);
await verifyOwnership(req.admin, config, config.domain, ActionTypes.DELETE, RouterTypes.ADMIN);

await History.deleteMany({ domainId: config.domain, elementId: config._id }).exec();
await deleteHistory(config.domain, config._id);
res.send(config);
} catch (e) {
responseException(res, e, 500);
Expand Down
11 changes: 4 additions & 7 deletions src/routers/domain.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import express from 'express';
import History from '../models/history';
import { auth } from '../middleware/auth';
import { check } from 'express-validator';
import { validate, verifyInputUpdateParameters } from '../middleware/validators';
import { verifyOwnership, sortBy } from '../helpers';
import { ActionTypes, RouterTypes } from '../models/permission';
import { checkDomain, SwitcherKeys } from '../external/switcher-api-facade';
import * as Services from '../services/domain';
import { getHistory } from '../services/history';
import { responseException } from '../exceptions';

const router = new express.Router();
Expand Down Expand Up @@ -58,12 +58,9 @@ router.get('/domain/history/:id', auth, [
], validate, async (req, res) => {
try {
const domain = await Services.getDomainById(req.params.id);
const history = await History.find({ elementId: domain._id })
.select('oldValue newValue updatedBy date -_id')
.sort(sortBy(req.query))
.limit(parseInt(req.query.limit || 10))
.skip(parseInt(req.query.skip || 0))
.exec();

const query = 'oldValue newValue updatedBy date -_id';
const history = await getHistory(query, domain._id, undefined, req.query);

await verifyOwnership(req.admin, domain, domain._id, ActionTypes.READ, RouterTypes.DOMAIN);

Expand Down
13 changes: 5 additions & 8 deletions src/routers/group-config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import express from 'express';
import { check, query } from 'express-validator';
import History from '../models/history';
import { auth } from '../middleware/auth';
import { validate, verifyInputUpdateParameters } from '../middleware/validators';
import { sortBy, verifyOwnership } from '../helpers';
import { responseException } from '../exceptions';
import { ActionTypes, RouterTypes } from '../models/permission';
import * as Services from '../services/group-config';
import { getHistory, deleteHistory } from '../services/history';
import { getDomainById } from '../services/domain';
import { SwitcherKeys } from '../external/switcher-api-facade';

Expand Down Expand Up @@ -68,12 +68,9 @@ router.get('/groupconfig/history/:id', auth, [
], validate, async (req, res) => {
try {
const groupconfig = await Services.getGroupConfigById(req.params.id);
const history = await History.find({ domainId: groupconfig.domain, elementId: groupconfig._id })
.select('oldValue newValue updatedBy date -_id')
.sort(sortBy(req.query))
.limit(parseInt(req.query.limit || 10))
.skip(parseInt(req.query.skip || 0))
.exec();

const query = 'oldValue newValue updatedBy date -_id';
const history = await getHistory(query, groupconfig.domain, groupconfig._id, req.query);

await verifyOwnership(req.admin, groupconfig, groupconfig.domain, ActionTypes.READ, RouterTypes.GROUP);

Expand All @@ -90,7 +87,7 @@ router.delete('/groupconfig/history/:id', auth, [
const groupconfig = await Services.getGroupConfigById(req.params.id);
await verifyOwnership(req.admin, groupconfig, groupconfig.domain, ActionTypes.DELETE, RouterTypes.ADMIN);

await History.deleteMany({ domainId: groupconfig.domain, elementId: groupconfig._id }).exec();
await deleteHistory(groupconfig.domain, groupconfig._id);
res.send(groupconfig);
} catch (e) {
responseException(res, e, 500);
Expand Down
17 changes: 17 additions & 0 deletions src/services/history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import History from '../models/history';
import { sortBy } from '../helpers';

export async function getHistory(query, domainId, elementId, specs = {}) {
const findQuery = elementId ? { domainId, elementId } : { domainId };

return History.find(findQuery)
.select(query)
.sort(sortBy(specs))
.limit(parseInt(specs.limit || 10))
.skip(parseInt(specs.skip || 0))
.exec();
}

export async function deleteHistory(domainId, elementId) {
await History.deleteMany({ domainId, elementId }).exec();
}
24 changes: 24 additions & 0 deletions tests/fixtures/db_history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import mongoose from 'mongoose';
import History from '../../src/models/history';

export const domainId = new mongoose.Types.ObjectId();
export const element1Id = new mongoose.Types.ObjectId();
export const element2Id = new mongoose.Types.ObjectId();

export const addHistory = async (domainId, elementId, oldValue, newValue, date) => {
const history = {
id: new mongoose.Types.ObjectId(),
domainId,
elementId,
oldValue,
newValue,
date,
updatedBy: 'FIXTURE'
};

await new History(history).save();
};

export const setupDatabase = async () => {
await History.deleteMany().exec();
};
111 changes: 111 additions & 0 deletions tests/services/history.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
require('../../src/db/mongoose');

import mongoose from 'mongoose';
import { getHistory, deleteHistory } from '../../src/services/history';
import {
setupDatabase,
addHistory,
domainId,
element1Id,
element2Id
} from '../fixtures/db_history';

afterAll(async () => {
await new Promise(resolve => setTimeout(resolve, 1000));
await mongoose.disconnect();
});

describe('Testing history services', () => {
beforeEach(setupDatabase);

test('HISTORY_SERVICE - Should get history', async () => {
// given
const timestamp = Date.now();
await addHistory(domainId, element1Id, { value: 1 }, { value: 2 }, timestamp);

// test
const history = await getHistory('oldValue newValue updatedBy', domainId, element1Id);
expect(history[0].toJSON()).toEqual(
expect.objectContaining({
oldValue: { value: 1 },
newValue: { value: 2 },
updatedBy: 'FIXTURE'
})
);
});

test('HISTORY_SERVICE - Should get history - single entry', async () => {
// given
const timestamp = Date.now();
await addHistory(domainId, element1Id, { value: 1 }, { value: 2 }, timestamp);
await addHistory(domainId, element1Id, { value: 2 }, { value: 3 }, timestamp);
await addHistory(domainId, element2Id, { value: 4 }, { value: 5 }, timestamp);

// test
const history = await getHistory('elementId', domainId, element1Id, {
limit: 1
});

expect(history.length).toBe(1);
expect(history[0].elementId).toMatchObject(element1Id);
});

test('HISTORY_SERVICE - Should get history - skip first entry', async () => {
// given
const timestamp = Date.now();
await addHistory(domainId, element1Id, { value: 1 }, { value: 2 }, timestamp);
await addHistory(domainId, element1Id, { value: 3 }, { value: 4 }, timestamp);

// test
const history = await getHistory('oldValue', domainId, element1Id, {
skip: 1
});

expect(history[0].oldValue.toJSON()).toMatchObject({ value: 3 });
});

test('HISTORY_SERVICE - Should get history entries sorted by asc', async () => {
// given
const timestamp = Date.now();
await addHistory(domainId, element1Id, { value: 1 }, { value: 2 }, timestamp);
await addHistory(domainId, element1Id, { value: 3 }, { value: 4 }, timestamp);

// test
const history = await getHistory('oldValue', domainId, element1Id, {
sortBy: 'oldValue:asc'
});

expect(history[0].oldValue.toJSON()).toMatchObject({ value: 1 });
});

test('HISTORY_SERVICE - Should get history entries sorted by desc', async () => {
// given
const timestamp = Date.now();
await addHistory(domainId, element1Id, { value: 1 }, { value: 2 }, timestamp);
await addHistory(domainId, element1Id, { value: 3 }, { value: 4 }, timestamp);

// test
const history = await getHistory('oldValue', domainId, element1Id, {
sortBy: 'oldValue:desc'
});

expect(history[0].oldValue.toJSON()).toMatchObject({ value: 3 });
});

test('HISTORY_SERVICE - Should delete element history entries', async () => {
// given
const timestamp = Date.now();
await addHistory(domainId, element1Id, { value: 1 }, { value: 2 }, timestamp);
await addHistory(domainId, element1Id, { value: 3 }, { value: 4 }, timestamp);

// that
let history = await getHistory('oldValue', domainId, element1Id);
expect(history.length).toBe(2);

// test
await deleteHistory(domainId, element1Id);
history = await getHistory('oldValue', domainId, element1Id);
expect(history.length).toBe(0);
});

});