Skip to content

Commit

Permalink
fix: ensure there is guard clause to catch issues when projects are m…
Browse files Browse the repository at this point in the history
…issing EC records (#3863)

* fix: ensure there is guard clause to catch issues when projects are missing ec records

* fix: issue with checking existence

* fix: add more guard clauses as we do not have control over data quality

* fix: linting error
  • Loading branch information
as1729 authored Jan 7, 2025
1 parent 728d2e0 commit ae44c7c
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions packages/server/src/arpa_reporter/lib/audit-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,12 +466,12 @@ async function createReportsGroupedBySubAward(periodId, tenantId, dateFormat = R
}

function getMostRecentRecordForProject(records, logger = log) {
let mostRecentRecord = {};
let mostRecentRecord;
// Ensures we are only looking at records that are in the EC-tabs rather than the other tabs
records = records.filter((record) => Object.keys(EXPENDITURE_CATEGORIES).includes(record.type));

for (const record of records) {
if (Object.keys(mostRecentRecord).length === 0) {
if (!mostRecentRecord) {
logger.debug(`found first record: ${JSON.stringify(record)}`);
mostRecentRecord = record;
} else if (new Date(record.upload.created_at) > new Date(mostRecentRecord.upload.created_at)) {
Expand All @@ -480,10 +480,6 @@ function getMostRecentRecordForProject(records, logger = log) {
}
}

if (Object.keys(mostRecentRecord).length === 0) {
logger.warn('no records found for project');
}

logger.debug(`most recent record is: ${JSON.stringify(mostRecentRecord)}`);

return mostRecentRecord;
Expand All @@ -503,12 +499,17 @@ async function createKpiDataGroupedByProject(periodId, tenantId, logger = log) {
project: { id: projectId, totalRecords: projectRecords.length },
});
const mostRecentRecord = getMostRecentRecordForProject(projectRecords, projectLogger);
if (!mostRecentRecord) {
projectLogger.warn(`Unexpected error - project has no EC records ${projectId}`);
} else if (!mostRecentRecord.content) {
projectLogger.warn(`Unexpected error - project record has no content ${projectId}`);
}
projectLogger.debug('populating row from records in project');
const row = {
'Project ID': projectId,
'Number of Subawards': 0,
'Number of Expenditures': 0,
'Evidence Based Total Spend': mostRecentRecord?.content.Spending_Allocated_Toward_Evidence_Based_Interventions || 0,
'Evidence Based Total Spend': mostRecentRecord?.content?.Spending_Allocated_Toward_Evidence_Based_Interventions || 0,
};

projectRecords.forEach((r) => {
Expand Down

0 comments on commit ae44c7c

Please sign in to comment.