Files
esdp/serverless/src/handlers/scrub.js
2026-01-12 14:26:30 -08:00

63 lines
2.2 KiB
JavaScript

const axios = require('axios');
const FormData = require('form-data');
const ES_USER = process.env.ES_USER;
const ES_PASSWORD = process.env.ES_PASSWORD;
const ES_ENDPOINT = process.env.ES_ENDPOINT;
const getVehicleType = require("../lib/vehicleTypes/vehicleType").getVehicleType;
exports.handler = async (event) => {
// Path parameters are automatically parsed for you
//Take the estimate details, add them to the database, scrub it, and then retrun the result and add the scrubbed results to database.
try {
const { esApiKey, estimate } = event.body ? JSON.parse(event.body) : {};
//TODO: Replace the previous vehicle logic with just getting it here.
estimate.v_type = getVehicleType(estimate.v_model).type;
estimate.sendingEntityId = "87330f61-412b-4251-baaa-d026565b23c5";
const fileName = `${esApiKey}-${estimate.clm_no}-${Date.now()}`;
const formData = new FormData();
const jsonString = JSON.stringify(estimate);
formData.append(
"file",
Buffer.from(jsonString),
{
filename: `${fileName}.json`,
contentType: 'application/json'
}
);
const result = await axios.post(
`${ES_ENDPOINT}/api/sendems`,
formData,
{
auth: {
username: ES_USER,
password: ES_PASSWORD,
},
headers: {
APIkey: esApiKey,
},
},
);
const resultPDFUrl = result?.data?.report_link;
const reportIssueUrl = `https://insurtechtoolkit.com/pcontactUs.aspx?apiKey=${esApiKey}&file=${fileName}.json`;
return {
statusCode: 200,
body: JSON.stringify({
resultPDFUrl, reportIssueUrl, identified_item: result.data?.identified_item
}),
}
}
catch (error) {
return {
statusCode: 400,
body: JSON.stringify({
message: `Error scrubbing estimate.`,
error: error.response.data || error.message || 'Unknown error',
})
};
}
};