2019-08-31 04:07:25 +05:00
|
|
|
const bcrypt = require('bcrypt');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
inputs: {
|
|
|
|
values: {
|
|
|
|
type: 'json',
|
2020-08-04 01:32:46 +05:00
|
|
|
custom: (value) => {
|
|
|
|
if (!_.isPlainObject(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isString(value.email)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isString(value.password)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value.username && !_.isString(value.username)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
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
|
|
|
},
|
|
|
|
|
|
|
|
exits: {
|
2020-04-03 00:35:25 +05:00
|
|
|
emailAlreadyInUse: {},
|
|
|
|
usernameAlreadyInUse: {},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
async fn(inputs) {
|
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();
|
|
|
|
}
|
|
|
|
|
2019-08-31 04:07:25 +05:00
|
|
|
const user = await User.create({
|
|
|
|
...inputs.values,
|
|
|
|
email: inputs.values.email.toLowerCase(),
|
2019-11-05 18:01:42 +05:00
|
|
|
password: bcrypt.hashSync(inputs.values.password, 10),
|
2019-08-31 04:07:25 +05:00
|
|
|
})
|
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
|
|
|
.fetch();
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
// const userIds = await sails.helpers.users.getAdminIds();
|
|
|
|
|
|
|
|
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-08-31 04:07:25 +05:00
|
|
|
sails.sockets.broadcast(
|
|
|
|
`user:${userId}`,
|
|
|
|
'userCreate',
|
|
|
|
{
|
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
|
|
|
inputs.request,
|
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
|
|
|
};
|