Archived
1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
SOD/routes/utils/userCreate.js

54 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2020-08-20 14:50:59 +02:00
let models = require("../../models");
let error = require("../utils/error");
2020-08-25 15:40:06 +02:00
let emailCheck = require("./emailCheck");
2020-08-20 14:50:59 +02:00
module.exports = async (req, res) => {
if (!req.body.username || !req.body.email || !req.body.firstName || !req.body.lastName ||
!req.body.department || !req.body.password)
return error(req, res, "Invalid register !", 400, "Missing args");
if (await models.User.findByPk(req.body.username))
return error(req, res, "Invalid register !", 400, "Username already taken");
if (await models.User.findOne({where: {firstName: req.body.firstName,
lastName: req.body.lastName}}))
return error(req, res, "Invalid register !", 400, "First & last name already register");
if (await models.User.findOne({where: {email: req.body.email}}))
return error(req, res, "Invalid register !", 400, "Email already used");
let department = await models.Department.findByPk(req.body.department, {where: {enable: true}});
2020-08-20 14:50:59 +02:00
if (!department)
return error(req, res, "Invalid register !", 400, "Invalid department");
try {
let user = await models.User.create({
username: req.body.username,
email: req.body.email,
firstName: req.body.firstName,
lastName: req.body.lastName,
passwordHash: req.body.password
});
await user.setDepartment(department);
for (let c of await models.Order.findAll({where: {
firstName: user.firstName,
lastName: user.lastName,
UserUsername: null
}}))
await c.setUser(user);
2020-08-25 15:40:06 +02:00
if (!req.app.locals.test) {
new Promise(async done => await emailCheck(req, res, user, done));
await user.reload();
}
2020-08-20 14:50:59 +02:00
return user;
} catch (e) {
error(req, res, "Registration fail !");
throw e;
}
};