Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TTAHUB-3860] Prevent unnecessary throwing of error from grant update - CDI replacements #2655

Merged
merged 9 commits into from
Feb 18, 2025
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