Skip to content

Commit 957c663

Browse files
Merge pull request #2268 from HHS/al-ttahub-3083-optimize-program-query
[TTAHUB-3083] Integrate programs retrieve into normal sequelize pattern
2 parents 184165d + 5e534b7 commit 957c663

File tree

1 file changed

+26
-112
lines changed

1 file changed

+26
-112
lines changed

src/services/activityReports.js

Lines changed: 26 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -300,80 +300,6 @@ export function activityReportByLegacyId(legacyId) {
300300
});
301301
}
302302

303-
export async function populateRecipientInfo(activityRecipients, grantPrograms) {
304-
/*
305-
Hopefully this code is temporary until we figure
306-
out why joining programs to grants causes issues.
307-
*/
308-
return activityRecipients.map((recipient) => {
309-
if (recipient.grant && grantPrograms.length) {
310-
const programsToAssign = grantPrograms.filter((p) => p.grantId === recipient.grantId);
311-
// Programs.
312-
Object.defineProperty(
313-
recipient.grant,
314-
'programs',
315-
{
316-
value: programsToAssign,
317-
enumerable: true,
318-
},
319-
);
320-
321-
// Program Types.
322-
const programTypes = programsToAssign && programsToAssign.length > 0
323-
? [...new Set(
324-
programsToAssign.filter((p) => (p.programType))
325-
.map((p) => (p.programType)).sort(),
326-
)] : [];
327-
Object.defineProperty(
328-
recipient.grant,
329-
'programTypes',
330-
{
331-
value: programTypes,
332-
enumerable: true,
333-
},
334-
);
335-
336-
// Number with Program Types.
337-
const numberWithProgramTypes = `${recipient.grant.number} ${programTypes.length > 0 ? ` - ${programTypes.join(', ')}` : ''}`;
338-
339-
Object.defineProperty(
340-
recipient.grant,
341-
'numberWithProgramTypes',
342-
{
343-
value: numberWithProgramTypes,
344-
enumerable: true,
345-
},
346-
);
347-
348-
// Name.
349-
let nameValue;
350-
if (recipient.grant.recipient) {
351-
nameValue = `${recipient.grant.recipient.name} - ${recipient.grant.numberWithProgramTypes}`;
352-
} else {
353-
nameValue = `${recipient.grant.numberWithProgramTypes}`;
354-
}
355-
Object.defineProperty(
356-
recipient.grant,
357-
'name',
358-
{
359-
value: nameValue,
360-
enumerable: true,
361-
},
362-
);
363-
364-
Object.defineProperty(
365-
recipient,
366-
'name',
367-
{
368-
value: nameValue,
369-
enumerable: true,
370-
},
371-
);
372-
}
373-
return { ...recipient };
374-
});
375-
}
376-
377303
export async function activityReportAndRecipientsById(activityReportId) {
378304
const arId = parseInt(activityReportId, DECIMAL_BASE);
379305

@@ -395,22 +321,23 @@ export async function activityReportAndRecipientsById(activityReportId) {
395321
{
396322
model: Grant,
397323
as: 'grant',
324+
include: [
325+
{
326+
model: Program,
327+
as: 'programs',
328+
attributes: ['programType'],
329+
},
330+
{
331+
model: Recipient,
332+
as: 'recipient',
333+
attributes: ['name'],
334+
},
335+
],
398336
},
399337
],
400338
});
401339

402-
// Get all grant programs at once to reduce DB calls.
403-
const grantIds = recipients.map((a) => a.grantId);
404-
const grantPrograms = await Program.findAll({
405-
where: {
406-
grantId: grantIds,
407-
},
408-
});
409-
410-
// Populate Activity Recipient info.
411-
const updatedRecipients = await populateRecipientInfo(recipients, grantPrograms);
412-
413-
const activityRecipients = updatedRecipients.map((recipient) => {
340+
const activityRecipients = recipients.map((recipient) => {
414341
const name = recipient.otherEntity ? recipient.otherEntity.name : recipient.grant.name;
415342
const activityRecipientId = recipient.otherEntity
416343
? recipient.otherEntity.dataValues.id : recipient.grant.dataValues.id;
@@ -724,17 +651,6 @@ export async function activityReports(
724651
name: arot.topic.name,
725652
}));
726653

727-
// Get all grant programs at once to reduce DB calls.
728-
const grantIds = recipients.map((a) => a.grantId);
729-
const grantPrograms = await Program.findAll({
730-
where: {
731-
grantId: grantIds,
732-
},
733-
});
734-
735-
// Populate Activity Recipient info.
736-
await populateRecipientInfo(recipients, grantPrograms);
737-
738654
return {
739655
...reports, recipients, topics,
740656
};
@@ -1251,6 +1167,18 @@ async function getDownloadableActivityReports(where, separate = true) {
12511167
{
12521168
model: Grant,
12531169
as: 'grant',
1170+
include: [
1171+
{
1172+
model: Program,
1173+
as: 'programs',
1174+
attributes: ['programType'],
1175+
},
1176+
{
1177+
model: Recipient,
1178+
as: 'recipient',
1179+
attributes: ['name'],
1180+
},
1181+
],
12541182
},
12551183
],
12561184
},
@@ -1329,21 +1257,7 @@ async function getDownloadableActivityReports(where, separate = true) {
13291257
};
13301258

13311259
// Get reports.
1332-
const reports = await batchQuery(query, 2000);
1333-
1334-
// Populate Activity Recipient info.
1335-
const updatedReportPromises = reports.map(async (r) => {
1336-
const grantIds = r.activityRecipients.map((a) => a.grantId);
1337-
// Get all grant programs at once to reduce DB calls.
1338-
const grantPrograms = await Program.findAll({
1339-
where: {
1340-
grantId: grantIds,
1341-
},
1342-
});
1343-
const updatedRecipients = await populateRecipientInfo(r.activityRecipients, grantPrograms);
1344-
return { ...r, activityRecipients: updatedRecipients };
1345-
});
1346-
return Promise.all(updatedReportPromises);
1260+
return batchQuery(query, 2000);
13471261
}
13481262

13491263
export async function getAllDownloadableActivityReports(

0 commit comments

Comments
 (0)