add cause and region as org and creating an org
This commit is contained in:
@@ -0,0 +1,18 @@
|
|||||||
|
const db = require('../../config/db.config.js'),
|
||||||
|
errorMaker = require('../../helpers/error.maker');
|
||||||
|
|
||||||
|
const { Grant, Cause, Region, Organization } = db;
|
||||||
|
|
||||||
|
// ADD a cause into the database
|
||||||
|
exports.create = (req, res, next) => {
|
||||||
|
const { name } = req.body;
|
||||||
|
|
||||||
|
Cause.create({ name })
|
||||||
|
.then(cause => {
|
||||||
|
res.status(200).json({
|
||||||
|
message: 'Cause created',
|
||||||
|
cause
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch(error => next(error));
|
||||||
|
};
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
organization: require('./organization.controller')
|
organization: require('./organization.controller'),
|
||||||
|
cause: require('./cause.controller'),
|
||||||
|
region: require('./region.controller')
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
const db = require('../../config/db.config.js'),
|
const db = require('../../config/db.config.js'),
|
||||||
|
bcrypt = require('bcrypt-nodejs'),
|
||||||
errorMaker = require('../../helpers/error.maker');
|
errorMaker = require('../../helpers/error.maker');
|
||||||
|
|
||||||
const { Grant, Cause, Region, Organization } = db;
|
const { Grant, Cause, Region, Organization } = db;
|
||||||
@@ -25,36 +26,45 @@ exports.create = (req, res, next) => {
|
|||||||
if (orgs.length >= 1) {
|
if (orgs.length >= 1) {
|
||||||
return next(errorMaker(409, `Email Exists: ${email}`));
|
return next(errorMaker(409, `Email Exists: ${email}`));
|
||||||
} else {
|
} else {
|
||||||
const QUERY = `SELECT count(*) from causes `;
|
const QUERY = `SELECT c.name, r.name \
|
||||||
db.sequelize.query(QUERY, {
|
FROM causes AS c, regions AS r
|
||||||
replacements: { causes, regions, max_orgs },
|
WHERE c.name = :primary_cause AND r.name = :primary_region`;
|
||||||
type: db.Sequelize.QueryTypes.SELECT
|
return db.sequelize
|
||||||
});
|
.query(QUERY, {
|
||||||
sequelize.query;
|
replacements: { primary_cause, primary_region },
|
||||||
// hash and store
|
type: db.Sequelize.QueryTypes.SELECT
|
||||||
bcrypt.hash(password, null, null, function(error, hash) {
|
})
|
||||||
// Store hash in your password DB.
|
.then(num => {
|
||||||
if (error) {
|
if (num.length < 1) {
|
||||||
return next(error);
|
// could not find the cause or region in the db
|
||||||
} else {
|
return next(errorMaker(401, `Not a valid cause or region`));
|
||||||
Organization.create({
|
} else {
|
||||||
name,
|
// hash and store
|
||||||
email,
|
return bcrypt.hash(password, null, null, function(error, hash) {
|
||||||
password: hash, //hashed password
|
// Store hash in your password DB.
|
||||||
short_description,
|
if (error) {
|
||||||
primary_cause,
|
return next(error);
|
||||||
primary_region
|
} else {
|
||||||
})
|
Organization.create({
|
||||||
.then(org => {
|
name,
|
||||||
// Send created org to client
|
email,
|
||||||
return res.status(201).json({
|
password: hash, //hashed password
|
||||||
message: 'Organization created',
|
short_description,
|
||||||
org
|
primary_cause,
|
||||||
});
|
primary_region
|
||||||
})
|
})
|
||||||
.catch(error => next(error));
|
.then(org => {
|
||||||
}
|
// Send created org to client
|
||||||
});
|
return res.status(201).json({
|
||||||
|
message: 'Organization created',
|
||||||
|
org
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch(error => next(error));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => next(error));
|
.catch(error => next(error));
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
const db = require('../../config/db.config.js'),
|
||||||
|
errorMaker = require('../../helpers/error.maker');
|
||||||
|
|
||||||
|
const { Grant, Cause, Region, Organization } = db;
|
||||||
|
|
||||||
|
// ADD a region into the database
|
||||||
|
exports.create = (req, res, next) => {
|
||||||
|
const { name } = req.body;
|
||||||
|
|
||||||
|
Region.create({ name })
|
||||||
|
.then(region => {
|
||||||
|
res.status(200).json({
|
||||||
|
message: 'Region created',
|
||||||
|
region
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch(error => next(error));
|
||||||
|
};
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
const express = require('express'),
|
const express = require('express'),
|
||||||
router = express.Router(),
|
router = express.Router(),
|
||||||
|
checkAuth = require('../middleware/check-auth'),
|
||||||
roles = require('../helpers/roles');
|
roles = require('../helpers/roles');
|
||||||
|
|
||||||
const controllers = require('../controllers/organization');
|
const controllers = require('../controllers/organization');
|
||||||
@@ -7,4 +8,14 @@ const controllers = require('../controllers/organization');
|
|||||||
// POST Signup route
|
// POST Signup route
|
||||||
router.post('/', controllers.organization.create);
|
router.post('/', controllers.organization.create);
|
||||||
|
|
||||||
|
// POST add cause
|
||||||
|
router.post('/cause', checkAuth(roles.ORGANIZATION), controllers.cause.create);
|
||||||
|
|
||||||
|
// POST add region
|
||||||
|
router.post(
|
||||||
|
'/region',
|
||||||
|
checkAuth(roles.ORGANIZATION),
|
||||||
|
controllers.region.create
|
||||||
|
);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
Reference in New Issue
Block a user