Remove serverless, add graphql relationships.
This commit is contained in:
@@ -1,76 +1,10 @@
|
||||
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
|
||||
import axios, { AxiosError } from 'axios';
|
||||
import FormData from 'form-data';
|
||||
|
||||
import { ESJobObject, RawJobDataObject } from '../../../shared/types';
|
||||
import { transformJobForEstimateScrubber } from '../lib/transformEstimate';
|
||||
import { getVehicleType } from '../lib/vehicleTypes/vehicleType';
|
||||
|
||||
const ES_USER = process.env.ES_USER || '';
|
||||
const ES_PASSWORD = process.env.ES_PASSWORD || '';
|
||||
const ES_ENDPOINT = process.env.ES_ENDPOINT || '';
|
||||
|
||||
interface ScrubRequest {
|
||||
esApiKey: string;
|
||||
rawJob: RawJobDataObject;
|
||||
}
|
||||
|
||||
interface ScrubResponse {
|
||||
report_link?: string;
|
||||
identified_item?: unknown;
|
||||
}
|
||||
|
||||
export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
|
||||
try {
|
||||
const { esApiKey, rawJob } = JSON.parse(event.body || '{}') as ScrubRequest;
|
||||
|
||||
// Transform the raw job object to ES format
|
||||
const estimate: ESJobObject = await transformJobForEstimateScrubber(rawJob);
|
||||
|
||||
// Set vehicle type and sending entity ID
|
||||
estimate.v_type = getVehicleType(estimate.v_model || '').type;
|
||||
estimate.sending_entity_id = '87330f61-412b-4251-baaa-d026565b23c5';
|
||||
|
||||
const fileName = `${esApiKey}-${rawJob.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<ScrubResponse>(`${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) {
|
||||
const axiosError = error as AxiosError;
|
||||
const errorMessage = axiosError.response?.data || axiosError.message || 'Unknown error';
|
||||
|
||||
return {
|
||||
statusCode: 400,
|
||||
body: JSON.stringify({
|
||||
message: 'Error scrubbing estimate.',
|
||||
error: errorMessage,
|
||||
}),
|
||||
};
|
||||
}
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: JSON.stringify({
|
||||
message: 'Scrub handler is under construction',
|
||||
}),
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user