Skip to content

Commit

Permalink
WIP - prevent multiple notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
negue committed Jul 15, 2018
1 parent 08e925e commit 701c314
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
@@ -0,0 +1,33 @@
import {
createAndPopulateGroup,
} from '../../../../helpers/api-integration/v3';

describe('Prevent multiple notifications', () => {
let partyLeader, partyMembers, party;

before(async () => {
let { group, groupLeader, members } = await createAndPopulateGroup({
groupDetails: {
type: 'party',
privacy: 'private',
},
members: 4,
});

party = group;
partyLeader = groupLeader;
partyMembers = members;
});


it('does not add the same notification twice', async () => {
await partyMembers[0].post(`/groups/${party._id}/chat`, { message: 'Message 1'});
await partyMembers[1].post(`/groups/${party._id}/chat`, { message: 'Message 2'});
await partyMembers[2].post(`/groups/${party._id}/chat`, { message: 'Message 3'});
await partyMembers[3].post(`/groups/${party._id}/chat`, { message: 'Message 4'});

const userWithNotification = await partyLeader.get('/user');

expect(userWithNotification.notifications.length).to.be.eq(1);
});
});
20 changes: 13 additions & 7 deletions website/server/models/user/methods.js
Expand Up @@ -7,8 +7,8 @@ import {
model as Group,
} from '../group';

import { defaults, map, flatten, flow, compact, uniq, partialRight } from 'lodash';
import { model as UserNotification } from '../userNotification';
import {defaults, map, flatten, flow, compact, uniq, partialRight, filter, isEqual} from 'lodash';
import {model as UserNotification} from '../userNotification';
import schema from './schema';
import payments from '../../libs/payments/payments';
import amazonPayments from '../../libs/payments/amazon';
Expand Down Expand Up @@ -150,11 +150,17 @@ schema.methods.sendMessage = async function sendMessage (userToReceiveMessage, o
* @param seen If the notification should be marked as seen
*/
schema.methods.addNotification = function addUserNotification (type, data = {}, seen = false) {
this.notifications.push({
type,
data,
seen,
});
// only check if chat notification exists
const notifactionAlreadyExists = type === 'NEW_CHAT_MESSAGE' ?
filter(this.notifications, n => n.type === type && isEqual(n.data, data)) : -1;

if (notifactionAlreadyExists === -1) {
this.notifications.push({
type,
data,
seen,
});
}
};

/**
Expand Down

0 comments on commit 701c314

Please sign in to comment.