Skip to content

Commit

Permalink
Merge pull request #2265 from HHS/jp/3095/dont-remove-expired-grants-…
Browse files Browse the repository at this point in the history
…on-ar

[TTAHUB-3095] Don't remove expired grants from the recipients dropdown when an AR is unlocked
  • Loading branch information
nvms authored Jul 15, 2024
2 parents 45f3b6e + 7cd85f7 commit 98ac36a
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 19 deletions.
6 changes: 6 additions & 0 deletions frontend/src/fetchers/activityReports.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ export const getRecipients = async (region) => {
return recipients.json();
};

export const getRecipientsForExistingAR = async (reportId) => {
const url = join(activityReportUrl, `${reportId}`, 'activity-recipients');
const recipients = await get(url);
return recipients.json();
};

export const getGoals = async (grantIds) => {
const params = grantIds.map((grantId) => `grantIds=${grantId}`);
const url = join(activityReportUrl, 'goals', `?${params.join('&')}`);
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/pages/ActivityReport/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('ActivityReport', () => {

beforeEach(() => {
fetchMock.get('/api/activity-reports/activity-recipients?region=1', recipients);
fetchMock.get('/api/activity-reports/1/activity-recipients', recipients);
fetchMock.get('/api/activity-reports/groups?region=1', [{
id: 110,
name: 'Group 1',
Expand Down Expand Up @@ -155,6 +156,7 @@ describe('ActivityReport', () => {
};

fetchMock.get('/api/activity-reports/activity-recipients?region=1', groupRecipients, { overwriteRoutes: true });
fetchMock.get('/api/activity-reports/1/activity-recipients', groupRecipients, { overwriteRoutes: true });

const data = formData();
fetchMock.get('/api/activity-reports/1', { ...data, activityRecipients: [] });
Expand Down Expand Up @@ -226,6 +228,7 @@ describe('ActivityReport', () => {
};

fetchMock.get('/api/activity-reports/activity-recipients?region=1', groupRecipients, { overwriteRoutes: true });
fetchMock.get('/api/activity-reports/1/activity-recipients', groupRecipients, { overwriteRoutes: true });

const data = formData();
fetchMock.get('/api/activity-reports/1', { ...data, activityRecipients: [] });
Expand Down Expand Up @@ -363,6 +366,7 @@ describe('ActivityReport', () => {

describe('resetToDraft', () => {
it('navigates to the correct page', async () => {
fetchMock.get('/api/activity-reports/3/activity-recipients', recipients);
const data = formData();
// load the report
fetchMock.get('/api/activity-reports/3', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('Local storage fallbacks', () => {

beforeEach(() => {
fetchMock.get('/api/activity-reports/activity-recipients?region=1', recipients);
fetchMock.get('/api/activity-reports/1/activity-recipients', recipients);
fetchMock.get('/api/activity-reports/groups?region=1', []);
fetchMock.get('/api/users/collaborators?region=1', []);
fetchMock.get('/api/activity-reports/approvers?region=1', []);
Expand Down
13 changes: 11 additions & 2 deletions frontend/src/pages/ActivityReport/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ import {
submitReport,
saveReport,
getReport,
getRecipients,
getRecipientsForExistingAR,
createReport,
getCollaborators,
getApprovers,
reviewReport,
resetToDraft,
getGroupsForActivityReport,
getRecipients,
} from '../../fetchers/activityReports';
import useLocalStorage, { setConnectionActiveWithError } from '../../hooks/useLocalStorage';
import NetworkContext, { isOnlineMode } from '../../NetworkContext';
Expand Down Expand Up @@ -277,8 +278,16 @@ function ActivityReport({
};
}

const getRecips = async () => {
if (reportId.current && reportId.current !== 'new') {
return getRecipientsForExistingAR(reportId.current);
}

return getRecipients(report.regionId);
};

const apiCalls = [
getRecipients(report.regionId),
getRecips(),
getCollaborators(report.regionId),
getApprovers(report.regionId),
getGroupsForActivityReport(report.regionId),
Expand Down
18 changes: 18 additions & 0 deletions src/routes/activityReports/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,24 @@ export async function getActivityRecipients(req, res) {
res.json(activityRecipients);
}

export async function getActivityRecipientsForExistingReport(req, res) {
const { activityReportId } = req.params;

const [report] = await activityReportAndRecipientsById(activityReportId);
const userId = await currentUserId(req, res);
const user = await userById(userId);
const authorization = new ActivityReport(user, report);

if (!authorization.canGet()) {
res.sendStatus(403);
return;
}

const targetRegion = parseInt(report.regionId, DECIMAL_BASE);
const activityRecipients = await possibleRecipients(targetRegion, activityReportId);
res.json(activityRecipients);
}

/**
* Retrieve an activity report
*
Expand Down
3 changes: 3 additions & 0 deletions src/routes/activityReports/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getReports,
getReportAlerts,
getActivityRecipients,
getActivityRecipientsForExistingReport,
getGoals,
reviewReport,
resetToDraft,
Expand Down Expand Up @@ -40,6 +41,7 @@ router.post('/', transactionWrapper(createReport));
router.get('/approvers', transactionWrapper(getApprovers));
router.get('/groups', transactionWrapper(getGroups));
router.get('/activity-recipients', transactionWrapper(getActivityRecipients));
router.get('/activity-recipients/:reportId', transactionWrapper(getActivityRecipientsForExistingReport));
router.get('/goals', transactionWrapper(getGoals));
router.post('/goals', transactionWrapper(createGoalsForReport));
router.post('/objectives', transactionWrapper(saveOtherEntityObjectivesForReport));
Expand All @@ -60,5 +62,6 @@ router.put('/:activityReportId/review', checkActivityReportIdParam, transactionW
router.put('/:activityReportId/submit', checkActivityReportIdParam, transactionWrapper(submitReport));
router.put('/:activityReportId/unlock', checkActivityReportIdParam, transactionWrapper(unlockReport));
router.put('/:activityReportId/goals/edit', checkActivityReportIdParam, transactionWrapper(setGoalAsActivelyEdited));
router.get('/:activityReportId/activity-recipients', transactionWrapper(getActivityRecipientsForExistingReport));

export default router;
56 changes: 39 additions & 17 deletions src/services/activityReports.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ActivityReportCollaborator,
ActivityReportFile,
sequelize,
Sequelize,
ActivityRecipient,
File,
Grant,
Expand Down Expand Up @@ -1125,33 +1126,54 @@ export async function setStatus(report, status) {
* @param {number} [regionId] - A region id to query against
* @returns {*} Grants and Other entities
*/
export async function possibleRecipients(regionId) {
const where = { status: 'Active', regionId };

export async function possibleRecipients(regionId, activityReportId = null) {
const grants = await Recipient.findAll({
attributes: ['id', 'name'],
attributes: [
'id',
'name',
],
order: ['name'],
include: [{
where,
model: Grant,
as: 'grants',
attributes: [['id', 'activityRecipientId'], 'name', 'number'],
include: [{
model: Recipient,
as: 'recipient',
},
include: [
{
model: Program,
as: 'programs',
attributes: ['programType'],
model: Grant,
as: 'grants',
attributes: ['number', ['id', 'activityRecipientId'], 'name'],
required: true,
include: [
{
model: Program,
as: 'programs',
attributes: ['programType'],
required: false,
},
{
model: Recipient,
as: 'recipient',
required: true,
},
{
model: ActivityRecipient,
as: 'activityRecipients',
attributes: [],
required: false,
},
],
},
],
where: {
'$grants.regionId$': regionId,
[Op.or]: [
{ '$grants.status$': 'Active' },
{ '$grants->activityRecipients.activityReportId$': activityReportId },
],
}],
},
});

const otherEntities = await OtherEntity.findAll({
raw: true,
attributes: [['id', 'activityRecipientId'], 'name'],
});

return { grants, otherEntities };
}

Expand Down

0 comments on commit 98ac36a

Please sign in to comment.