241 lines
6.8 KiB
JavaScript
241 lines
6.8 KiB
JavaScript
import { message } from "antd";
|
|
import gql from "graphql-tag";
|
|
import _ from "lodash";
|
|
import moment from "moment";
|
|
import client from "../graphql/GraphQLClient";
|
|
import {
|
|
INSERT_NEW_JOB,
|
|
QUERY_JOB_BY_CLM_NO,
|
|
UPDATE_JOB,
|
|
} from "../graphql/jobs.queries";
|
|
import { QUERY_GROUPS_BY_MAKE_TYPE } from "../graphql/veh_group.queries";
|
|
import { store } from "../redux/store";
|
|
import TrucksList from "./trucks.json";
|
|
import CargoVanList from "./cargovans.json";
|
|
import PassengerVanList from "./passengervans.json";
|
|
import SuvList from "./suvs.json";
|
|
import ipcTypes from "../ipc.types";
|
|
const { logger } = window;
|
|
const { ipcRenderer } = window;
|
|
|
|
export function CalculateVehicleAge(job) {
|
|
const parsedYr = parseInt(job.v_model_yr);
|
|
|
|
const vehicleYr =
|
|
moment().year() + 1 - 2000 >= parsedYr ? 2000 + parsedYr : 1900 + parsedYr;
|
|
|
|
let ret;
|
|
|
|
ret = Math.max(0, moment(job.close_date || new Date()).year() - vehicleYr);
|
|
|
|
return ret;
|
|
}
|
|
|
|
export async function UpsertEstimate(job) {
|
|
const shopId = store.getState().user.bodyshop.id;
|
|
//logger.info("Beginning Upserting job from Renderer.");
|
|
console.log("WINDOW IPCS",ipcRenderer)
|
|
ipcRenderer.send(
|
|
ipcTypes.default.app.toMain.log.info,
|
|
"Beginning Upserting job from Renderer."
|
|
);
|
|
const existingJobs = await client.query({
|
|
query: QUERY_JOB_BY_CLM_NO,
|
|
variables: { clm_no: job.clm_no },
|
|
});
|
|
|
|
job = {
|
|
...job,
|
|
v_mileage: (job.v_mileage !== "" && job.v_mileage) || null,
|
|
v_type: DetermineVehicleType(job),
|
|
v_age: CalculateVehicleAge({
|
|
...job,
|
|
close_date:
|
|
existingJobs.data.jobs[0] && existingJobs.data.jobs[0].close_date,
|
|
}),
|
|
};
|
|
job.group = await DetermineVehicleGroup(job);
|
|
|
|
if (existingJobs.data.jobs.length === 1) {
|
|
let suppDelta = await GetSupplementDelta(
|
|
existingJobs.data.jobs[0].id,
|
|
existingJobs.data.jobs[0].joblines,
|
|
job.joblines.data
|
|
);
|
|
|
|
logger.info("Attemping to update job lines.");
|
|
await client.mutate({
|
|
mutation: gql`
|
|
${suppDelta}
|
|
`,
|
|
});
|
|
delete job.joblines;
|
|
delete job.group; //Added to preserve group already set in the system RPS-49.
|
|
|
|
logger.info("Attemping to update job.");
|
|
await client.mutate({
|
|
mutation: UPDATE_JOB,
|
|
variables: { jobId: existingJobs.data.jobs[0].id, job: job },
|
|
refetchQueries: ["QUERY_JOB_BY_PK"],
|
|
});
|
|
logger.info("Job updated succesfully.");
|
|
} else {
|
|
logger.info("Attemping to insert job record.");
|
|
|
|
await client.mutate({
|
|
mutation: INSERT_NEW_JOB,
|
|
variables: {
|
|
job: { ...job, bodyshopid: shopId },
|
|
},
|
|
refetchQueries: ["QUERY_ALL_JOBS_PAGINATED", "QUERY_JOB_BY_PK"],
|
|
});
|
|
logger.info("Job inserted succesfully.");
|
|
}
|
|
message.success("Job uploaded successfully!");
|
|
}
|
|
|
|
export const GetSupplementDelta = async (jobId, existingLinesO, newLines) => {
|
|
const existingLines = _.cloneDeep(existingLinesO);
|
|
|
|
//console.log("GetSupplementDelta -> newLines", newLines);
|
|
//console.log("GetSupplementDelta -> existingLines", existingLines);
|
|
const linesToInsert = [];
|
|
const linesToUpdate = [];
|
|
|
|
newLines.forEach((newLine) => {
|
|
const matchingIndex = existingLines.findIndex(
|
|
(eL) => eL.unq_seq === newLine.unq_seq
|
|
);
|
|
if (matchingIndex >= 0) {
|
|
//Found a relevant matching line. Add it to lines to update.
|
|
linesToUpdate.push({
|
|
id: existingLines[matchingIndex].id,
|
|
newData: newLine,
|
|
// {
|
|
// ...newLine,
|
|
// ignore:
|
|
// newLine.db_ref === "900511" && newLine.prt_dsmk_p !== 50
|
|
// ? false
|
|
// : existingLines[matchingIndex].ignore,
|
|
// },
|
|
});
|
|
//Splice out item we found for performance.
|
|
existingLines.splice(matchingIndex, 1);
|
|
} else {
|
|
//Didn't find a match. Must be a new line.
|
|
linesToInsert.push(newLine);
|
|
}
|
|
});
|
|
|
|
//Wahtever is left in the existing lines, are lines that should be removed.
|
|
|
|
const insertQueries = linesToInsert.reduce((acc, value, idx) => {
|
|
return acc + generateInsertQuery(value, idx, jobId);
|
|
}, "");
|
|
|
|
const updateQueries = linesToUpdate.reduce((acc, value, idx) => {
|
|
return acc + generateUpdateQuery(value, idx);
|
|
}, "");
|
|
|
|
const removeQueries = existingLines.reduce((acc, value, idx) => {
|
|
return acc + generateRemoveQuery(value, idx);
|
|
}, "");
|
|
|
|
return new Promise((resolve, reject) => {
|
|
resolve(gql`
|
|
mutation SUPPLEMENT_EST_LINES{
|
|
${insertQueries + updateQueries + removeQueries}
|
|
}
|
|
`);
|
|
});
|
|
};
|
|
|
|
const generateInsertQuery = (lineToInsert, index, jobId) => {
|
|
lineToInsert.jobid = jobId;
|
|
return `
|
|
insert_joblines${index}: insert_joblines(objects: ${JSON.stringify(
|
|
lineToInsert
|
|
).replace(/"(\w+)"\s*:/g, "$1:")}) {
|
|
returning {
|
|
id
|
|
}
|
|
}`;
|
|
};
|
|
|
|
const generateUpdateQuery = (lineToUpdate, index) => {
|
|
return `
|
|
update_joblines${index}: update_joblines(where: { id: { _eq: "${
|
|
lineToUpdate.id
|
|
}" } }, _set: ${JSON.stringify(lineToUpdate.newData).replace(
|
|
/"(\w+)"\s*:/g,
|
|
"$1:"
|
|
)}) {
|
|
returning {
|
|
id
|
|
}
|
|
}`;
|
|
};
|
|
|
|
const generateRemoveQuery = (lineToRemove, index) => {
|
|
return `
|
|
delete_joblines_r${index}: delete_joblines_by_pk(id: "${lineToRemove.id}") {
|
|
id
|
|
}`;
|
|
};
|
|
|
|
const DetermineVehicleGroup = async (job) => {
|
|
logger.info(
|
|
"Searching for vehicle groups.!",
|
|
job.v_makedesc.toUpperCase(),
|
|
job.v_type ? job.v_type.toUpperCase() : null
|
|
);
|
|
|
|
const vehicleGroups = await client.query({
|
|
query: QUERY_GROUPS_BY_MAKE_TYPE,
|
|
variables: {
|
|
make: job.v_makedesc.toUpperCase(),
|
|
type: job.v_type,
|
|
date: moment().format("YYYY-MM-DD"),
|
|
},
|
|
});
|
|
|
|
if (vehicleGroups.data.groupings.length === 1) {
|
|
logger.info("Found 1 vehicle group.!", vehicleGroups.data.groupings[0]);
|
|
return vehicleGroups.data.groupings[0].group;
|
|
} else if (vehicleGroups.data.groupings.length === 0) {
|
|
//Uh-oh, should only be 1.
|
|
logger.info("No vehicle groups found.");
|
|
return null;
|
|
} else {
|
|
//Should never be here.
|
|
alert("Fatal error. Multiple vehicle groups found for this claim.");
|
|
logger.error(
|
|
"Found multiple vehicle groups!",
|
|
job.v_makedesc.toUpperCase(),
|
|
job.v_type ? job.v_type.toUpperCase() : null
|
|
);
|
|
}
|
|
|
|
return "";
|
|
};
|
|
|
|
const DetermineVehicleType = (job) => {
|
|
console.log("job.v_type", job.v_type, job.v_model.toUpperCase());
|
|
|
|
const inTrucks = TrucksList.includes(job.v_model.toUpperCase());
|
|
const inPV = PassengerVanList.includes(job.v_model.toUpperCase());
|
|
const inSuv = SuvList.includes(job.v_model.toUpperCase());
|
|
const inCv = CargoVanList.includes(job.v_model.toUpperCase());
|
|
|
|
console.log("inTrucks", inTrucks);
|
|
console.log("inPV", inPV);
|
|
console.log("inSuv", inSuv);
|
|
console.log("inCv", inCv);
|
|
|
|
if (inTrucks) return "TK";
|
|
else if (inPV) return "PV";
|
|
else if (inSuv) return "SUV";
|
|
else if (inCv) return "CV";
|
|
else return job.v_type;
|
|
};
|