Skip to content

Commit

Permalink
Merge branch 'main' into js-360-filter-reports-to-approved
Browse files Browse the repository at this point in the history
  • Loading branch information
rahearn committed Mar 12, 2021
2 parents 2c6e28b + 1ed933c commit 78ddff4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 33 deletions.
27 changes: 6 additions & 21 deletions src/lib/updateGrantsGrantees.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ const fs = require('mz/fs');
*
*/
export async function processFiles() {
let grantGrantees;
let grants;
const granteesForDb = [];
const grantsForDb = [];

Expand All @@ -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',
);

Expand All @@ -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,
}));
Expand All @@ -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),
Expand Down
19 changes: 7 additions & 12 deletions src/lib/updateGrantsGrantees.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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');
Expand Down

0 comments on commit 78ddff4

Please sign in to comment.