mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
Ported addPushDevice. Added unit tests. Added addPushDevice route. Added integration tests
This commit is contained in:
@@ -1,20 +1,43 @@
|
||||
import _ from 'lodash';
|
||||
import i18n from '../i18n';
|
||||
import splitWhitespace from '../libs/splitWhitespace';
|
||||
import {
|
||||
BadRequest,
|
||||
NotAuthorized,
|
||||
} from '../libs/errors';
|
||||
|
||||
module.exports = function addPushDevice (user, req = {}) {
|
||||
let regId = _.get(req, 'body.regId');
|
||||
if (!regId) throw new BadRequest(i18n.t('regIdRequired', req.language));
|
||||
|
||||
let type = _.get(req, 'body.type');
|
||||
if (!type) throw new BadRequest(i18n.t('typeRequired', req.language));
|
||||
|
||||
module.exports = function(user, req, cb) {
|
||||
var i, item, pd;
|
||||
if (!user.pushDevices) {
|
||||
user.pushDevices = [];
|
||||
}
|
||||
pd = user.pushDevices;
|
||||
item = {
|
||||
regId: req.body.regId,
|
||||
type: req.body.type
|
||||
|
||||
let pushDevices = user.pushDevices;
|
||||
|
||||
let item = {
|
||||
regId,
|
||||
type,
|
||||
};
|
||||
i = _.findIndex(pd, {
|
||||
regId: item.regId
|
||||
|
||||
let indexOfPushDevice = _.findIndex(pushDevices, {
|
||||
regId: item.regId,
|
||||
});
|
||||
if (i === -1) {
|
||||
pd.push(item);
|
||||
|
||||
if (indexOfPushDevice !== -1) {
|
||||
throw new NotAuthorized(i18n.t('pushDeviceAlreadyAdded', req.language));
|
||||
}
|
||||
return typeof cb === "function" ? cb(null, user.pushDevices) : void 0;
|
||||
|
||||
pushDevices.push(item);
|
||||
|
||||
let response = {
|
||||
data: _.pick(user, splitWhitespace('pushDevices')),
|
||||
message: i18n.t('pushDeviceAdded', req.language),
|
||||
};
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user