1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 05:09:43 +02:00
planka/server/api/helpers/users/create-one.js

102 lines
2 KiB
JavaScript
Raw Normal View History

2019-08-31 04:07:25 +05:00
const bcrypt = require('bcrypt');
2022-12-26 21:10:50 +01:00
const valuesValidator = (value) => {
if (!_.isPlainObject(value)) {
return false;
}
if (!_.isString(value.email)) {
return false;
}
2023-10-17 19:18:19 +02:00
if (!_.isNil(value.password) && !_.isString(value.password)) {
2022-12-26 21:10:50 +01:00
return false;
}
2023-01-08 22:10:41 +01:00
if (!_.isNil(value.username) && !_.isString(value.username)) {
2022-12-26 21:10:50 +01:00
return false;
}
2022-12-26 21:10:50 +01:00
return true;
};
2022-12-26 21:10:50 +01:00
module.exports = {
inputs: {
values: {
type: 'json',
custom: valuesValidator,
required: true,
2019-08-31 04:07:25 +05:00
},
actorUser: {
type: 'ref',
required: true,
},
2019-08-31 04:07:25 +05:00
request: {
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
},
async fn(inputs) {
2022-12-26 21:10:50 +01:00
const { values } = inputs;
2023-10-17 19:18:19 +02:00
if (values.password) {
values.password = bcrypt.hashSync(values.password, 10);
}
2022-12-26 21:10:50 +01:00
if (values.username) {
values.username = values.username.toLowerCase();
2020-04-03 00:35:25 +05:00
}
2019-08-31 04:07:25 +05:00
const user = await User.create({
2022-12-26 21:10:50 +01:00
...values,
email: values.email.toLowerCase(),
2019-08-31 04:07:25 +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-08-31 04:07:25 +05:00
.fetch();
// 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',
{
item: user,
2019-08-31 04:07:25 +05:00
},
inputs.request,
2019-08-31 04:07:25 +05:00
);
});
sails.helpers.utils.sendWebhooks.with({
event: 'userCreate',
data: {
item: user,
},
user: inputs.actorUser,
});
return user;
},
2019-08-31 04:07:25 +05:00
};