working cleaner query for getting grants...split to multiple queries
This commit is contained in:
@@ -109,44 +109,38 @@ exports.getGrantsByDonorId = async (req, res, next) => {
|
|||||||
try {
|
try {
|
||||||
const grants = await db.sequelize.query(
|
const grants = await db.sequelize.query(
|
||||||
`
|
`
|
||||||
Select
|
Select *
|
||||||
g.id,
|
from grants
|
||||||
g.name,
|
where donor_id = :donor_id
|
||||||
g.amount,
|
|
||||||
g.monthly,
|
|
||||||
g.num_causes,
|
|
||||||
g.num_regions,
|
|
||||||
g.donor_id,
|
|
||||||
g.created_at,
|
|
||||||
g.updated_at,
|
|
||||||
JSON_ARRAYAGG(o.id) AS organizations_ids,
|
|
||||||
JSON_ARRAYAGG(o.name) AS organization_names,
|
|
||||||
JSON_ARRAYAGG(go.amount) AS organization_amounts,
|
|
||||||
JSON_ARRAYAGG(o.primary_cause) AS causes,
|
|
||||||
JSON_ARRAYAGG(o.primary_region) AS regions
|
|
||||||
from grants as g
|
|
||||||
inner join grants_organizations as go on go.grant_id = g.id
|
|
||||||
left join organizations as o on o.id = go.organization_id
|
|
||||||
where donor_id = :donor_id
|
|
||||||
group by g.id, g.name, g.amount, g.monthly, g.num_causes, g.num_regions, g.donor_id;
|
|
||||||
`,
|
`,
|
||||||
{ type: db.sequelize.QueryTypes.SELECT, replacements: { donor_id } }
|
{ type: db.sequelize.QueryTypes.SELECT, replacements: { donor_id } }
|
||||||
);
|
);
|
||||||
|
|
||||||
// adding in the organizations, causes, and regions for each grant into arrays
|
// adding in the organizations, causes, and regions for each grant into arrays
|
||||||
grants.map(grant => {
|
await Promise.all(
|
||||||
grant.organizations = [];
|
grants.map(async grant => {
|
||||||
|
let grant_organizations = await db.sequelize.query(
|
||||||
|
`
|
||||||
|
Select
|
||||||
|
go.amount,
|
||||||
|
o.id,
|
||||||
|
o.name,
|
||||||
|
o.primary_cause,
|
||||||
|
o.primary_region
|
||||||
|
from grants_organizations as go
|
||||||
|
left join organizations as o on o.id = go.organization_id
|
||||||
|
where grant_id = :grant_id
|
||||||
|
`,
|
||||||
|
{
|
||||||
|
type: db.sequelize.QueryTypes.SELECT,
|
||||||
|
replacements: { grant_id: grant.id }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
for (var i = 0; i < grant.organization_names.length; i++) {
|
grant.organizations = grant_organizations;
|
||||||
grant.organizations.push({
|
grant.monthly = grant.monthly == 1 ? true : false;
|
||||||
id: grant.organizations_ids[i],
|
})
|
||||||
name: grant.organization_names[i],
|
);
|
||||||
amount: grant.organization_amounts[i]
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
grant.monthly = grant.monthly == 1 ? true : false;
|
|
||||||
});
|
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
grants
|
grants
|
||||||
|
|||||||
Reference in New Issue
Block a user