Skip to content

Commit

Permalink
Merge pull request #2655 from HHS/al-ttahub-3860-investigate-error-on…
Browse files Browse the repository at this point in the history
…-update-grants

[TTAHUB-3860] Prevent unnecessary throwing of error from grant update - CDI replacements
  • Loading branch information
AdamAdHocTeam authored Feb 18, 2025
2 parents 52bb697 + f7d0d75 commit 6e3c4ec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/lib/updateGrantsRecipients.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ export const updateCDIGrantsWithOldGrantData = async (grantsToUpdate) => {
const updates = grantsToUpdate.map(async (grant) => {
// eslint-disable-next-line max-len
const replacedGrants = await GrantReplacements.findAll({ where: { replacingGrantId: grant.id } });

// If we don't have any replaced grants replacements we have nothing to do for this grant.
// Prevent confusion of throwing exception below.
if (!replacedGrants.length) {
logger.info(`updateCDIGrantsWithOldGrantData: No grant replacements found for CDI grant: ${grant.id}, skipping`);
return Promise.resolve();
}

// eslint-disable-next-line max-len
const validOldGrants = (await Promise.all(replacedGrants.map((rg) => Grant.findByPk(rg.replacedGrantId)))).filter(Boolean);

Expand Down
19 changes: 19 additions & 0 deletions src/lib/updateGrantsRecipients.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import db, {
ActivityRecipient,
ProgramPersonnel,
} from '../models';
import { logger } from '../logger';

jest.mock('axios');
const mockZip = jest.fn();
Expand Down Expand Up @@ -130,6 +131,7 @@ describe('Update grants, program personnel, and recipients', () => {
individualHooks: true,
});
await Recipient.unscoped().destroy({ where: { id: { [Op.gt]: SMALLEST_GRANT_ID } } });
jest.clearAllMocks();
});

afterAll(async () => {
Expand Down Expand Up @@ -1158,6 +1160,23 @@ describe('Update grants, program personnel, and recipients', () => {
expect(updatedGrant2.recipientId).toEqual(11);
expect(updatedGrant2.regionId).toEqual(2);
});

it('shouldn\'t throw an error if there are no grant replacements found', async () => {
// spy on logger.error.
jest.spyOn(logger, 'error').mockImplementation(() => {});
jest.spyOn(logger, 'info').mockImplementation(() => {});
const newGrant = {
id: 8546, cdi: true, number: 'X5', recipientId: 628, regionId: 13,
};

await updateCDIGrantsWithOldGrantData([newGrant]);

// Ensure logger.error wasn't called.
expect(logger.error).not.toHaveBeenCalled();

// Expect logger.info to display the message that no replacements were found.
expect(logger.info).toHaveBeenCalledWith('updateCDIGrantsWithOldGrantData: No grant replacements found for CDI grant: 8546, skipping');
});
});
});

Expand Down

0 comments on commit 6e3c4ec

Please sign in to comment.