diff --git a/app/controllers/organization/cause.controller.js b/app/controllers/organization/cause.controller.js new file mode 100644 index 0000000..fb45014 --- /dev/null +++ b/app/controllers/organization/cause.controller.js @@ -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)); +}; diff --git a/app/controllers/organization/index.js b/app/controllers/organization/index.js index e650b45..193d956 100644 --- a/app/controllers/organization/index.js +++ b/app/controllers/organization/index.js @@ -1,3 +1,5 @@ module.exports = { - organization: require('./organization.controller') + organization: require('./organization.controller'), + cause: require('./cause.controller'), + region: require('./region.controller') }; diff --git a/app/controllers/organization/organization.controller.js b/app/controllers/organization/organization.controller.js index 2f2102b..4281d5f 100644 --- a/app/controllers/organization/organization.controller.js +++ b/app/controllers/organization/organization.controller.js @@ -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)); diff --git a/app/controllers/organization/region.controller.js b/app/controllers/organization/region.controller.js new file mode 100644 index 0000000..8cb882a --- /dev/null +++ b/app/controllers/organization/region.controller.js @@ -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)); +}; diff --git a/app/routes/organization.route.js b/app/routes/organization.route.js index 70c7fb3..48a13d1 100644 --- a/app/routes/organization.route.js +++ b/app/routes/organization.route.js @@ -1,5 +1,6 @@ const express = require('express'), router = express.Router(), + checkAuth = require('../middleware/check-auth'), roles = require('../helpers/roles'); const controllers = require('../controllers/organization'); @@ -7,4 +8,14 @@ const controllers = require('../controllers/organization'); // POST Signup route 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;