2019-08-31 04:07:25 +05:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const bcrypt = require('bcrypt');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
inputs: {
|
|
|
|
record: {
|
|
|
|
type: 'ref',
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
values: {
|
|
|
|
type: 'json',
|
2020-02-03 18:42:31 +05:00
|
|
|
custom: value =>
|
|
|
|
_.isPlainObject(value) &&
|
|
|
|
(_.isUndefined(value.email) || _.isString(value.email)) &&
|
|
|
|
(_.isUndefined(value.password) || _.isString(value.password)),
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
request: {
|
2019-11-05 18:01:42 +05:00
|
|
|
type: 'ref',
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
2019-10-18 08:06:34 +05:00
|
|
|
exits: {
|
2019-11-05 18:01:42 +05:00
|
|
|
conflict: {},
|
2019-10-18 08:06:34 +05:00
|
|
|
},
|
|
|
|
|
2019-11-05 18:01:42 +05:00
|
|
|
async fn(inputs, exits) {
|
2019-08-31 04:07:25 +05:00
|
|
|
if (!_.isUndefined(inputs.values.email)) {
|
2020-02-03 18:42:31 +05:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2019-08-31 04:07:25 +05:00
|
|
|
inputs.values.email = inputs.values.email.toLowerCase();
|
|
|
|
}
|
|
|
|
|
2019-10-18 08:06:34 +05:00
|
|
|
let isOnlyPasswordChange = false;
|
|
|
|
|
2019-08-31 04:07:25 +05:00
|
|
|
if (!_.isUndefined(inputs.values.password)) {
|
2020-02-03 18:42:31 +05:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2019-08-31 04:07:25 +05:00
|
|
|
inputs.values.password = bcrypt.hashSync(inputs.values.password, 10);
|
2019-10-18 08:06:34 +05:00
|
|
|
|
|
|
|
if (Object.keys(inputs.values).length === 1) {
|
|
|
|
isOnlyPasswordChange = true;
|
|
|
|
}
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
const user = await User.updateOne({
|
|
|
|
id: inputs.record.id,
|
2019-11-05 18:01:42 +05:00
|
|
|
deletedAt: null,
|
2019-10-18 08:06:34 +05:00
|
|
|
})
|
|
|
|
.set(inputs.values)
|
2019-11-05 18:01:42 +05:00
|
|
|
.intercept(
|
|
|
|
{
|
|
|
|
message:
|
|
|
|
'Unexpected error from database adapter: conflicting key value violates exclusion constraint "user_email_unique"',
|
|
|
|
},
|
|
|
|
'conflict',
|
|
|
|
);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
if (user) {
|
|
|
|
if (inputs.record.avatar && user.avatar !== inputs.record.avatar) {
|
|
|
|
try {
|
2019-11-05 18:01:42 +05:00
|
|
|
fs.unlinkSync(path.join(sails.config.custom.uploadsPath, inputs.record.avatar));
|
|
|
|
} catch (error) {
|
|
|
|
console.warn(error.stack); // eslint-disable-line no-console
|
|
|
|
}
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
|
|
|
|
2019-10-18 08:06:34 +05:00
|
|
|
if (!isOnlyPasswordChange) {
|
|
|
|
const adminUserIds = await sails.helpers.getAdminUserIds();
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2019-11-05 18:01:42 +05:00
|
|
|
const projectIds = await sails.helpers.getMembershipProjectIdsForUser(user.id);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2019-11-05 18:01:42 +05:00
|
|
|
const userIdsForProject = await sails.helpers.getMembershipUserIdsForProject(projectIds);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2019-10-18 08:06:34 +05:00
|
|
|
const userIds = _.union([user.id], adminUserIds, userIdsForProject);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2020-02-03 18:42:31 +05:00
|
|
|
userIds.forEach(userId => {
|
2019-10-18 08:06:34 +05:00
|
|
|
sails.sockets.broadcast(
|
|
|
|
`user:${userId}`,
|
|
|
|
'userUpdate',
|
|
|
|
{
|
2019-11-05 18:01:42 +05:00
|
|
|
item: user,
|
2019-10-18 08:06:34 +05:00
|
|
|
},
|
2019-11-05 18:01:42 +05:00
|
|
|
inputs.request,
|
2019-10-18 08:06:34 +05:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
return exits.success(user);
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|