2019-10-18 08:06:34 +05:00
|
|
|
const bcrypt = require('bcrypt');
|
|
|
|
|
|
|
|
const Errors = {
|
|
|
|
USER_NOT_FOUND: {
|
2020-04-03 00:35:25 +05:00
|
|
|
userNotFound: 'User not found',
|
2019-10-18 08:06:34 +05:00
|
|
|
},
|
2020-04-03 00:35:25 +05:00
|
|
|
INVALID_CURRENT_PASSWORD: {
|
|
|
|
invalidCurrentPassword: 'Invalid current password',
|
2019-10-18 08:06:34 +05:00
|
|
|
},
|
2020-04-03 00:35:25 +05:00
|
|
|
EMAIL_ALREADY_IN_USE: {
|
|
|
|
emailAlreadyInUse: 'Email already in use',
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-10-18 08:06:34 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
inputs: {
|
|
|
|
id: {
|
|
|
|
type: 'string',
|
|
|
|
regex: /^[0-9]+$/,
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-10-18 08:06:34 +05:00
|
|
|
},
|
|
|
|
email: {
|
|
|
|
type: 'string',
|
|
|
|
isEmail: true,
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-10-18 08:06:34 +05:00
|
|
|
},
|
|
|
|
currentPassword: {
|
|
|
|
type: 'string',
|
2019-11-05 18:01:42 +05:00
|
|
|
isNotEmptyString: true,
|
|
|
|
},
|
2019-10-18 08:06:34 +05:00
|
|
|
},
|
|
|
|
|
|
|
|
exits: {
|
2020-04-03 00:35:25 +05:00
|
|
|
userNotFound: {
|
2019-11-05 18:01:42 +05:00
|
|
|
responseType: 'notFound',
|
2019-10-18 08:06:34 +05:00
|
|
|
},
|
2020-04-03 00:35:25 +05:00
|
|
|
invalidCurrentPassword: {
|
2019-11-05 18:01:42 +05:00
|
|
|
responseType: 'forbidden',
|
2019-10-18 08:06:34 +05:00
|
|
|
},
|
2020-04-03 00:35:25 +05:00
|
|
|
emailAlreadyInUse: {
|
2019-11-05 18:01:42 +05:00
|
|
|
responseType: 'conflict',
|
|
|
|
},
|
2019-10-18 08:06:34 +05:00
|
|
|
},
|
|
|
|
|
2019-11-05 18:01:42 +05:00
|
|
|
async fn(inputs, exits) {
|
2019-10-18 08:06:34 +05:00
|
|
|
const { currentUser } = this.req;
|
|
|
|
|
|
|
|
if (inputs.id === currentUser.id) {
|
|
|
|
if (!inputs.currentPassword) {
|
2020-04-03 00:35:25 +05:00
|
|
|
throw Errors.INVALID_CURRENT_PASSWORD;
|
2019-10-18 08:06:34 +05:00
|
|
|
}
|
|
|
|
} else if (!currentUser.isAdmin) {
|
|
|
|
throw Errors.USER_NOT_FOUND; // Forbidden
|
|
|
|
}
|
|
|
|
|
|
|
|
let user = await sails.helpers.getUser(inputs.id);
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
throw Errors.USER_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2020-02-03 18:42:31 +05:00
|
|
|
inputs.id === currentUser.id &&
|
|
|
|
!bcrypt.compareSync(inputs.currentPassword, user.password)
|
2019-10-18 08:06:34 +05:00
|
|
|
) {
|
2020-04-03 00:35:25 +05:00
|
|
|
throw Errors.INVALID_CURRENT_PASSWORD;
|
2019-10-18 08:06:34 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
const values = _.pick(inputs, ['email']);
|
|
|
|
|
|
|
|
user = await sails.helpers
|
|
|
|
.updateUser(user, values, this.req)
|
2020-04-03 00:35:25 +05:00
|
|
|
.intercept('emailAlreadyInUse', () => Errors.EMAIL_ALREADY_IN_USE);
|
2019-10-18 08:06:34 +05:00
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
throw Errors.USER_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
return exits.success({
|
2019-11-05 18:01:42 +05:00
|
|
|
item: user.email,
|
2019-10-18 08:06:34 +05:00
|
|
|
});
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-10-18 08:06:34 +05:00
|
|
|
};
|