Resolve incorrect age calculation when value may be null.

This commit is contained in:
Patrick Fic
2024-05-06 11:38:11 -07:00
parent 798ea18b5b
commit 9ab6e511b9

View File

@@ -2,12 +2,7 @@ import { message } from "antd";
import gql from "graphql-tag";
import _ from "lodash";
import client from "../graphql/GraphQLClient";
import {
INSERT_NEW_JOB,
QUERY_CLOSE_DATE_BY_CLM_NO,
QUERY_JOB_BY_CLM_NO,
UPDATE_JOB,
} from "../graphql/jobs.queries";
import { INSERT_NEW_JOB, QUERY_CLOSE_DATE_BY_CLM_NO, 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";
@@ -15,34 +10,25 @@ import CargoVanList from "./cargovans.json";
import PassengerVanList from "./passengervans.json";
import SuvList from "./suvs.json";
import ipcTypes from "../ipc.types";
import dayjs from '../util/day.js';
import dayjs from "../util/day.js";
const { logger } = window;
const { ipcRenderer } = window;
export function CalculateVehicleAge(job) {
//Per new rules in 2023, we need to determine which set of rules to apply.
const parsedYr = parseInt(job.v_model_yr);
const vehicleYr =
dayjs().year() + 1 - 2000 >= parsedYr ? 2000 + parsedYr : 1900 + parsedYr;
const closeDate = dayjs(job.close_date);
const lossDate = dayjs(job.loss_date);
const vehicleYr = dayjs().year() + 1 - 2000 >= parsedYr ? 2000 + parsedYr : 1900 + parsedYr;
const closeDate = job.close_date ? dayjs(job.close_date) : dayjs();
const lossDate = job.loss_date ? dayjs(job.loss_date) : dayjs();
let ret;
if (closeDate.isSameOrAfter(dayjs("2023-04-01"))) {
//Post April 2023 rules where the age is calculated based on loss date.
ipcRenderer.send(
ipcTypes.app.toMain.log.debug,
"Using post 0423 ruleset to calculate vehicle age for job.",
job
);
ipcRenderer.send(ipcTypes.app.toMain.log.debug, "Using post 0423 ruleset to calculate vehicle age for job.", job);
ret = Math.max(0, lossDate.year() - vehicleYr);
} else {
//Pre-April 2023 rules where the age was calculated based on model year, not loss date.
ipcRenderer.send(
ipcTypes.app.toMain.log.debug,
"Using pre 0423 ruleset to calculate vehicle age for job.",
job
);
ipcRenderer.send(ipcTypes.app.toMain.log.debug, "Using pre 0423 ruleset to calculate vehicle age for job.", job);
ret = Math.max(0, dayjs(job.close_date || new Date()).year() - vehicleYr);
}
@@ -52,7 +38,7 @@ export function CalculateVehicleAge(job) {
export async function GetR4PDateWithClaim(clm_no) {
const existingJobs = await client.query({
query: QUERY_CLOSE_DATE_BY_CLM_NO,
variables: { clm_no: clm_no },
variables: { clm_no: clm_no }
});
return existingJobs.data.jobs[0] && existingJobs.data.jobs[0].close_date;
}
@@ -61,13 +47,10 @@ 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.app.toMain.log.info,
"Beginning Upserting job from Renderer."
);
ipcRenderer.send(ipcTypes.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 },
variables: { clm_no: job.clm_no }
});
job = {
@@ -77,9 +60,8 @@ export async function UpsertEstimate(job) {
v_type: DetermineVehicleType(job),
v_age: CalculateVehicleAge({
...job,
close_date:
existingJobs.data.jobs[0] && existingJobs.data.jobs[0].close_date,
}),
close_date: existingJobs.data.jobs[0] && existingJobs.data.jobs[0].close_date
})
};
job.group = await DetermineVehicleGroup(job);
@@ -94,7 +76,7 @@ export async function UpsertEstimate(job) {
await client.mutate({
mutation: gql`
${suppDelta}
`,
`
});
delete job.joblines;
delete job.group; //Added to preserve group already set in the system RPS-49.
@@ -103,7 +85,7 @@ export async function UpsertEstimate(job) {
await client.mutate({
mutation: UPDATE_JOB,
variables: { jobId: existingJobs.data.jobs[0].id, job: job },
refetchQueries: ["QUERY_JOB_BY_PK"],
refetchQueries: ["QUERY_JOB_BY_PK"]
});
logger.info("Job updated succesfully.");
} else {
@@ -112,17 +94,17 @@ export async function UpsertEstimate(job) {
await client.mutate({
mutation: INSERT_NEW_JOB,
variables: {
job: { ...job, bodyshopid: shopId },
job: { ...job, bodyshopid: shopId }
},
update(cache, { data }) {
cache.modify({
fields: {
jobs(existingJobs) {
return [data.insert_jobs.returning[0], ...existingJobs];
},
},
}
}
});
},
}
// refetchQueries: ["QUERY_ALL_JOBS_PAGINATED", "QUERY_JOB_BY_PK"],
});
logger.info("Job inserted succesfully.");
@@ -139,14 +121,12 @@ export const GetSupplementDelta = async (jobId, existingLinesO, newLines) => {
const linesToUpdate = [];
newLines.forEach((newLine) => {
const matchingIndex = existingLines.findIndex(
(eL) => eL.unq_seq === newLine.unq_seq
);
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,
newData: newLine
// {
// ...newLine,
// ignore:
@@ -189,9 +169,7 @@ export const GetSupplementDelta = async (jobId, existingLinesO, newLines) => {
const generateInsertQuery = (lineToInsert, index, jobId) => {
lineToInsert.jobid = jobId;
return `
insert_joblines${index}: insert_joblines(objects: ${JSON.stringify(
lineToInsert
).replace(/"(\w+)"\s*:/g, "$1:")}) {
insert_joblines${index}: insert_joblines(objects: ${JSON.stringify(lineToInsert).replace(/"(\w+)"\s*:/g, "$1:")}) {
returning {
id
}
@@ -200,12 +178,9 @@ const generateInsertQuery = (lineToInsert, index, jobId) => {
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:"
)}) {
update_joblines${index}: update_joblines(where: { id: { _eq: "${lineToUpdate.id}" } }, _set: ${JSON.stringify(
lineToUpdate.newData
).replace(/"(\w+)"\s*:/g, "$1:")}) {
returning {
id
}
@@ -231,8 +206,8 @@ const DetermineVehicleGroup = async (job) => {
variables: {
make: job.v_makedesc.toUpperCase(),
type: job.v_type,
date: dayjs().format("YYYY-MM-DD"),
},
date: dayjs().format("YYYY-MM-DD")
}
});
if (vehicleGroups.data.groupings.length === 1) {