* Added direct join functionality * Completed CreateUser test suite * Removed non-essential imports * Reordered test functions * Login test suite completed * Completed Logout test suite * Completed RequestReset test suite * Completed ResetPassword test suite * Completed UpdateUserDetails test suite * Completed SetAudio test suite * Fixed UpdateUserDetials suite * GetUserDetails test suite complete * Completed ResetPassword test suite * Completed GetMemberOrgs test suite * Completed GetOwnedOrgs test suite * AddOrgMember test suite added * Created CreateOrg test suite * Modified AddOrgMember suite * Delete Org test suite completed * Completed GetOrg test suite * Completed RemoveOrgMember test suite * Completed UpdateOrgDetails test suite * Changed oackage,json files * Switched project over to workspaces * Test build script completed * Fixed config.json missing on workflow run * Completed test-build.js.yml * Completed test-build.js.yml * Completed test-build.js.yml * Test build completed * Completed test-build.js.yml * Added touch server * Touched test-build.js.yml * Reworked workflows and automated test suite (#7) Reworked workflows and automated test suite (#7) * Configured new js file and isolated database files in db folder (#10) * Changed config.js and isolated db * Configured new js file and isolated database files in db folder * Removed .env files from config (#11)
69 lines
1.7 KiB
JavaScript
69 lines
1.7 KiB
JavaScript
const forge = require('node-forge');
|
|
const { User, Organization } = require('../../db/models/index');
|
|
|
|
async function CreateUser(req, res, next) {
|
|
const body = req.body;
|
|
|
|
if (!body.firstName || !body.lastName || !body.email || !body.password) {
|
|
return res.status(400).send('The request is missing required fields.');
|
|
}
|
|
|
|
const existingUsers = await User.count({
|
|
where: {
|
|
email: req.body.email,
|
|
},
|
|
});
|
|
|
|
if (existingUsers > 0) {
|
|
return res.status(500).send('A user with that email already exists.');
|
|
}
|
|
|
|
const passwordHash = forge.md.sha512
|
|
.create()
|
|
.update(body.password)
|
|
.digest()
|
|
.toHex();
|
|
|
|
if (body.orgID) {
|
|
try {
|
|
const org = await Organization.findByPk(body.orgID);
|
|
|
|
if (!org) {
|
|
const result = await User.create({
|
|
...body,
|
|
password: passwordHash,
|
|
});
|
|
|
|
return res.send(result);
|
|
}
|
|
|
|
const { orgID, ...filteredBody } = body;
|
|
|
|
const result = await org.createOrgMember({
|
|
...filteredBody,
|
|
password: passwordHash,
|
|
});
|
|
|
|
return res.send({ user: result, orgID });
|
|
} catch (e) {
|
|
console.log(e);
|
|
|
|
return res.status(500).send('Whoops, something went wrong!');
|
|
}
|
|
} else {
|
|
try {
|
|
const result = await User.create({
|
|
...body,
|
|
password: passwordHash,
|
|
});
|
|
|
|
return res.send(result);
|
|
} catch (e) {
|
|
console.log(e);
|
|
|
|
return res.status(500).send('Whoops, something went wrong!');
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = CreateUser;
|