diff --git a/app/server/app/utilities/bap.js b/app/server/app/utilities/bap.js index 821c8dca..41e18692 100644 --- a/app/server/app/utilities/bap.js +++ b/app/server/app/utilities/bap.js @@ -110,6 +110,9 @@ const { submissionPeriodOpen } = require("../config/formio"); * @typedef {Object} BapDataFor2023PRF * @property {{ * Id: string + * CSB_Snapshot__r: { + * JSON_Snapshot__c: string + * } * Primary_Applicant__r: { * FirstName: string * LastName: string @@ -825,6 +828,7 @@ async function queryBapFor2023PRFData(req, frfReviewItemId) { // `SELECT // Id, + // CSB_Snapshot__r.JSON_Snapshot__c // Primary_Applicant__r.FirstName, // Primary_Applicant__r.LastName, // Primary_Applicant__r.Title, @@ -869,6 +873,7 @@ async function queryBapFor2023PRFData(req, frfReviewItemId) { { // "*": 1, Id: 1, // Salesforce record ID + "CSB_Snapshot__r.JSON_Snapshot__c": 1, "Primary_Applicant__r.FirstName": 1, "Primary_Applicant__r.LastName": 1, "Primary_Applicant__r.Title": 1, diff --git a/app/server/app/utilities/formio.js b/app/server/app/utilities/formio.js index 5b8103ad..41feedb3 100644 --- a/app/server/app/utilities/formio.js +++ b/app/server/app/utilities/formio.js @@ -183,6 +183,7 @@ function fetchDataForPRFSubmission({ rebateYear, req, res }) { const newBusOwnerType = "New Bus Owner"; const { + CSB_Snapshot__r, Primary_Applicant__r, Alternate_Applicant__r, CSB_School_District__r, @@ -195,12 +196,14 @@ function fetchDataForPRFSubmission({ rebateYear, req, res }) { Prioritized_as_Rural__c, } = frf2023RecordQuery[0]; + const frf2023RecordJson = JSON.parse(CSB_Snapshot__r.JSON_Snapshot__c); + const [schoolDistrictStreetAddress1, schoolDistrictStreetAddress2] = ( CSB_School_District__r?.BillingStreet ?? "\n" ).split("\n"); - const org_organizations = frf2023BusRecordsContactsOrgsQueries.map( - (frf2023BusRecordsContactsOrgs) => { + const org_organizations = frf2023BusRecordsContactsOrgsQueries.reduce( + (array, frf2023BusRecordsContactsOrgs) => { const { Id, Name, @@ -211,50 +214,59 @@ function fetchDataForPRFSubmission({ rebateYear, req, res }) { BillingPostalCode, } = frf2023BusRecordsContactsOrgs; - const [orgStreetAddress1, orgStreetAddress2] = ( - BillingStreet ?? "\n" - ).split("\n"); - - const orgContacts = frf2023BusRecordsContactsQueries.filter( - (item) => item.Contact__r.AccountId === Id, - ); - - const existingBusOwner = orgContacts.some( - (item) => item.Relationship_Type__c === existingBusOwnerType, - ); - - const newBusOwner = orgContacts.some( - (item) => item.Relationship_Type__c === newBusOwnerType, + const jsonOrg = frf2023RecordJson.data.organizations.find( + (item) => item.org_orgName === Name, ); - const { FirstName, LastName, Title, Email, Phone } = - orgContacts[0].Contact__r ?? {}; - - return { - org_number: null, - org_type: { - existingBusOwner, - newBusOwner, - // privateFleet: false, - }, - // _org_typeCombined: "", // NOTE: 'Existing Bus Owner, New Bus Owner' - org_orgName: Name, - org_contactFName: FirstName, - org_contactLName: LastName, - org_contactTitle: Title, - org_contactEmail: Email, - org_contactPhone: Phone, - org_address1: orgStreetAddress1, - org_address2: orgStreetAddress2, - org_county: BillingCountry, - org_city: BillingCity, - org_state: { - name: BillingState, - // abbreviation: "", - }, - org_zip: BillingPostalCode, - }; + if (jsonOrg) { + const [orgStreetAddress1, orgStreetAddress2] = ( + BillingStreet ?? "\n" + ).split("\n"); + + const orgContacts = frf2023BusRecordsContactsQueries.filter( + (item) => item.Contact__r.AccountId === Id, + ); + + const existingBusOwner = orgContacts.some( + (item) => item.Relationship_Type__c === existingBusOwnerType, + ); + + const newBusOwner = orgContacts.some( + (item) => item.Relationship_Type__c === newBusOwnerType, + ); + + const { FirstName, LastName, Title, Email, Phone } = + orgContacts[0].Contact__r ?? {}; + + array.push({ + org_number: jsonOrg.org_number, + org_type: { + existingBusOwner, + newBusOwner, + // privateFleet: false, + }, + // _org_typeCombined: "", // NOTE: 'Existing Bus Owner, New Bus Owner' + org_orgName: Name, + org_contactFName: FirstName, + org_contactLName: LastName, + org_contactTitle: Title, + org_contactEmail: Email, + org_contactPhone: Phone, + org_address1: orgStreetAddress1, + org_address2: orgStreetAddress2, + org_county: BillingCountry, + org_city: BillingCity, + org_state: { + name: BillingState, + // abbreviation: "", + }, + org_zip: BillingPostalCode, + }); + } + + return array; }, + [], ); const bus_buses = frf2023BusRecordsQuery.map((frf2023BusRecord) => {