1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-18 20:59:44 +02:00
planka/server/api/helpers/create-user.js

51 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-08-31 04:07:25 +05:00
const bcrypt = require('bcrypt');
module.exports = {
inputs: {
values: {
type: 'json',
// 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: {
type: 'ref',
},
2019-08-31 04:07:25 +05:00
},
exits: {
conflict: {},
2019-08-31 04:07:25 +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(),
password: bcrypt.hashSync(inputs.values.password, 10),
2019-08-31 04:07:25 +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();
userIds.forEach((userId) => {
2019-08-31 04:07:25 +05:00
sails.sockets.broadcast(
`user:${userId}`,
'userCreate',
{
item: user,
2019-08-31 04:07:25 +05:00
},
inputs.request,
2019-08-31 04:07:25 +05:00
);
});
return exits.success(user);
},
2019-08-31 04:07:25 +05:00
};