2019-08-31 04:07:25 +05:00
|
|
|
const bcrypt = require('bcrypt');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
inputs: {
|
|
|
|
values: {
|
|
|
|
type: 'json',
|
2019-11-05 18:01:42 +05:00
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
custom: (value) => _.isPlainObject(value) && _.isString(value.email) && _.isString(value.password),
|
|
|
|
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: {
|
2019-11-05 18:01:42 +05:00
|
|
|
conflict: {},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
2019-11-05 18:01:42 +05:00
|
|
|
async fn(inputs, exits) {
|
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"',
|
|
|
|
},
|
|
|
|
'conflict',
|
|
|
|
)
|
2019-08-31 04:07:25 +05:00
|
|
|
.fetch();
|
|
|
|
|
|
|
|
const userIds = await sails.helpers.getAdminUserIds();
|
|
|
|
|
2019-11-05 18:01:42 +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
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return exits.success(user);
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|