2019-08-31 04:07:25 +05:00
|
|
|
const Errors = {
|
|
|
|
USER_NOT_FOUND: {
|
2020-04-03 00:35:25 +05:00
|
|
|
userNotFound: 'User not found',
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|
|
|
|
|
2022-12-26 21:10:50 +01:00
|
|
|
const avatarUrlValidator = (value) => _.isNull(value);
|
|
|
|
|
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
|
|
|
},
|
2020-04-21 05:04:34 +05:00
|
|
|
avatarUrl: {
|
2019-08-31 04:07:25 +05:00
|
|
|
type: 'json',
|
2022-12-26 21:10:50 +01:00
|
|
|
custom: avatarUrlValidator,
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2020-04-09 18:27:28 +05:00
|
|
|
phone: {
|
|
|
|
type: 'string',
|
|
|
|
isNotEmptyString: true,
|
|
|
|
allowNull: true,
|
|
|
|
},
|
|
|
|
organization: {
|
|
|
|
type: 'string',
|
|
|
|
isNotEmptyString: true,
|
|
|
|
allowNull: true,
|
|
|
|
},
|
2022-07-26 12:26:42 +02:00
|
|
|
language: {
|
|
|
|
type: 'string',
|
|
|
|
isNotEmptyString: true,
|
|
|
|
allowNull: true,
|
|
|
|
},
|
2020-04-10 00:11:34 +05:00
|
|
|
subscribeToOwnCards: {
|
|
|
|
type: 'boolean',
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
|
|
|
exits: {
|
2020-04-03 00:35:25 +05:00
|
|
|
userNotFound: {
|
2019-11-05 18:01:42 +05:00
|
|
|
responseType: 'notFound',
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
async fn(inputs) {
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-03 18:42:31 +05:00
|
|
|
delete inputs.isAdmin; // eslint-disable-line no-param-reassign
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
let user = await sails.helpers.users.getOne(inputs.id);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
throw Errors.USER_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2023-09-12 01:12:38 +02:00
|
|
|
if (user.email === sails.config.custom.defaultAdminEmail) {
|
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
delete inputs.isAdmin;
|
|
|
|
delete inputs.name;
|
|
|
|
/* eslint-enable no-param-reassign */
|
|
|
|
}
|
|
|
|
|
2022-12-24 00:47:59 +01:00
|
|
|
const values = {
|
|
|
|
..._.pick(inputs, [
|
|
|
|
'isAdmin',
|
|
|
|
'name',
|
|
|
|
'phone',
|
|
|
|
'organization',
|
|
|
|
'language',
|
|
|
|
'subscribeToOwnCards',
|
|
|
|
]),
|
|
|
|
avatar: inputs.avatarUrl,
|
|
|
|
};
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2022-12-26 21:10:50 +01:00
|
|
|
user = await sails.helpers.users.updateOne.with({
|
|
|
|
values,
|
|
|
|
record: user,
|
|
|
|
user: currentUser,
|
|
|
|
request: this.req,
|
|
|
|
});
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
throw Errors.USER_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
return {
|
2019-11-05 18:01:42 +05:00
|
|
|
item: user,
|
2021-06-24 01:05:22 +05:00
|
|
|
};
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|