diff --git a/serverless/src/handlers/scrub copy.ts b/serverless/src/handlers/scrub copy.ts new file mode 100644 index 0000000..95dcafd --- /dev/null +++ b/serverless/src/handlers/scrub copy.ts @@ -0,0 +1,144 @@ +import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'; +import axios, { AxiosError } from 'axios'; +import FormData from 'form-data'; +import { GraphQLClient, gql } from 'graphql-request'; +import { ESJobObject, RawJobDataObject } from '../../../shared/types'; +import { transformJobForEstimateScrubber } from '../lib/transformEstimate'; +import { getVehicleType } from '../lib/vehicleTypes/vehicleType'; +import { UUID } from 'node:crypto'; + +const ES_USER = process.env.ES_USER || ''; +const ES_PASSWORD = process.env.ES_PASSWORD || ''; +const ES_ENDPOINT = process.env.ES_ENDPOINT || ''; +const HASURA_URL = process.env.HASURA_URL || ''; + +interface ScrubRequest { + esApiKey: string; + rawJob: RawJobDataObject; +} + +interface ScrubResponse { + report_link?: string; + identified_item?: unknown; +} + +export const handler = async (event: APIGatewayProxyEvent): Promise => { + try { + const { esApiKey, rawJob } = JSON.parse(event.body || '{}') as ScrubRequest; + + await uploadJobToHasura(rawJob, esApiKey); + // 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', + }); + + return { + statusCode: 200, + body: JSON.stringify({ + message: 'Debug - skipping ES scrub call', + // Uncomment below to enable actual ES scrub call + // ...await callEstimateScrubber(esApiKey, formData, fileName), + }), + }; + + 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) { + console.log('Error in scrub handler:', error); + return { + statusCode: 500, + body: JSON.stringify({ + message: 'Internal server error.', + error: (error as Error).message, + stack: (error as Error).stack, + }), + }; + + 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, + }), + }; + } +}; + +const uploadJobToHasura = async (rawJob: RawJobDataObject, esApiKey: string): Promise => { + const graphQLClient = new GraphQLClient(HASURA_URL, { + headers: { + 'x-hasura-admin-secret': 'UXWqeUlNMc2dd2SD7DTOKgjEQlVkZkaW', + }, + }); + + //Fetch to get the latest version of this based on the es api key and clm no + + const version = await graphQLClient.request<{ + jobs: { version: number; id: UUID; clm_no: string }[]; + }>(LATEST_VERSION_BY_CLM_NO, { + esApiKey, + clm_no: rawJob.clm_no || '', + }); + + //Manipulate the raw job to insert to the database. + rawJob.version = version.jobs.length > 0 ? version.jobs[0].version + 1 : 1; + + await graphQLClient.request(INSERT_JOB, { + job: rawJob, + }); +}; + +const LATEST_VERSION_BY_CLM_NO = gql` + query LATEST_VERSION_BY_CLM_NO($esApiKey: String!, $clm_no: String!) { + jobs( + where: { clm_no: { _eq: $clm_no }, shop: { es_api_key: { _eq: $esApiKey } } } + order_by: { version: desc } + limit: 1 + ) { + id + clm_no + version + } + } +`; + +const INSERT_JOB = gql` + mutation INSERT_JOB($job: jobs_insert_input!) { + insert_jobs_one(object: $job) { + id + } + } +`;