mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +01:00
Do not throw an error when adding the same push device twice (#10770)
* do not throw an error when adding the same push device twice * fix spelling * fix linting
This commit is contained in:
@@ -39,14 +39,16 @@ describe('POST /user/push-devices', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns an error if user already has the push device', async () => {
|
it('fails silently if user already has the push device', async () => {
|
||||||
await user.post('/user/push-devices', {type, regId});
|
await user.post('/user/push-devices', {type, regId});
|
||||||
await expect(user.post('/user/push-devices', {type, regId}))
|
const response = await user.post('/user/push-devices', {type, regId});
|
||||||
.to.eventually.be.rejected.and.eql({
|
await user.sync();
|
||||||
code: 401,
|
|
||||||
error: 'NotAuthorized',
|
expect(response.message).to.equal(t('pushDeviceAdded'));
|
||||||
message: t('pushDeviceAlreadyAdded'),
|
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 () => {
|
it('adds a push device to the user', async () => {
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { authWithHeaders } from '../../middlewares/auth';
|
import { authWithHeaders } from '../../middlewares/auth';
|
||||||
import {
|
import {
|
||||||
NotAuthorized,
|
|
||||||
NotFound,
|
NotFound,
|
||||||
} from '../../libs/errors';
|
} from '../../libs/errors';
|
||||||
import { model as PushDevice } from '../../models/pushDevice';
|
import { model as PushDevice } from '../../models/pushDevice';
|
||||||
@@ -39,8 +38,10 @@ api.addPushDevice = {
|
|||||||
type: req.body.type,
|
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)) {
|
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
|
// Concurrency safe update
|
||||||
|
|||||||
Reference in New Issue
Block a user