From c7c5d2e3ff7c8d9ef256c4904707277d0b55d8a0 Mon Sep 17 00:00:00 2001 From: Courtney Myers Date: Thu, 23 Jun 2022 15:25:07 -0400 Subject: [PATCH 01/26] Inject currently logged in user's email, title, and name in hidden form fields for new and existing rebate forms --- app/client/src/contexts/user.tsx | 12 +- app/client/src/routes/existingRebate.tsx | 21 +- app/client/src/routes/newRebate.tsx | 309 +++++++++++------------ app/client/src/utilities.tsx | 36 +++ 4 files changed, 201 insertions(+), 177 deletions(-) create mode 100644 app/client/src/utilities.tsx diff --git a/app/client/src/contexts/user.tsx b/app/client/src/contexts/user.tsx index 5b6dc9b3..9b10c1ef 100644 --- a/app/client/src/contexts/user.tsx +++ b/app/client/src/contexts/user.tsx @@ -10,13 +10,13 @@ type Props = { children: ReactNode; }; -export type EPAUserData = { +type EpaUserData = { mail: string; memberof: string; exp: number; }; -export type SAMUserData = { +export type SamEntityData = { ENTITY_COMBO_KEY__c: string; UNIQUE_ENTITY_ID__c: string; ENTITY_EFT_INDICATOR__c: string; @@ -55,7 +55,7 @@ type State = { epaUserData: | { status: "idle"; data: {} } | { status: "pending"; data: {} } - | { status: "success"; data: EPAUserData } + | { status: "success"; data: EpaUserData } | { status: "failure"; data: {} }; samUserData: | { status: "idle"; data: {} } @@ -63,7 +63,7 @@ type State = { | { status: "success"; data: - | { results: true; records: SAMUserData[] } + | { results: true; records: SamEntityData[] } | { results: false; records: [] }; } | { status: "failure"; data: {} }; @@ -76,7 +76,7 @@ type Action = | { type: "FETCH_EPA_USER_DATA_SUCCESS"; payload: { - epaUserData: EPAUserData; + epaUserData: EpaUserData; }; } | { type: "FETCH_EPA_USER_DATA_FAILURE" } @@ -85,7 +85,7 @@ type Action = type: "FETCH_SAM_USER_DATA_SUCCESS"; payload: { samUserData: - | { results: true; records: SAMUserData[] } + | { results: true; records: SamEntityData[] } | { results: false; records: [] }; }; } diff --git a/app/client/src/routes/existingRebate.tsx b/app/client/src/routes/existingRebate.tsx index 259c43d3..979ed6a5 100644 --- a/app/client/src/routes/existingRebate.tsx +++ b/app/client/src/routes/existingRebate.tsx @@ -13,6 +13,7 @@ import { Formio, Form } from "@formio/react"; import { cloneDeep, isEqual } from "lodash"; // --- import { serverUrl, fetchData } from "../config"; +import { getUserInfo } from "../utilities"; import Loading from "components/loading"; import Message from "components/message"; import MarkdownContent from "components/markdownContent"; @@ -170,6 +171,7 @@ export default function ExistingRebate() { type FormioSubmissionData = { // NOTE: more fields are in a form.io submission, // but we're only concerned with the fields below + bap_hidden_entity_combo_key?: string; ncesDataSource?: string; // (other fields...) }; @@ -231,7 +233,7 @@ function ExistingRebateContent() { const navigate = useNavigate(); const { id } = useParams<"id">(); const { content } = useContentState(); - const { epaUserData } = useUserState(); + const { epaUserData, samUserData } = useUserState(); const dispatch = useExistingRebateDispatch(); const [rebateFormSubmission, setRebateFormSubmission] = @@ -331,10 +333,20 @@ function ExistingRebateContent() { ); } - if (epaUserData.status !== "success") { + if (epaUserData.status !== "success" || samUserData.status !== "success") { return ; } + const entityComboKey = storedSubmissionData.bap_hidden_entity_combo_key; + const samData = samUserData.data.records.find((record) => { + return record.ENTITY_COMBO_KEY__c === entityComboKey; + }); + + if (!samData) return null; + + const email = epaUserData.data.mail; + const { title, name } = getUserInfo(email, samData); + return (
{content.status === "success" && ( @@ -361,7 +373,10 @@ function ExistingRebateContent() { submission={{ data: { ...storedSubmissionData, - last_updated_by: epaUserData.data.mail, + last_updated_by: email, + hidden_current_user_email: email, + hidden_current_user_title: title, + hidden_current_user_name: name, ...pendingSubmissionData, }, }} diff --git a/app/client/src/routes/newRebate.tsx b/app/client/src/routes/newRebate.tsx index 0e65b0dc..90f15db1 100644 --- a/app/client/src/routes/newRebate.tsx +++ b/app/client/src/routes/newRebate.tsx @@ -4,52 +4,25 @@ import { DialogOverlay, DialogContent } from "@reach/dialog"; import icons from "uswds/img/sprite.svg"; // --- import { serverUrl, fetchData } from "../config"; +import { getUserInfo } from "../utilities"; import Loading from "components/loading"; import Message from "components/message"; import MarkdownContent from "components/markdownContent"; import { TextWithTooltip } from "components/infoTooltip"; import { useContentState } from "contexts/content"; -import { EPAUserData, SAMUserData, useUserState } from "contexts/user"; +import { SamEntityData, useUserState } from "contexts/user"; -function getMatchedContactInfo(samData: SAMUserData, epaData: EPAUserData) { - const samEmailFields = [ - "ELEC_BUS_POC_EMAIL__c", - "ALT_ELEC_BUS_POC_EMAIL__c", - "GOVT_BUS_POC_EMAIL__c", - "ALT_GOVT_BUS_POC_EMAIL__c", - ]; - - let matchedEmailField; - - for (const [field, value] of Object.entries(samData)) { - if (!samEmailFields.includes(field)) continue; - // NOTE: take the first match only – there shouldn't be a case where the - // currently logged in user would be listed as multiple POCs for a single record - if ( - typeof value === "string" && - value.toLowerCase() === epaData.mail.toLowerCase() - ) { - matchedEmailField = field; - break; - } - } - - const fieldPrefix = matchedEmailField?.split("_EMAIL__c").shift(); - - return { - title: samData[`${fieldPrefix}_TITLE__c` as keyof SAMUserData] as string, - name: samData[`${fieldPrefix}_NAME__c` as keyof SAMUserData] as string, - }; -} - -function createNewRebate(samData: SAMUserData, epaData: EPAUserData) { - const { title, name } = getMatchedContactInfo(samData, epaData); +function createNewRebate(email: string, samData: SamEntityData) { + const { title, name } = getUserInfo(email, samData); return fetchData(`${serverUrl}/api/rebate-form-submission/`, { data: { - last_updated_by: epaData.mail, + last_updated_by: email, + hidden_current_user_email: email, + hidden_current_user_title: title, + hidden_current_user_name: name, bap_hidden_entity_combo_key: samData.ENTITY_COMBO_KEY__c, - sam_hidden_applicant_email: epaData.mail, + sam_hidden_applicant_email: email, sam_hidden_applicant_title: title, sam_hidden_applicant_name: name, sam_hidden_applicant_efti: samData.ENTITY_EFT_INDICATOR__c, @@ -80,15 +53,16 @@ export default function NewRebate() { text: "", }); - const activeSamData = - samUserData.status === "success" && - samUserData.data?.results && - samUserData.data?.records.filter((e) => e.ENTITY_STATUS__c === "Active"); - - if (epaUserData.status !== "success") { - return null; + if (epaUserData.status !== "success" || samUserData.status !== "success") { + return ; } + const email = epaUserData.data.mail; + + const activeSamRecords = samUserData.data.records.filter((record) => { + return record.ENTITY_STATUS__c === "Active"; + }); + return (
navigate("/")}> @@ -99,135 +73,134 @@ export default function NewRebate() { >
- {content.status === "success" && ( - ( -

- {props.children} -

- ), - p: (props) => ( -

- {props.children} -

- ), - }} + {activeSamRecords.length <= 0 ? ( + - )} - - {message.displayed && ( - - )} - -
- - - - - - - - - - - {!activeSamData ? ( - - - - ) : ( - activeSamData.map((samData, index) => ( - - + + + + + ))} + +
- Create - - - - - - -
-
- -
-
- + + {samData.UNIQUE_ENTITY_ID__c} + + {samData.ENTITY_EFT_INDICATOR__c} + + {samData.LEGAL_BUSINESS_NAME__c} +
+
+ + )}