attempting fixes for association routes

This commit is contained in:
Arjun Patel
2019-01-18 15:54:09 -08:00
parent 3e1cec54e0
commit 243e28eb67
+27 -10
View File
@@ -1,7 +1,7 @@
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 Grants = db.Grants; const { Grants, Causes, Regions, Organizations } = db;
// Create a grant for certain donor // Create a grant for certain donor
exports.create = (req, res, next) => { exports.create = (req, res, next) => {
@@ -16,6 +16,7 @@ exports.create = (req, res, next) => {
} = req.body; } = req.body;
Grants.create({ Grants.create({
donor_id,
name, name,
amount, amount,
monthly, monthly,
@@ -37,12 +38,28 @@ exports.create = (req, res, next) => {
); );
Promise.all([ Promise.all([
grant.addCauses(grants_causes_rows), grants_causes_rows,
grant.addRegions(grants_regions_rows), grants_regions_rows,
grant.addOrganizations(grants_organizations_rows) grants_organizations_rows
]).then(results => { ])
res.status(201).json(results); .then(results => {
}); Causes.bulkCreate(results[0], { raw: true }).then(causes => {
grant.addCauses(causes);
// console.log(causes);
});
console.log(results[1]);
Regions.bulkCreate(results[1], { raw: true }).then(regions => {
grant.addCauses(regions);
// console.log(regions);
});
Organizations.bulkCreate(results[2], { raw: true }).then(
organizations => {
grant.addCauses(organizations);
// console.log(organizations);
}
);
})
.catch(error => next(error));
}) })
.catch(error => next(error)); .catch(error => next(error));
}; };
@@ -56,10 +73,10 @@ exports.findByDonorId = (req, res, next) => {
}).then(grants => { }).then(grants => {
Promise.all( Promise.all(
grants.map(grant => { grants.map(grant => {
const causes = grant.getCauses({ raw: true }).then(causes => causes), const causes = grant.getCauses().then(causes => causes),
regions = grant.getRegions({ raw: true }).then(regions => regions), regions = grant.getRegions().then(regions => regions),
organizations = grant organizations = grant
.getOrganizations({ raw: true }) .getOrganizations()
.then(organizations => organizations); .then(organizations => organizations);
return Promise.all([causes, regions, organizations]).then(data => { return Promise.all([causes, regions, organizations]).then(data => {
return { return {