Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
12 changes: 9 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ module.exports.deepMerge = deepMerge;
*
* @param {object|Array} source - source object
* @param {object|Array} target - target object
* @param {Array<string>} [requiredFields] - fields to leave in diff object
* @returns {object}
*/
function deepDiff(source, target) {
function deepDiff(source, target, requiredFields= []) {
if (typeOf(target) === 'array') {
return arrayDiff(source, target);
} else if (typeOf(target) === 'object') {
return objectDiff(source, target);
return objectDiff(source, target, requiredFields);
} else if (source !== target) {
return target;
} else {
Expand Down Expand Up @@ -62,10 +63,11 @@ function arrayDiff(source, target) {
*
* @param {object} objectA - first object for comparing
* @param {object} objectB - second object for comparing
* @param {Array<string>} requiredFields - fields to leave
*
* @returns {object}
*/
function objectDiff(objectA, objectB) {
function objectDiff(objectA, objectB, requiredFields = []) {
const diffObject = {};

/**
Expand Down Expand Up @@ -93,6 +95,10 @@ function objectDiff(objectA, objectB) {
}

if (objectAItem === objectBItem) {
if (requiredFields.includes(prop)) {
diffObject[prop] = objectAItem;
}

return;
}

Expand Down
8 changes: 8 additions & 0 deletions lib/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,12 @@ describe('Utils', () => {
expect(merge).toEqual(testCase.expectedMerge);
});
});

test('should leave required fields', () => {
const data = dataProvider[1];

const diff = utils.deepDiff(data.sourceObject, data.targetObject, ['a']);

expect(diff.a).toEqual(data.sourceObject.a);
})
});
50 changes: 50 additions & 0 deletions migrations/20210826165556-add-timestamp-to-repetitions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* This migration sets timestamp for event repetitions if it was omitted because it's the same as original event one
*/
module.exports = {
async up(db, client) {
const collections = await db.listCollections({}, {
authorizedCollections: true,
nameOnly: true,
}).toArray();

const projectIds = [];
const REPETITIONS = 'repetitions';
const EVENTS = 'events';

collections.forEach((collection) => {
if (/repetitions/.test(collection.name)) {
projectIds.push(collection.name.split(':')[1]);
}
});

const session = client.startSession();

try {
session.withTransaction(async () => {
for (const projectId of projectIds) {
const originalEvents = await db.collection(`${EVENTS}:${projectId}`).find({}).toArray();
const repetitions = await db.collection(`${REPETITIONS}:${projectId}`).find({
'payload.timestamp': {$eq: null}
}).toArray();

for (const event of originalEvents) {
const eventRepetitions = repetitions.filter(rep => rep.groupHash === event.groupHash);

for (const repetition of eventRepetitions) {
await db.collection(`${REPETITIONS}:${projectId}`).updateOne({
_id: repetition._id,
}, {
$set: {
'payload.timestamp': event.payload.timestamp,
},
});
}
}
}
})
} finally {
session.endSession();
}
},
};
4 changes: 3 additions & 1 deletion workers/grouper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ export default class GrouperWorker extends Worker {

/**
* Save event's repetitions
*
* Leave timestamp in diff for database queries
*/
const diff = utils.deepDiff(existedEvent.payload, task.event);
const diff = utils.deepDiff(existedEvent.payload, task.event, [ 'timestamp' ]);

const newRepetition = {
groupHash: uniqueEventHash,
Expand Down