2019-08-31 04:07:25 +05:00
|
|
|
const Errors = {
|
|
|
|
USER_NOT_FOUND: {
|
2019-11-05 18:01:42 +05:00
|
|
|
notFound: 'User is not found',
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
inputs: {
|
|
|
|
id: {
|
2019-10-10 02:51:54 +05:00
|
|
|
type: 'string',
|
|
|
|
regex: /^[0-9]+$/,
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
isAdmin: {
|
2019-11-05 18:01:42 +05:00
|
|
|
type: 'boolean',
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
name: {
|
|
|
|
type: 'string',
|
2019-11-05 18:01:42 +05:00
|
|
|
isNotEmptyString: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
avatar: {
|
|
|
|
type: 'json',
|
2019-11-05 18:01:42 +05:00
|
|
|
custom: (value) => _.isNull(value),
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
|
|
|
exits: {
|
|
|
|
notFound: {
|
2019-11-05 18:01:42 +05:00
|
|
|
responseType: 'notFound',
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
2019-11-05 18:01:42 +05:00
|
|
|
async fn(inputs, exits) {
|
2019-08-31 04:07:25 +05:00
|
|
|
const { currentUser } = this.req;
|
|
|
|
|
|
|
|
if (!currentUser.isAdmin) {
|
|
|
|
if (inputs.id !== currentUser.id) {
|
|
|
|
throw Errors.USER_NOT_FOUND; // Forbidden
|
|
|
|
}
|
|
|
|
|
|
|
|
delete inputs.isAdmin;
|
|
|
|
}
|
|
|
|
|
|
|
|
let user = await sails.helpers.getUser(inputs.id);
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
throw Errors.USER_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
const values = _.pick(inputs, ['isAdmin', 'name', 'avatar']);
|
|
|
|
|
|
|
|
user = await sails.helpers.updateUser(user, values, this.req);
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
throw Errors.USER_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
return exits.success({
|
2019-11-05 18:01:42 +05:00
|
|
|
item: user,
|
2019-08-31 04:07:25 +05:00
|
|
|
});
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|