From 1450bfe1fe89d54a94e7636f41c28969c0363a44 Mon Sep 17 00:00:00 2001 From: Courtney Myers Date: Thu, 28 Mar 2024 16:46:01 -0400 Subject: [PATCH] Update BAP query for 2023 PRF data to include the bus contact org info in the frf2023BusRecordsContactsQueries, and remove the no longer needed frf2023BusRecordsContactsOrgsQueries --- app/server/app/utilities/bap.js | 48 ++++++++--------- app/server/app/utilities/formio.js | 87 +++++++++++++++--------------- 2 files changed, 68 insertions(+), 67 deletions(-) diff --git a/app/server/app/utilities/bap.js b/app/server/app/utilities/bap.js index 70eef20a..1582dd69 100644 --- a/app/server/app/utilities/bap.js +++ b/app/server/app/utilities/bap.js @@ -189,19 +189,18 @@ const { submissionPeriodOpen } = require("../config/formio"); * Title: string * Email: string * Phone: string - * AccountId: string + * Account: { + * Id: string + * Name: string + * BillingStreet: string + * BillingCity: string + * BillingState: string + * BillingPostalCode: string + * County__c: string + * } * } | null * }[]} frf2023BusRecordsContactsQueries * @property {{ - * Id: string - * Name: string - * BillingStreet: string - * BillingCountry: string - * BillingCity: string - * BillingState: string - * BillingPostalCode: string - * }[]} frf2023BusRecordsContactsOrgsQueries - * @property {{ * type: string * url: string * }} attributes @@ -1031,7 +1030,13 @@ async function queryBapFor2023PRFData(req, frfReviewItemId) { // Contact__r.Title, // Contact__r.Email, // Contact__r.Phone, - // Contact__r.AccountId + // Contact__r.Account.Id, + // Contact__r.Account.Name, + // Contact__r.Account.BillingStreet, + // Contact__r.Account.BillingCity, + // Contact__r.Account.BillingState, + // Contact__r.Account.BillingPostalCode, + // Contact__r.Account.County__c, // FROM // Line_Item__c // WHERE @@ -1058,7 +1063,13 @@ async function queryBapFor2023PRFData(req, frfReviewItemId) { "Contact__r.Title": 1, "Contact__r.Email": 1, "Contact__r.Phone": 1, - "Contact__r.AccountId": 1, + "Contact__r.Account.Id": 1, + "Contact__r.Account.Name": 1, + "Contact__r.Account.BillingStreet": 1, + "Contact__r.Account.BillingCity": 1, + "Contact__r.Account.BillingState": 1, + "Contact__r.Account.BillingPostalCode": 1, + "Contact__r.Account.County__c": 1, }, ) .execute(async (err, records) => ((await err) ? err : records)); @@ -1066,23 +1077,10 @@ async function queryBapFor2023PRFData(req, frfReviewItemId) { ) ).flat(); - const frf2023BusRecordsContactsAccountIds = [ - ...new Set( - frf2023BusRecordsContactsQueries.map((item) => item.Contact__r.AccountId), - ), - ]; - - const frf2023BusRecordsContactsOrgsQueries = await bapConnection - .sobject("Account") - .retrieve(frf2023BusRecordsContactsAccountIds, async (err, records) => - (await err) ? err : records, - ); - return { frf2023RecordQuery, frf2023BusRecordsQuery, frf2023BusRecordsContactsQueries, - frf2023BusRecordsContactsOrgsQueries, }; } diff --git a/app/server/app/utilities/formio.js b/app/server/app/utilities/formio.js index 28dec57e..44da104f 100644 --- a/app/server/app/utilities/formio.js +++ b/app/server/app/utilities/formio.js @@ -177,7 +177,6 @@ function fetchDataForPRFSubmission({ rebateYear, req, res }) { frf2023RecordQuery, frf2023BusRecordsQuery, frf2023BusRecordsContactsQueries, - frf2023BusRecordsContactsOrgsQueries, } = results; const existingBusOwnerType = "Old Bus Private Fleet Owner (if changed)"; @@ -204,47 +203,59 @@ function fetchDataForPRFSubmission({ rebateYear, req, res }) { CSB_School_District__r?.BillingStreet ?? "\n" ).split("\n"); - const org_organizations = frf2023BusRecordsContactsOrgsQueries.reduce( - (array, frf2023BusRecordsContactsOrg) => { + const org_organizations = frf2023BusRecordsContactsQueries.reduce( + (array, frf2023BusRecordsContact) => { + const { Relationship_Type__c, Contact__r } = + frf2023BusRecordsContact; + + const { + Id: contactId, + FirstName, + LastName, + Title, + Email, + Phone, + Account, + } = Contact__r; + const { Id: orgId, - Name, + Name: orgName, BillingStreet, - BillingCountry, BillingCity, BillingState, BillingPostalCode, - } = frf2023BusRecordsContactsOrg; + County__c, + } = Account; const jsonOrg = frf2023RecordJson.data.organizations.find( - (item) => item.org_orgName === Name, + (item) => item.org_orgName === orgName, ); - if (jsonOrg) { - const [orgStreetAddress1, orgStreetAddress2] = ( - BillingStreet ?? "\n" - ).split("\n"); + const existingBusOwner = Relationship_Type__c === existingBusOwnerType; // prettier-ignore + const newBusOwner = Relationship_Type__c === newBusOwnerType; - const orgContacts = frf2023BusRecordsContactsQueries.filter( - (item) => item.Contact__r.AccountId === orgId, - ); + /** + * Ensure the org exists in the 2023 FRF submission's + * "organizations" array. + */ + if (jsonOrg) { + /** + * If the org has already been added, update org_type as needed + * and and advance to the next org in the loop. + */ + if (array.some((item) => item.org_id === orgId)) { + const org = array.find((item) => item.org_id === orgId); - const existingBusOwner = orgContacts.some( - (item) => item.Relationship_Type__c === existingBusOwnerType, - ); + if (existingBusOwner) org.org_type.existingBusOwner = true; + if (newBusOwner) org.org_type.newBusOwner = true; - const newBusOwner = orgContacts.some( - (item) => item.Relationship_Type__c === newBusOwnerType, - ); + return array; + } - const { - Id: contactId, - FirstName, - LastName, - Title, - Email, - Phone, - } = orgContacts[0].Contact__r ?? {}; + const [orgStreetAddress1, orgStreetAddress2] = ( + BillingStreet ?? "\n" + ).split("\n"); array.push({ org_number: jsonOrg.org_number, @@ -255,7 +266,7 @@ function fetchDataForPRFSubmission({ rebateYear, req, res }) { }, // _org_typeCombined: "", // NOTE: 'Existing Bus Owner, New Bus Owner' org_id: orgId, - org_name: Name, + org_name: orgName, org_contact_id: contactId, org_contactFName: FirstName, org_contactLName: LastName, @@ -264,7 +275,7 @@ function fetchDataForPRFSubmission({ rebateYear, req, res }) { org_contactPhone: Phone, org_address1: orgStreetAddress1, org_address2: orgStreetAddress2, - org_county: BillingCountry, + org_county: County__c, org_city: BillingCity, org_state: { name: BillingState, @@ -314,19 +325,11 @@ function fetchDataForPRFSubmission({ rebateYear, req, res }) { item.Relationship_Type__c === newBusOwnerType, ); - const existingOwnerOrg = frf2023BusRecordsContactsOrgsQueries.find( - (item) => item.Id === existingOwnerRecord?.Contact__r?.AccountId, - ); - - const newOwnerOrg = frf2023BusRecordsContactsOrgsQueries.find( - (item) => item.Id === newOwnerRecord?.Contact__r?.AccountId, - ); - return { bus_busNumber: Rebate_Item_num__c, bus_existingOwner: { - org_id: existingOwnerOrg.Id, - org_name: existingOwnerOrg.Name, + org_id: existingOwnerRecord?.Contact__r?.Account?.Id, + org_name: existingOwnerRecord?.Contact__r?.Account?.Name, org_contact_id: existingOwnerRecord?.Contact__r?.Id, org_contact_fname: existingOwnerRecord?.Contact__r?.FirstName, org_contact_lname: existingOwnerRecord?.Contact__r?.LastName, @@ -345,8 +348,8 @@ function fetchDataForPRFSubmission({ rebateYear, req, res }) { bus_existingRemainingLife: Old_Bus_Estimated_Remaining_Life__c, bus_existingIdlingHours: Old_Bus_Annual_Idling_Hours__c, bus_newOwner: { - org_id: newOwnerOrg?.Id, - org_name: newOwnerOrg?.Name, + org_id: newOwnerRecord?.Contact__r?.Account?.Id, + org_name: newOwnerRecord?.Contact__r?.Account?.Name, org_contact_id: newOwnerRecord?.Contact__r?.Id, org_contact_fname: newOwnerRecord?.Contact__r?.FirstName, org_contact_lname: newOwnerRecord?.Contact__r?.LastName,