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-3912] Support multiple grant replacement and ensure all grants being replaced have the same region and recipient #2659

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/lib/updateGrantsRecipients.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,22 @@ export const updateCDIGrantsWithOldGrantData = async (grantsToUpdate) => {
const [regionId] = uniq(validOldGrants.map((g) => g.regionId));
const [recipientId] = uniq(validOldGrants.map((g) => g.recipientId));

if (!regionId || !recipientId || validOldGrants.length !== replacedGrants.length) {
if (!regionId || !recipientId) {
throw new Error(`Expected one region and recipient for grant ${grant.id}, got ${validOldGrants.length} valid grants`);
}

// Ensure allValidOldGrants have the same recipient and region.
if (!validOldGrants.every(
(g) => g.regionId === regionId && g.recipientId === recipientId,
)) {
logger.error(
'updateGrantsRecipients: Error updating grants:',
`Expected all valid replaced grants to have the same recipient and region for CDI grant ${grant.id}, skipping`,
);
return Promise.resolve();
}

// eslint-disable-next-line consistent-return
return grant.update({ recipientId, regionId });
});

Expand Down
24 changes: 24 additions & 0 deletions src/lib/updateGrantsRecipients.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,30 @@ describe('Update grants, program personnel, and recipients', () => {
// 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');
});

it('logs an error if all oldGrants dont have the same recipient and region id', async () => {
const newGrant = {
id: 8546, cdi: true, number: 'X5', recipientId: 628, regionId: 13,
};

// spy on logger.error.
jest.spyOn(logger, 'error').mockImplementation(() => {});

// Mock return replacements.
jest.spyOn(GrantReplacements, 'findAll').mockResolvedValueOnce([{ replacedGrantId: 1 }, { replacedGrantId: 2 }]);

// Mock return old grants.
jest.spyOn(Grant, 'findByPk').mockResolvedValueOnce({ recipientId: 1, regionId: 1 });
jest.spyOn(Grant, 'findByPk').mockResolvedValueOnce({ recipientId: 2, regionId: 2 });

await updateCDIGrantsWithOldGrantData([newGrant]);

// Ensure logger.error was called.
expect(logger.error).toHaveBeenCalledWith(
'updateGrantsRecipients: Error updating grants:',
'Expected all valid replaced grants to have the same recipient and region for CDI grant 8546, skipping',
);
});
});
});

Expand Down