From 89bc7120c965bd2f257c1c04f90fe932c6c1b574 Mon Sep 17 00:00:00 2001 From: Dean Hasley Date: Thu, 15 Feb 2024 08:44:57 +0000 Subject: [PATCH] fix(aapd-951): get user by email from session, ensure correct user --- .../src/view-model-maps/appeal-headline.js | 5 ++-- .../selected-appeal/selected-appeal.js | 30 +++++++++++-------- .../forms-web-app/src/lib/determine-user.js | 3 +- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/packages/common/src/view-model-maps/appeal-headline.js b/packages/common/src/view-model-maps/appeal-headline.js index a2c6652cf4..81a13464d9 100644 --- a/packages/common/src/view-model-maps/appeal-headline.js +++ b/packages/common/src/view-model-maps/appeal-headline.js @@ -1,10 +1,11 @@ +const { APPEAL_USER_ROLES } = require('@pins/common/src/constants'); const { formatAddress } = require('../lib/format-address'); /** * @param {import("../client/appeals-api-client").AppealCaseWithAppellant} caseData - * @param {import('@pins/common/src/constants').AppealToUserRoles} userType + * @param {import('@pins/common/src/constants').AppealToUserRoles|string|null} userType */ -exports.formatHeadlineData = (caseData, userType = 'interestedParty') => { +exports.formatHeadlineData = (caseData, userType = APPEAL_USER_ROLES.INTERESTED_PARTY) => { const { caseReference, LPAName, diff --git a/packages/forms-web-app/src/controllers/selected-appeal/selected-appeal.js b/packages/forms-web-app/src/controllers/selected-appeal/selected-appeal.js index 7206d6c00b..36d48291a0 100644 --- a/packages/forms-web-app/src/controllers/selected-appeal/selected-appeal.js +++ b/packages/forms-web-app/src/controllers/selected-appeal/selected-appeal.js @@ -16,20 +16,26 @@ exports.get = async (req, res) => { //i.e '/appeals/' = appellant | agent const userType = determineUser(userRouteUrl); - let caseData; - - if (userType) { - // TODO: determine user ID (can get from API via email, in session) - caseData = await apiClient.getUsersAppealCase({ - caseReference: appealNumber, - role: userType, - userId: '' - }); - } else { - // TODO: handle LPA - caseData = await apiClient.getAppealCaseDataByCaseReference(appealNumber); + if (userType === null) { + throw new Error('Unknown role'); } + let userEmail; + + if (req.session.lpaUser) { + userEmail = req.session.lpaUser.email; + } else if (req.session.email) { + userEmail = req.session.email; + } + + const user = await apiClient.getUserByEmailV2(userEmail); + + const caseData = await apiClient.getUsersAppealCase({ + caseReference: appealNumber, + role: userType, + userId: user.id + }); + const headlineData = formatHeadlineData(caseData, userType); const viewContext = { diff --git a/packages/forms-web-app/src/lib/determine-user.js b/packages/forms-web-app/src/lib/determine-user.js index d3716f1086..f4ed149f8c 100644 --- a/packages/forms-web-app/src/lib/determine-user.js +++ b/packages/forms-web-app/src/lib/determine-user.js @@ -11,8 +11,9 @@ const determineUser = (url) => { return APPEAL_USER_ROLES.APPELLANT; } else if (url.includes('/manage-appeals/')) { return LPA_USER_ROLE; + } else { + return null; } - return null; }; module.exports = { determineUser };