2019-08-31 04:07:25 +05:00
|
|
|
const path = require('path');
|
|
|
|
const bcrypt = require('bcrypt');
|
2020-04-21 05:04:34 +05:00
|
|
|
const rimraf = require('rimraf');
|
2022-08-09 18:03:21 +02:00
|
|
|
const { v4: uuid } = require('uuid');
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
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-08-04 01:32:46 +05:00
|
|
|
custom: (value) => {
|
|
|
|
if (!_.isPlainObject(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isUndefined(value.email) && !_.isString(value.email)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isUndefined(value.password) && !_.isString(value.password)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value.username && !_.isString(value.username)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isUndefined(value.avatarUrl) && !_.isNull(value.avatarUrl)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
2022-08-09 18:03:21 +02:00
|
|
|
user: {
|
|
|
|
type: 'ref',
|
|
|
|
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: {
|
2020-04-03 00:35:25 +05:00
|
|
|
emailAlreadyInUse: {},
|
|
|
|
usernameAlreadyInUse: {},
|
2019-10-18 08:06:34 +05:00
|
|
|
},
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
async fn(inputs) {
|
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)) {
|
2022-08-09 18:03:21 +02:00
|
|
|
Object.assign(inputs.values, {
|
|
|
|
password: bcrypt.hashSync(inputs.values.password, 10),
|
|
|
|
passwordChangedAt: new Date().toUTCString(),
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
2020-04-03 00:35:25 +05:00
|
|
|
if (inputs.values.username) {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
inputs.values.username = inputs.values.username.toLowerCase();
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:04:34 +05:00
|
|
|
if (!_.isUndefined(inputs.values.avatarUrl)) {
|
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
inputs.values.avatarDirname = null;
|
|
|
|
delete inputs.values.avatarUrl;
|
|
|
|
/* eslint-enable no-param-reassign */
|
|
|
|
}
|
|
|
|
|
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"',
|
|
|
|
},
|
2020-04-03 00:35:25 +05:00
|
|
|
'emailAlreadyInUse',
|
|
|
|
)
|
|
|
|
.intercept(
|
|
|
|
{
|
|
|
|
message:
|
|
|
|
'Unexpected error from database adapter: conflicting key value violates exclusion constraint "user_username_unique"',
|
|
|
|
},
|
|
|
|
'usernameAlreadyInUse',
|
2019-11-05 18:01:42 +05:00
|
|
|
);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
if (user) {
|
2020-04-21 05:04:34 +05:00
|
|
|
if (inputs.record.avatarDirname && user.avatarDirname !== inputs.record.avatarDirname) {
|
2019-08-31 04:07:25 +05:00
|
|
|
try {
|
2020-04-21 05:04:34 +05:00
|
|
|
rimraf.sync(path.join(sails.config.custom.userAvatarsPath, inputs.record.avatarDirname));
|
2019-11-05 18:01:42 +05:00
|
|
|
} catch (error) {
|
|
|
|
console.warn(error.stack); // eslint-disable-line no-console
|
|
|
|
}
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
|
|
|
|
2022-08-09 18:03:21 +02:00
|
|
|
if (!_.isUndefined(inputs.values.password)) {
|
|
|
|
sails.sockets.broadcast(
|
|
|
|
`user:${user.id}`,
|
|
|
|
'userDelete', // TODO: introduce separate event
|
|
|
|
{
|
|
|
|
item: user,
|
|
|
|
},
|
|
|
|
inputs.request,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (user.id === inputs.user.id && inputs.request && inputs.request.isSocket) {
|
|
|
|
const tempRoom = uuid();
|
|
|
|
|
2022-10-03 12:11:19 +02:00
|
|
|
sails.sockets.addRoomMembersToRooms(`@user:${user.id}`, tempRoom, () => {
|
2022-08-09 18:03:21 +02:00
|
|
|
sails.sockets.leave(inputs.request, tempRoom, () => {
|
|
|
|
sails.sockets.leaveAll(tempRoom);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
2022-10-03 12:11:19 +02:00
|
|
|
sails.sockets.leaveAll(`@user:${user.id}`);
|
2022-08-09 18:03:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 08:06:34 +05:00
|
|
|
if (!isOnlyPasswordChange) {
|
2021-06-24 01:05:22 +05:00
|
|
|
/* const projectIds = await sails.helpers.users.getManagerProjectIds(user.id);
|
|
|
|
|
|
|
|
const userIds = _.union(
|
|
|
|
[user.id],
|
|
|
|
await sails.helpers.users.getAdminIds(),
|
|
|
|
await sails.helpers.projects.getManagerAndBoardMemberUserIds(projectIds),
|
|
|
|
); */
|
|
|
|
|
|
|
|
const users = await sails.helpers.users.getMany();
|
|
|
|
const userIds = sails.helpers.utils.mapRecords(users);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2020-04-03 00:35:25 +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
|
|
|
}
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
return user;
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|