diff --git a/app/controllers/organization/index.js b/app/controllers/organization/index.js new file mode 100644 index 0000000..e650b45 --- /dev/null +++ b/app/controllers/organization/index.js @@ -0,0 +1,3 @@ +module.exports = { + organization: require('./organization.controller') +}; diff --git a/app/controllers/organization/organization.controller.js b/app/controllers/organization/organization.controller.js new file mode 100644 index 0000000..2f2102b --- /dev/null +++ b/app/controllers/organization/organization.controller.js @@ -0,0 +1,61 @@ +const db = require('../../config/db.config.js'), + errorMaker = require('../../helpers/error.maker'); + +const { Grant, Cause, Region, Organization } = db; + +// POST create an organization +// Temporarily only use: name, email, password, short_description, primary_cause, primary_region +exports.create = (req, res, next) => { + const { + name, + email, + password, + short_description, + primary_cause, + primary_region + } = req.body; + + // see if organization already in db + Organization.findAll({ + where: { + email + } + }) + .then(orgs => { + 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)); + } + }); + } + }) + .catch(error => next(error)); +}; diff --git a/app/models/organization.model.js b/app/models/organization.model.js index 3cfbde9..104829e 100644 --- a/app/models/organization.model.js +++ b/app/models/organization.model.js @@ -6,14 +6,6 @@ module.exports = (sequelize, DataTypes) => { autoIncrement: true }, name: { type: DataTypes.STRING, allowNull: false }, - first_name: { - type: DataTypes.STRING, - allowNull: false - }, - last_name: { - type: DataTypes.STRING, - allowNull: false - }, email: { type: DataTypes.STRING, validate: { diff --git a/app/routes/organization.route.js b/app/routes/organization.route.js new file mode 100644 index 0000000..70c7fb3 --- /dev/null +++ b/app/routes/organization.route.js @@ -0,0 +1,10 @@ +const express = require('express'), + router = express.Router(), + roles = require('../helpers/roles'); + +const controllers = require('../controllers/organization'); + +// POST Signup route +router.post('/', controllers.organization.create); + +module.exports = router; diff --git a/server.js b/server.js index dec5469..7bfb5f2 100644 --- a/server.js +++ b/server.js @@ -47,8 +47,10 @@ app.get('/', function(req, res, next) { res.send('Welcome to the Ucharify API'); }); -//adding routes to Express app +//donor routes app.use('/api/donors', require('./app/routes/donor.route.js')); +//organization routes +app.use('/api/org', require('./app/routes/organization.route')); //auth route app.use('/api/auth', require('./app/routes/auth.route.js')); //general routes