changes to grant creation and model clean
This commit is contained in:
@@ -1,35 +1,44 @@
|
|||||||
const db = require('../../config/db.config.js'),
|
const db = require('../../config/db.config.js'),
|
||||||
errorMaker = require('../../helpers/error.maker');
|
errorMaker = require('../../helpers/error.maker');
|
||||||
|
|
||||||
const { Grant, Cause, Region, Organization } = db;
|
const { Grant, Cause, Region, Organization, sequelize } = db;
|
||||||
|
|
||||||
// Create a grant for certain donor
|
// Create a grant for certain donor
|
||||||
exports.create = (req, res, next) => {
|
exports.create = (req, res, next) => {
|
||||||
const donor_id = req.params.donor_id;
|
const donor_id = req.user.id;
|
||||||
const { name, amount, monthly, causes, regions, organizations } = req.body;
|
const { name, amount, monthly, causes, regions, organizations } = req.body;
|
||||||
|
|
||||||
Grant.create({
|
return sequelize
|
||||||
donor_id,
|
.transaction(function(t) {
|
||||||
name,
|
return Grant.create(
|
||||||
amount,
|
{
|
||||||
monthly,
|
donor_id,
|
||||||
num_causes: causes.length,
|
name,
|
||||||
num_regions: regions.length
|
amount,
|
||||||
})
|
monthly,
|
||||||
.then(grant => {
|
num_causes: causes.length,
|
||||||
return Promise.all([
|
num_regions: regions.length
|
||||||
grant.addCauses(causes),
|
},
|
||||||
grant.addRegions(regions),
|
{ transaction: t }
|
||||||
grant.addOrganizations(organizations)
|
).then(grant => {
|
||||||
]).then(result => result);
|
return Promise.all([
|
||||||
|
grant.addCauses(causes, { transaction: t }),
|
||||||
|
grant.addRegions(regions, { transaction: t }),
|
||||||
|
grant.addOrganizations(organizations, { transaction: t })
|
||||||
|
]).then(result => grant);
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.then(result => {
|
.then(function(grant) {
|
||||||
|
// transaction committed
|
||||||
res.status(201).json({
|
res.status(201).json({
|
||||||
result,
|
grant,
|
||||||
message: 'Grant Created'
|
message: 'Grant Created'
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch(error => next(error));
|
.catch(function(error) {
|
||||||
|
// transaction rollback
|
||||||
|
next(error);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Find grants with causes, regions, and organizations by donor_id
|
// Find grants with causes, regions, and organizations by donor_id
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ exports.findSuggested = (req, res, next) => {
|
|||||||
|
|
||||||
const QUERY =
|
const QUERY =
|
||||||
'SELECT * from organizations ' +
|
'SELECT * from organizations ' +
|
||||||
'WHERE primary_cause IN (:causes) and primary_region IN (:regions) ' +
|
'WHERE primary_cause IN (:causes) or primary_region IN (:regions) ' +
|
||||||
'LIMIT :max_orgs';
|
'LIMIT :max_orgs';
|
||||||
db.sequelize
|
db.sequelize
|
||||||
.query(QUERY, {
|
.query(QUERY, {
|
||||||
|
|||||||
@@ -14,10 +14,6 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
type: DataTypes.INTEGER,
|
type: DataTypes.INTEGER,
|
||||||
allowNull: false
|
allowNull: false
|
||||||
},
|
},
|
||||||
last_payment: {
|
|
||||||
type: DataTypes.DATE,
|
|
||||||
default: new Date()
|
|
||||||
},
|
|
||||||
monthly: {
|
monthly: {
|
||||||
type: DataTypes.BOOLEAN,
|
type: DataTypes.BOOLEAN,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
|
|||||||
@@ -20,11 +20,7 @@ router.get('/grants/', checkAuth(roles.DONOR), controllers.grant.findByDonorId);
|
|||||||
* - monthly true or false
|
* - monthly true or false
|
||||||
* - donor_id
|
* - donor_id
|
||||||
* */
|
* */
|
||||||
router.post(
|
router.post('/grants/', checkAuth(roles.DONOR), controllers.grant.create);
|
||||||
'/grants/:donor_id',
|
|
||||||
checkAuth(roles.DONOR),
|
|
||||||
controllers.grant.create
|
|
||||||
);
|
|
||||||
|
|
||||||
// DELECT a grant of a donor
|
// DELECT a grant of a donor
|
||||||
router.delete(
|
router.delete(
|
||||||
|
|||||||
Reference in New Issue
Block a user