Skip to content

Commit

Permalink
do not throw an error when adding the same push device twice
Browse files Browse the repository at this point in the history
  • Loading branch information
paglias committed Oct 20, 2018
1 parent 52fd6a1 commit d196c56
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
16 changes: 9 additions & 7 deletions test/api/v3/integration/user/POST-user_push_device.test.js
Expand Up @@ -39,14 +39,16 @@ describe('POST /user/push-devices', () => {
});
});

it('returns an error if user already has the push device', async () => {
it('fail silently if user already has the push device', async () => {
await user.post('/user/push-devices', {type, regId});
await expect(user.post('/user/push-devices', {type, regId}))
.to.eventually.be.rejected.and.eql({
code: 401,
error: 'NotAuthorized',
message: t('pushDeviceAlreadyAdded'),
});
const response = await user.post('/user/push-devices', {type, regId});
await user.sync();

expect(response.message).to.equal(t('pushDeviceAdded'));
expect(response.data[0].type).to.equal(type);
expect(response.data[0].regId).to.equal(regId);
expect(user.pushDevices[0].type).to.equal(type);
expect(user.pushDevices[0].regId).to.equal(regId);
});

it('adds a push device to the user', async () => {
Expand Down
4 changes: 3 additions & 1 deletion website/server/controllers/api-v3/pushNotifications.js
Expand Up @@ -39,8 +39,10 @@ api.addPushDevice = {
type: req.body.type,
};

// When adding a duplicate push device, fail silently instead of throwing an error
if (pushDevices.find(device => device.regId === item.regId)) {
throw new NotAuthorized(res.t('pushDeviceAlreadyAdded'));
res.respond(200, user.pushDevices, res.t('pushDeviceAdded'));
return;
}

// Concurrency safe update
Expand Down

0 comments on commit d196c56

Please sign in to comment.