Addressing but where flag increases like count.

This commit is contained in:
David Jay
2016-11-18 11:50:12 -05:00
parent 53fa63b269
commit 2e6700e490
2 changed files with 19 additions and 9 deletions
+5 -7
View File
@@ -45,25 +45,23 @@ ActionSchema.statics.getActionSummaries = function(item_ids) {
return ActionSchema.statics.findByItemIdArray(item_ids).then((rawActions) => {
// Create an object with a count of each action type for each item
const actionSummaries = rawActions.reduce((actionObj, action) => {
if (!actionObj[action.item_id]) {
actionObj[action.item_id] = {
if (!actionObj[`${action.item_id}_${action.action_type}`]) {
actionObj[`${action.item_id}_${action.action_type}`] = {
id: action.id,
item_id: action.item_id,
item_type: action.item_type,
action_type: action.action_type,
count: 1,
current_user: false //Update this later when we have authentication
};
} else {
actionObj[action.item_id].count ++;
actionObj[`${action.item_id}_${action.action_type}`].count ++;
}
return actionObj;
}, {});
// Return an array extracted from the actionSummaries object
return Object.keys(actionSummaries).reduce((actions, key) => {
let actionSummary = actionSummaries[key];
actionSummary.item_id = key;
actions.push(actionSummary);
actions.push(actionSummaries[key]);
return actions;
}, []);
});
+14 -2
View File
@@ -22,6 +22,10 @@ describe('Action: models', () => {
action_type: 'flag',
item_id: '123',
item_type: 'comments'
}, {
action_type: 'like',
item_id: '123',
item_type: 'comments'
}]).then((actions) => {
mockActions = actions;
});
@@ -39,7 +43,7 @@ describe('Action: models', () => {
describe('#findByItemIdArray()', () => {
it('should find an array of actions from an array of item_ids', () => {
return Action.findByItemIdArray(['123', '456']).then((result) => {
expect(result).to.have.length(3);
expect(result).to.have.length(4);
});
});
});
@@ -47,10 +51,11 @@ describe('Action: models', () => {
describe('#getActionSummaries()', () => {
it('should return properly formatted summaries from an array of item_ids', () => {
return Action.getActionSummaries(['123', '789']).then((result) => {
expect(result).to.have.length(2);
expect(result).to.have.length(3);
const sorted = result.sort((a, b) => a.count - b.count);
delete sorted[0].id;
delete sorted[1].id;
delete sorted[2].id;
expect(sorted[0]).to.deep.equal({
action_type: 'like',
count: 1,
@@ -59,6 +64,13 @@ describe('Action: models', () => {
current_user: false
});
expect(sorted[1]).to.deep.equal({
action_type: 'like',
count: 1,
item_id: '123',
item_type: 'comments',
current_user: false
});
expect(sorted[2]).to.deep.equal({
action_type: 'flag',
count: 2,
item_id: '123',