add cause and region as org and creating an org

This commit is contained in:
Arjun Patel
2019-01-27 22:46:49 -08:00
parent 60590906a0
commit e77b31a8f2
5 changed files with 90 additions and 31 deletions
@@ -1,4 +1,5 @@
const db = require('../../config/db.config.js'),
bcrypt = require('bcrypt-nodejs'),
errorMaker = require('../../helpers/error.maker');
const { Grant, Cause, Region, Organization } = db;
@@ -25,36 +26,45 @@ exports.create = (req, res, next) => {
if (orgs.length >= 1) {
return next(errorMaker(409, `Email Exists: ${email}`));
} else {
const QUERY = `SELECT count(*) from causes `;
db.sequelize.query(QUERY, {
replacements: { causes, regions, max_orgs },
type: db.Sequelize.QueryTypes.SELECT
});
sequelize.query;
// hash and store
bcrypt.hash(password, null, null, function(error, hash) {
// Store hash in your password DB.
if (error) {
return next(error);
} else {
Organization.create({
name,
email,
password: hash, //hashed password
short_description,
primary_cause,
primary_region
})
.then(org => {
// Send created org to client
return res.status(201).json({
message: 'Organization created',
org
});
})
.catch(error => next(error));
}
});
const QUERY = `SELECT c.name, r.name \
FROM causes AS c, regions AS r
WHERE c.name = :primary_cause AND r.name = :primary_region`;
return db.sequelize
.query(QUERY, {
replacements: { primary_cause, primary_region },
type: db.Sequelize.QueryTypes.SELECT
})
.then(num => {
if (num.length < 1) {
// could not find the cause or region in the db
return next(errorMaker(401, `Not a valid cause or region`));
} else {
// hash and store
return bcrypt.hash(password, null, null, function(error, hash) {
// Store hash in your password DB.
if (error) {
return next(error);
} else {
Organization.create({
name,
email,
password: hash, //hashed password
short_description,
primary_cause,
primary_region
})
.then(org => {
// Send created org to client
return res.status(201).json({
message: 'Organization created',
org
});
})
.catch(error => next(error));
}
});
}
});
}
})
.catch(error => next(error));