From 7eefcc9a4eab1bbe1bba99f27ed564bc76d94f79 Mon Sep 17 00:00:00 2001 From: Ryan Ahearn Date: Fri, 12 Mar 2021 16:19:21 -0500 Subject: [PATCH] Import all grants/grantees found in HSES data --- src/lib/updateGrantsGrantees.js | 27 ++++++--------------------- src/lib/updateGrantsGrantees.test.js | 19 +++++++------------ 2 files changed, 13 insertions(+), 33 deletions(-) diff --git a/src/lib/updateGrantsGrantees.js b/src/lib/updateGrantsGrantees.js index fa47a42229..cd3a9e464c 100644 --- a/src/lib/updateGrantsGrantees.js +++ b/src/lib/updateGrantsGrantees.js @@ -22,8 +22,6 @@ const fs = require('mz/fs'); * */ export async function processFiles() { - let grantGrantees; - let grants; const granteesForDb = []; const grantsForDb = []; @@ -32,7 +30,7 @@ export async function processFiles() { const json = toJson(grantAgencyData); const grantAgency = JSON.parse(json); // we are only interested in non-delegates - grantGrantees = grantAgency.grant_agencies.grant_agency.filter( + const grantGrantees = grantAgency.grant_agencies.grant_agency.filter( (g) => g.grant_agency_number === '0', ); @@ -43,20 +41,7 @@ export async function processFiles() { // filter out delegates by matching to the non-delegates // eslint-disable-next-line max-len const granteesNonDelegates = agency.agencies.agency.filter((a) => grantGrantees.some((gg) => gg.agency_id === a.agency_id)); - - const hubGranteeIds = await Grantee.findAll({ attributes: ['id'] }).map((hgi) => hgi.id); - - // process grants - const grantData = await fs.readFile('./temp/grant_award.xml'); - const grant = JSON.parse(toJson(grantData)); - - // Check if the grantee id already exists in the smarthub db OR if it belongs to - // at least one active grant. grant_award data structure includes agency_id - // eslint-disable-next-line max-len - const grantees = granteesNonDelegates.filter((gnd) => hubGranteeIds.some((id) => id.toString() === gnd.agency_id) - || grant.grant_awards.grant_award.some((ga) => ga.agency_id === gnd.agency_id && ga.grant_status === 'Active')); - - grantees.forEach((g) => granteesForDb.push({ + granteesNonDelegates.forEach((g) => granteesForDb.push({ id: parseInt(g.agency_id, 10), name: g.agency_name, })); @@ -67,11 +52,11 @@ export async function processFiles() { updateOnDuplicate: ['name', 'updatedAt'], }); - const hubGrantIds = await Grant.findAll({ attributes: ['id'] }).map((hgi) => hgi.id); - - grants = grant.grant_awards.grant_award.filter((ga) => hubGrantIds.some((id) => id.toString() === ga.grant_award_id) || ga.grant_status === 'Active'); + // process grants + const grantData = await fs.readFile('./temp/grant_award.xml'); + const grant = JSON.parse(toJson(grantData)); - grants.forEach((g) => grantsForDb.push({ + grant.grant_awards.grant_award.forEach((g) => grantsForDb.push({ id: parseInt(g.grant_award_id, 10), number: g.grant_number, regionId: parseInt(g.region_id, 10), diff --git a/src/lib/updateGrantsGrantees.test.js b/src/lib/updateGrantsGrantees.test.js index c664801d54..c11a61b02f 100644 --- a/src/lib/updateGrantsGrantees.test.js +++ b/src/lib/updateGrantsGrantees.test.js @@ -89,18 +89,17 @@ describe('Update grants and grantees', () => { const grants = await Grant.findAll({ where: { granteeId: 1335 } }); expect(grants).toBeDefined(); - expect(grants.length).toBe(3); + expect(grants.length).toBe(7); const containsNumber = grants.some((g) => g.number === '02CH01111'); expect(containsNumber).toBeTruthy(); + const totalGrants = await Grant.findAll({ where: { id: { [Op.gt]: 20 } } }); + expect(totalGrants.length).toBe(10); }); - it('should exclude grantees with only inactive grants', async () => { + it('should not exclude grantees with only inactive grants', async () => { await processFiles(); - let grantee = await Grantee.findOne({ where: { id: 119 } }); - expect(grantee).toBeNull(); - // Same grantee, but with a different id and having an active grant - grantee = await Grantee.findOne({ where: { id: 7709 } }); - expect(grantee.name).toBe('Multi ID Agency'); + const grantee = await Grantee.findOne({ where: { id: 119 } }); + expect(grantee).not.toBeNull(); }); it('should update an existing grantee if it exists in smarthub', async () => { @@ -114,14 +113,10 @@ describe('Update grants and grantees', () => { }); it('should update an existing grant if it exists in smarthub', async () => { - await processFiles(); - let grant = await Grant.findOne({ where: { id: 5151 } }); - expect(grant).toBeNull(); - await Grantee.findOrCreate({ where: { id: 119, name: 'Multi ID Agency' } }); const [dbGrant] = await Grant.findOrCreate({ where: { id: 5151, number: '90CI4444', granteeId: 119 } }); await processFiles(); - grant = await Grant.findOne({ where: { id: 5151 } }); + const grant = await Grant.findOne({ where: { id: 5151 } }); expect(grant).not.toBeNull(); expect(grant.updatedAt).not.toEqual(dbGrant.updatedAt); expect(grant.number).toBe('90CI4444');