diff --git a/packages/server/__tests__/api/grants.test.js b/packages/server/__tests__/api/grants.test.js index d67d22d81..be2699a45 100644 --- a/packages/server/__tests__/api/grants.test.js +++ b/packages/server/__tests__/api/grants.test.js @@ -491,7 +491,12 @@ describe('`/api/grants` endpoint', () => { method: 'put', body: JSON.stringify({ interestedCode: 1 }), }); + expect(response.statusText).to.equal('OK'); + // Fetch and check the interested agencies list + const interestedResponse = await fetchApi(`/grants/${interestEndpoint}`, agencies.own, fetchOptions.admin); + const interestedAgencies = await interestedResponse.json(); + expect(interestedAgencies.some((agency) => agency.agency_id === agencies.own)).to.be.true; }); it('records this user\'s own agency\'s subagencies\' interest in a grant', async () => { const response = await fetchApi(`/grants/${interestEndpoint}/${agencies.ownSub}`, agencies.ownSub, { @@ -500,6 +505,11 @@ describe('`/api/grants` endpoint', () => { body: JSON.stringify({ interestedCode: 1 }), }); expect(response.statusText).to.equal('OK'); + + // Fetch and check the interested agencies list + const interestedResponse = await fetchApi(`/grants/${interestEndpoint}`, agencies.ownSub, fetchOptions.admin); + const interestedAgencies = await interestedResponse.json(); + expect(interestedAgencies.some((agency) => agency.agency_id === agencies.ownSub)).to.be.true; }); it('forbids requests for any agency outside this user\'s hierarchy', async () => { const response = await fetchApi(`/grants/${interestEndpoint}/${agencies.offLimits}`, agencies.offLimits, { @@ -519,6 +529,11 @@ describe('`/api/grants` endpoint', () => { body: JSON.stringify({ interestedCode: 1 }), }); expect(response.statusText).to.equal('OK'); + + // Fetch and check the interested agencies list + const interestedResponse = await fetchApi(`/grants/${interestEndpoint}`, agencies.own, fetchOptions.staff); + const interestedAgencies = await interestedResponse.json(); + expect(interestedAgencies.some((agency) => agency.agency_id === agencies.own)).to.be.true; }); it('forbids requests for any agency except this user\'s own agency', async () => { let response = await fetchApi(`/grants/${interestEndpoint}/${agencies.ownSub}`, agencies.ownSub, { diff --git a/packages/server/src/routes/grants.js b/packages/server/src/routes/grants.js index 3eb9ced20..7a04765b5 100755 --- a/packages/server/src/routes/grants.js +++ b/packages/server/src/routes/grants.js @@ -431,6 +431,20 @@ router.put('/:grantId/interested/:agencyId', requireUser, async (req, res) => { return; } + const transaction = await knex.transaction(); + // Query to check if the interestedCode corresponds to 'Interested' + const interestedStatus = await knex('interested_codes') + .select('status_code') + .where({ id: interestedCode }) + .first(); + + // Follow or Unfollow the grant based on the interestedCode + if (interestedStatus?.status_code === 'Interested') { + await followGrant(transaction, grantId, user.id); + } else { + await unfollowGrant(transaction, grantId, user.id); + } + try { // Calling the existing function to mark the grant as interested await db.markGrantAsInterested({ @@ -439,24 +453,16 @@ router.put('/:grantId/interested/:agencyId', requireUser, async (req, res) => { userId: user.id, interestedCode, }); - // Query to check if the interestedCode corresponds to 'Interested' - const interestedStatus = await knex('interested_codes') - .select('status_code') - .where({ id: interestedCode }) - .first(); - - // Follow or Unfollow the grant based on the interestedCode - if (interestedStatus?.status_code === 'Interested') { - await followGrant(knex, grantId, user.id); - } else { - await unfollowGrant(knex, grantId, user.id); - } + await transaction.commit(); + // Retruning updated interested agencies const interestedAgencies = await db.getInterestedAgencies({ grantIds: [grantId], tenantId: user.tenant_id }); res.json(interestedAgencies); } catch (error) { + // Roll back the follow/unfollow operation since marking grant as interested failed + await transaction.rollback(); console.error('Error in marking as interested:', error); - res.status(500).send({ success: false, error: 'Internal Server Error' }); + throw error; } });