public vs general, roles

This commit is contained in:
Arjun Patel
2019-02-27 20:05:33 -08:00
parent 833c87d1ec
commit d14e111f53
13 changed files with 772 additions and 23 deletions
@@ -0,0 +1,22 @@
const db = require('../../config/db.config.js'),
errorMaker = require('../../helpers/error.maker'),
textCleaner = require('../../helpers/text_cleaner');
const { Grant, Cause, Region, Organization } = db;
// FETCH all causes
exports.findAll = (req, res, next) => {
Cause.findAll()
.then(causes => {
causes = causes.map(cause => {
cause.name = textCleaner.titleCase(cause.name);
return cause;
});
res.status(200).json({
causes,
number_items: causes.length
});
})
.catch(error => next(error));
};
+6
View File
@@ -0,0 +1,6 @@
module.exports = {
cause: require('./cause.controller'),
region: require('./region.controller'),
organization: require('./organization.controller'),
user: require('./user.controller')
};
@@ -0,0 +1,41 @@
const db = require('../../config/db.config.js'),
errorMaker = require('../../helpers/error.maker');
const { Grant, Cause, Region, Organization } = db;
// FETCH all organizations
exports.findAll = (req, res, next) => {
Organization.findAll()
.then(organizations => {
res.status(200).json({
organizations,
number_items: organizations.length
});
})
.catch(error => next(error));
};
// FETCH depending on the given search
exports.searchOrgs = (req, res, next) => {
let { search } = req.params;
search = '%' + search + '%';
const limit = 10;
const QUERY = `SELECT * from organizations
WHERE name LIKE :search or
primary_cause LIKE :search or
primary_region LIKE :search
LIMIT :limit`;
db.sequelize
.query(QUERY, {
replacements: { search, limit },
type: db.Sequelize.QueryTypes.SELECT
})
.then(organizations => {
res.status(200).json({
organizations,
number_items: organizations.length
});
})
.catch(error => next(error));
};
@@ -0,0 +1,22 @@
const db = require('../../config/db.config.js'),
errorMaker = require('../../helpers/error.maker'),
textCleaner = require('../../helpers/text_cleaner');
const { Grant, Cause, Region, Organization } = db;
// FETCH all regions
exports.findAll = (req, res, next) => {
Region.findAll()
.then(regions => {
regions = regions.map(region => {
region.name = textCleaner.titleCase(region.name);
return region;
});
res.status(200).json({
regions,
number_items: regions.length
});
})
.catch(error => next(error));
};
+31
View File
@@ -0,0 +1,31 @@
const db = require('../../config/db.config.js'),
errorMaker = require('../../helpers/error.maker');
const { Grant, Cause, Region, Organization, User } = db;
// POST an email into user general model from given email
exports.addEmail = (req, res, next) => {
User.findAll({
where: {
email: req.body.email
}
})
.then(users => {
if (users.length >= 1) {
return next(errorMaker(409, `Email Exists: ${req.body.email}`));
} else {
User.create({
email: req.body.email
})
.then(donor => {
// Send created user to client
return res.status(201).json({
message: 'User created',
donor
});
})
.catch(error => next(error));
}
})
.catch(error => next(error));
};