Skip to content

Commit

Permalink
Merge pull request #399 from USEPA/feature/apply-updates-around-bap-p…
Browse files Browse the repository at this point in the history
…rf-queries

Feature/apply updates around bap prf queries
  • Loading branch information
courtneymyers authored Mar 27, 2024
2 parents e9f2cdc + 4b741a9 commit ad6db61
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 82 deletions.
119 changes: 79 additions & 40 deletions app/server/app/utilities/bap.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,25 @@ const { submissionPeriodOpen } = require("../config/formio");
* Relationship_Type__c: string
* Contact_Organization_Name__c: string
* Contact__r: {
* Id: string
* FirstName: string
* LastName: string
* Title: string
* Email: string
* Phone: string
* AccountId: 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
Expand Down Expand Up @@ -973,50 +987,75 @@ async function queryBapFor2023PRFData(req, frfReviewItemId) {
)
.execute(async (err, records) => ((await err) ? err : records));

const frf2023BusRecordsContactsQueries = await Promise.all(
frf2023BusRecordsQuery.map(async (frf2023BusRecord) => {
const frf2023BusRecordId = frf2023BusRecord.Id;

// `SELECT
// Id,
// Related_Line_Item__c,
// Relationship_Type__c,
// Contact_Organization_Name__c,
// Contact__r.FirstName,
// Contact__r.LastName
// FROM
// Line_Item__c
// WHERE
// RecordTypeId = '${rebateItemRecordTypeId}' AND
// Related_Line_Item__c = '${frf2023BusRecordId}' AND
// CSB_Rebate_Item_Type__c = 'COF Relationship'`

return await bapConnection
.sobject("Line_Item__c")
.find(
{
RecordTypeId: rebateItemRecordTypeId,
Related_Line_Item__c: frf2023BusRecordId,
CSB_Rebate_Item_Type__c: "COF Relationship",
},
{
// "*": 1,
Id: 1, // Salesforce record ID
Related_Line_Item__c: 1,
Relationship_Type__c: 1,
Contact_Organization_Name__c: 1,
"Contact__r.FirstName": 1,
"Contact__r.LastName": 1,
},
)
.execute(async (err, records) => ((await err) ? err : records));
}),
);
const frf2023BusRecordsContactsQueries = (
await Promise.all(
frf2023BusRecordsQuery.map(async (frf2023BusRecord) => {
const frf2023BusRecordId = frf2023BusRecord.Id;

// `SELECT
// Id,
// Related_Line_Item__c,
// Relationship_Type__c,
// Contact_Organization_Name__c,
// Contact__r.Id,
// Contact__r.FirstName,
// Contact__r.LastName
// Contact__r.Title,
// Contact__r.Email,
// Contact__r.Phone,
// Contact__r.AccountId
// FROM
// Line_Item__c
// WHERE
// RecordTypeId = '${rebateItemRecordTypeId}' AND
// Related_Line_Item__c = '${frf2023BusRecordId}' AND
// CSB_Rebate_Item_Type__c = 'COF Relationship'`

return await bapConnection
.sobject("Line_Item__c")
.find(
{
RecordTypeId: rebateItemRecordTypeId,
Related_Line_Item__c: frf2023BusRecordId,
CSB_Rebate_Item_Type__c: "COF Relationship",
},
{
// "*": 1,
Id: 1, // Salesforce record ID
Related_Line_Item__c: 1,
Relationship_Type__c: 1,
Contact_Organization_Name__c: 1,
"Contact__r.Id": 1,
"Contact__r.FirstName": 1,
"Contact__r.LastName": 1,
"Contact__r.Title": 1,
"Contact__r.Email": 1,
"Contact__r.Phone": 1,
"Contact__r.AccountId": 1,
},
)
.execute(async (err, records) => ((await err) ? err : records));
}),
)
).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: frf2023BusRecordsContactsQueries.flat(),
frf2023BusRecordsContactsQueries,
frf2023BusRecordsContactsOrgsQueries,
};
}

Expand Down
114 changes: 72 additions & 42 deletions app/server/app/utilities/formio.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,12 @@ function fetchDataForPRFSubmission({ rebateYear, req, res }) {
frf2023RecordQuery,
frf2023BusRecordsQuery,
frf2023BusRecordsContactsQueries,
frf2023BusRecordsContactsOrgsQueries,
} = results;

const existingBusOwnerType = "Old Bus Private Fleet Owner (if changed)";
const newBusOwnerType = "New Bus Owner";

const {
Primary_Applicant__r,
Alternate_Applicant__r,
Expand All @@ -191,33 +195,67 @@ function fetchDataForPRFSubmission({ rebateYear, req, res }) {
Prioritized_as_Rural__c,
} = frf2023RecordQuery[0];

// TODO: ask BAP for the query for the fields below.
// NOTE: the data from the 2023 FRF is in an 'organizations' field (array of objects)
// which has the exact same fields below, except for "_org_typeCombined"
const org_organizations = new Array(0).map((item) => ({
org_number: Infinity,
org_type: {
existingBusOwner: true,
newBusOwner: true,
privateFleet: false,
},
_org_typeCombined: "", // NOTE: was 'org_hidden_type' in the FRF (example value: 'Existing Bus Owner, New Bus Owner')
org_orgName: "",
org_contactFName: "",
org_contactLName: "",
org_contactTitle: "",
org_contactEmail: "",
org_contactPhone: "",
org_address1: "",
org_address2: "",
org_county: "",
org_city: "",
org_state: {
name: "",
abbreviation: "",
const [schoolDistrictStreetAddress1, schoolDistrictStreetAddress2] = (
CSB_School_District__r?.BillingStreet ?? "\n"
).split("\n");

const org_organizations = frf2023BusRecordsContactsOrgsQueries.map(
(frf2023BusRecordsContactsOrgs) => {
const {
Id,
Name,
BillingStreet,
BillingCountry,
BillingCity,
BillingState,
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 { 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,
};
},
org_zip: "",
}));
);

const bus_buses = frf2023BusRecordsQuery.map((frf2023BusRecord) => {
const {
Expand All @@ -243,27 +281,20 @@ function fetchDataForPRFSubmission({ rebateYear, req, res }) {
} = frf2023BusRecord;

const existingOwnerRecord = frf2023BusRecordsContactsQueries.find(
({ Related_Line_Item__c, Relationship_Type__c }) => {
return (
Related_Line_Item__c === Id &&
Relationship_Type__c === "Existing Bus Owner"
);
},
(item) =>
item.Related_Line_Item__c === Id &&
item.Relationship_Type__c === existingBusOwnerType,
);

const newOwnerRecord = frf2023BusRecordsContactsQueries.find(
({ Related_Line_Item__c, Relationship_Type__c }) => {
return (
Related_Line_Item__c === Id &&
Relationship_Type__c === "New Bus Owner"
);
},
(item) =>
item.Related_Line_Item__c === Id &&
item.Relationship_Type__c === newBusOwnerType,
);

return {
bus_busNumber: Rebate_Item_num__c,
bus_existingOwner: {
org: "", // TODO: ask BAP how to get this value
organization: existingOwnerRecord?.Contact_Organization_Name__c,
orgContactFName: existingOwnerRecord?.Contact__r?.FirstName,
orgContactLName: existingOwnerRecord?.Contact__r?.LastName,
Expand All @@ -282,7 +313,6 @@ function fetchDataForPRFSubmission({ rebateYear, req, res }) {
bus_existingRemainingLife: Old_Bus_Estimated_Remaining_Life__c,
bus_existingIdlingHours: Old_Bus_Annual_Idling_Hours__c,
bus_newOwner: {
org: "", // TODO: ask BAP how to get this value
organization: newOwnerRecord?.Contact_Organization_Name__c,
orgContactFName: newOwnerRecord?.Contact__r?.FirstName,
orgContactLName: newOwnerRecord?.Contact__r?.LastName,
Expand Down Expand Up @@ -330,8 +360,8 @@ function fetchDataForPRFSubmission({ rebateYear, req, res }) {
_bap_alternate_phone_number: Alternate_Applicant__r?.Phone,
_bap_district_ncesID: CSB_NCES_ID__c,
_bap_district_name: CSB_School_District__r?.Name,
_bap_district_address_1: CSB_School_District__r?.BillingStreet, // TODO: once BAP returns this field with a new line character, split on it for address line 1 and 2
_bap_district_address_2: "", // TODO: see above
_bap_district_address_1: schoolDistrictStreetAddress1 || "",
_bap_district_address_2: schoolDistrictStreetAddress2 || "",
_bap_district_city: CSB_School_District__r?.BillingCity,
_bap_district_state: CSB_School_District__r?.BillingState,
_bap_district_zip: CSB_School_District__r?.BillingPostalCode,
Expand Down

0 comments on commit ad6db61

Please sign in to comment.