working getting grants with causes, regions, and orgranizations details

This commit is contained in:
Arjun Patel
2019-01-16 10:37:04 -08:00
parent 4a7078c059
commit 18abee110a
+22 -13
View File
@@ -14,27 +14,36 @@ exports.create = (req, res, next) => {
}); });
}; };
// Find grants with cause and region and charity details by Id // Find grants with cause and region and charity details by donor_id
exports.findByDonorId = (req, res, next) => { exports.findByDonorId = (req, res, next) => {
Grants.findAll({ Grants.findAll({
where: { where: {
donor_id: req.params.donor_id donor_id: req.params.donor_id
} }
}) }).then(grants => {
.then(grants => { Promise.all(
const grants_full_info = grants.map(grant => { grants.map(grant => {
const causes = grant.getCauses({ raw: true }).then(causes => causes), const causes = grant.getCauses({ raw: true }).then(causes => causes),
regions = grant.getRegions({ raw: true }).then(regions => regions), regions = grant.getRegions({ raw: true }).then(regions => regions),
organizations = grant organizations = grant
.getOrganizations({ raw: true }) .getOrganizations({ raw: true })
.then(organizations => organizations); .then(organizations => organizations);
return { grant, causes, regions, organizations }; return Promise.all([causes, regions, organizations]).then(data => {
}); return {
grant,
res.status(200).json({ causes: data[0],
grants: grants_full_info, regions: data[1],
number_grants: grants_full_info.length organizations: data[2]
}); };
}) });
.catch(error => next(error)); })
)
.then(list_grants => {
res.status(200).json({
grants: list_grants,
number_grants: list_grants.length
});
})
.catch(error => next(error));
});
}; };