Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/update bap 2023 prf data queries #403

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 23 additions & 25 deletions app/server/app/utilities/bap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -1058,31 +1063,24 @@ 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));
}),
)
).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,
};
}

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

const existingBusOwnerType = "Old Bus Private Fleet Owner (if changed)";
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Loading