working on organization create endpoint

This commit is contained in:
Arjun Patel
2019-01-27 19:40:18 -08:00
parent 1aa0d2268a
commit 60590906a0
5 changed files with 77 additions and 9 deletions
+3
View File
@@ -0,0 +1,3 @@
module.exports = {
organization: require('./organization.controller')
};
@@ -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));
};
-8
View File
@@ -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: {
+10
View File
@@ -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;
+3 -1
View File
@@ -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