2020-04-14 22:29:11 +02:00
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
'use strict';
|
2016-10-26 10:43:11 +02:00
|
|
|
|
2020-04-14 22:29:11 +02:00
|
|
|
const { diff } = require('deep-diff');
|
2016-12-09 14:50:30 +01:00
|
|
|
const {
|
|
|
|
STRATEGY_CREATED,
|
|
|
|
STRATEGY_DELETED,
|
2016-12-17 12:31:23 +01:00
|
|
|
STRATEGY_UPDATED,
|
2019-03-13 19:10:13 +01:00
|
|
|
STRATEGY_IMPORT,
|
|
|
|
DROP_STRATEGIES,
|
2016-12-09 14:50:30 +01:00
|
|
|
FEATURE_CREATED,
|
|
|
|
FEATURE_UPDATED,
|
|
|
|
FEATURE_ARCHIVED,
|
|
|
|
FEATURE_REVIVED,
|
2019-03-13 19:10:13 +01:00
|
|
|
FEATURE_IMPORT,
|
|
|
|
DROP_FEATURES,
|
2020-02-27 21:26:18 +01:00
|
|
|
CONTEXT_FIELD_CREATED,
|
|
|
|
CONTEXT_FIELD_UPDATED,
|
|
|
|
CONTEXT_FIELD_DELETED,
|
2016-12-09 14:50:30 +01:00
|
|
|
} = require('./event-type');
|
2015-01-26 16:54:50 +01:00
|
|
|
|
2019-03-13 19:10:13 +01:00
|
|
|
const strategyTypes = [
|
|
|
|
STRATEGY_CREATED,
|
|
|
|
STRATEGY_DELETED,
|
|
|
|
STRATEGY_UPDATED,
|
|
|
|
STRATEGY_IMPORT,
|
|
|
|
DROP_STRATEGIES,
|
|
|
|
];
|
2015-01-26 16:54:50 +01:00
|
|
|
|
2016-12-27 21:03:50 +01:00
|
|
|
const featureTypes = [
|
2016-12-09 14:50:30 +01:00
|
|
|
FEATURE_CREATED,
|
|
|
|
FEATURE_UPDATED,
|
|
|
|
FEATURE_ARCHIVED,
|
|
|
|
FEATURE_REVIVED,
|
2019-03-13 19:10:13 +01:00
|
|
|
FEATURE_IMPORT,
|
|
|
|
DROP_FEATURES,
|
2015-01-26 16:54:50 +01:00
|
|
|
];
|
|
|
|
|
2020-02-27 21:26:18 +01:00
|
|
|
const contextTypes = [
|
|
|
|
CONTEXT_FIELD_CREATED,
|
|
|
|
CONTEXT_FIELD_DELETED,
|
|
|
|
CONTEXT_FIELD_UPDATED,
|
|
|
|
];
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
function baseTypeFor(event) {
|
2015-01-26 16:54:50 +01:00
|
|
|
if (featureTypes.indexOf(event.type) !== -1) {
|
|
|
|
return 'features';
|
2020-04-14 22:29:11 +02:00
|
|
|
}
|
|
|
|
if (strategyTypes.indexOf(event.type) !== -1) {
|
2015-01-26 16:54:50 +01:00
|
|
|
return 'strategies';
|
2020-04-14 22:29:11 +02:00
|
|
|
}
|
|
|
|
if (contextTypes.indexOf(event.type) !== -1) {
|
2020-02-27 21:26:18 +01:00
|
|
|
return 'context';
|
2015-01-26 16:54:50 +01:00
|
|
|
}
|
2016-06-18 22:23:19 +02:00
|
|
|
throw new Error(`unknown event type: ${JSON.stringify(event)}`);
|
2015-01-26 16:54:50 +01:00
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
function groupByBaseTypeAndName(events) {
|
2016-06-18 21:53:18 +02:00
|
|
|
const groups = {};
|
2015-01-26 16:54:50 +01:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
events.forEach(event => {
|
|
|
|
const baseType = baseTypeFor(event);
|
2015-01-26 16:54:50 +01:00
|
|
|
|
|
|
|
groups[baseType] = groups[baseType] || {};
|
2017-06-28 10:17:14 +02:00
|
|
|
groups[baseType][event.data.name] =
|
|
|
|
groups[baseType][event.data.name] || [];
|
2015-01-26 16:54:50 +01:00
|
|
|
|
|
|
|
groups[baseType][event.data.name].push(event);
|
|
|
|
});
|
|
|
|
|
|
|
|
return groups;
|
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
function eachConsecutiveEvent(events, callback) {
|
2016-06-18 21:53:18 +02:00
|
|
|
const groups = groupByBaseTypeAndName(events);
|
2015-01-26 16:54:50 +01:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
Object.keys(groups).forEach(baseType => {
|
|
|
|
const group = groups[baseType];
|
2015-01-26 16:54:50 +01:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
Object.keys(group).forEach(name => {
|
2016-06-18 22:23:19 +02:00
|
|
|
const currentEvents = group[name];
|
2016-06-18 21:53:18 +02:00
|
|
|
let left;
|
|
|
|
let right;
|
|
|
|
let i;
|
|
|
|
let l;
|
2016-06-18 22:23:19 +02:00
|
|
|
for (i = 0, l = currentEvents.length; i < l; i++) {
|
2016-12-27 21:03:50 +01:00
|
|
|
left = currentEvents[i];
|
2016-06-18 22:23:19 +02:00
|
|
|
right = currentEvents[i + 1];
|
2015-01-26 16:54:50 +01:00
|
|
|
|
|
|
|
callback(left, right);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
function addDiffs(events) {
|
2020-04-14 22:29:11 +02:00
|
|
|
// TODO: no-param-reassign
|
2016-06-18 21:53:18 +02:00
|
|
|
eachConsecutiveEvent(events, (left, right) => {
|
2015-01-26 16:54:50 +01:00
|
|
|
if (right) {
|
|
|
|
left.diffs = diff(right.data, left.data);
|
|
|
|
left.diffs = left.diffs || [];
|
|
|
|
} else {
|
|
|
|
left.diffs = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2016-06-18 21:55:46 +02:00
|
|
|
addDiffs,
|
2016-04-24 22:41:37 +02:00
|
|
|
};
|