Files
imexrps/electron/estimate-scrubber/estimate-scrubber.js
Patrick Fic cf9c237235 ES Cleanup
2025-09-04 09:44:21 -07:00

132 lines
4.5 KiB
JavaScript

const log = require("electron-log");
const axios = require("axios");
const path = require("path");
const { BrowserWindow } = require("electron");
const { default: ipcTypes } = require("../../src/ipc.types.commonjs");
const { promises: fsPromises } = require("fs");
// Function to write job object to logs subfolder
async function writeJobToLogsFolder(job, fileName) {
try {
// Get the directory where electron-log stores its files
const logFilePath = log.transports.file.getFile().path;
const logsDir = path.dirname(logFilePath);
// Create a subfolder for job objects
const jobLogsDir = path.join(logsDir, 'esjson');
// Ensure the directory exists
await fsPromises.mkdir(jobLogsDir, { recursive: true });
// Write the job object as JSON
const jobFilePath = path.join(jobLogsDir, `${fileName}.json`);
await fsPromises.writeFile(jobFilePath, JSON.stringify(job, null, 2), 'utf8');
log.debug(`Job object written to: ${jobFilePath}`);
console.log(`Job object written to: ${jobFilePath}`);
return jobFilePath;
} catch (error) {
log.error('Error writing job object to logs folder:', error);
throw error;
}
}
async function ScrubEstimate({ job }) {
//These are hard coded as they are not secure values and checking happens based on other values.
//No secret or private information is exposed.
const basicAuthUser = "Imex2";
const basicAuthpassword = "Patrick";
const estimateScrubberUrl = "https://insurtechtoolkit.com/api/sendems";
const sendingEntityId = '87330f61-412b-4251-baaa-d026565b23c5'
//Perform data manipulation on the job object
if (!job) {
console.error("No job provided to ScrubEstimate");
return;
}
//Set shop metrics
job.sending_entity_id = sendingEntityId;
job.sending_entity_accept_terms_of_use = true;
job.association_switch = "ATAM";
job.rf_zip = job.bodyshop.zip_post;
job.rf_ph1 = job.bodyshop.phone;
job.g_ttl_amt = job.clm_total;
job.source_system = "M"; //Requested by Steven.
delete job.clm_total;
delete job.bodyshop //Bodyshop has to be passed through the object as we don't have access to the store here.
//Adjust the rates field to be MAT_TYPE instead of MATL_TYPE
if (job.rates && Array.isArray(job.rates)) {
job.rates.forEach(rate => {
if (rate.MATL_TYPE) {
rate.MAT_TYPE = rate.MATL_TYPE;
delete rate.MATL_TYPE;
}
});
}
//Lower case the rates & totals
if (job.rates && Array.isArray(job.rates)) {
job.rates = job.rates.map(rate => {
const lowercasedRate = {};
for (const [key, value] of Object.entries(rate)) {
lowercasedRate[key.toLowerCase()] = value;
}
return lowercasedRate;
});
}
if (job.totals && Array.isArray(job.totals)) {
job.totals = job.totals.map(total => {
const lowercasedTotal = {};
for (const [key, value] of Object.entries(total)) {
lowercasedTotal[key.toLowerCase()] = value;
}
return lowercasedTotal;
});
}
const fileName = `RPS-Scrub-${job.id}-${job.clm_no}-${Date.now()}`;
// Write job object to logs subfolder
try {
await writeJobToLogsFolder(job, fileName);
} catch (error) {
log.error('Failed to write job to logs folder:', error);
// Continue with the rest of the function even if this fails
}
const formData = new FormData();
const jsonString = JSON.stringify(job);
formData.append("file", new Blob([jsonString], { type: "application/json" }), `${fileName}.json`);
const result = await axios.post(estimateScrubberUrl, formData, {
auth: {
username: basicAuthUser,
password: basicAuthpassword
},
headers: formData.getHeaders ? formData.getHeaders() : {}
});
const resultPDFUrl = result?.data?.report_link
// log.log("Estimate Scrubber Result:", result.data, resultPDFUrl);
const b = BrowserWindow.getAllWindows()[0];
b.webContents.send(ipcTypes.app.toRenderer.scrubResults, {
jobid: job.id,
items: result.data?.identified_item,
pdfUrl: resultPDFUrl
});
// const pdfWindow = new BrowserWindow({
// webPreferences: {
// plugins: true, // Enable PDF viewing
// },
// });
// pdfWindow.loadURL(resultPDFUrl);
// pdfWindow.focus();
return resultPDFUrl
}
exports.ScrubEstimate = ScrubEstimate