Replaced electron-settings with store. Implemented job upsert logic.
This commit is contained in:
@@ -1,16 +1,131 @@
|
||||
import gql from "graphql-tag";
|
||||
import _ from "lodash";
|
||||
import client from "../graphql/GraphQLClient";
|
||||
import { INSERT_NEW_JOB } from "../graphql/jobs.queries";
|
||||
import {
|
||||
INSERT_NEW_JOB,
|
||||
QUERY_JOB_BY_CLM_NO,
|
||||
UPDATE_JOB,
|
||||
} from "../graphql/jobs.queries";
|
||||
import { store } from "../redux/store";
|
||||
|
||||
export async function UpsertEstimate(job) {
|
||||
const shopId = store.getState().user.bodyshop.id;
|
||||
console.log("UpsertEstimate -> shopId", shopId);
|
||||
|
||||
const result = await client.mutate({
|
||||
mutation: INSERT_NEW_JOB,
|
||||
variables: {
|
||||
job: { ...job, bodyshopid: shopId },
|
||||
},
|
||||
//Using the claim number, find out if the job exists. If it doesnt, then we need to create it.
|
||||
const existingJobs = await client.query({
|
||||
query: QUERY_JOB_BY_CLM_NO,
|
||||
variables: { clm_no: job.clm_no },
|
||||
});
|
||||
console.log("UpsertEstimate -> result", result);
|
||||
if (existingJobs.data.jobs.length === 1) {
|
||||
let suppDelta = await GetSupplementDelta(
|
||||
existingJobs.data.jobs[0].id,
|
||||
existingJobs.data.jobs[0].joblines,
|
||||
job.joblines.data
|
||||
);
|
||||
await client.mutate({
|
||||
mutation: gql`
|
||||
${suppDelta}
|
||||
`,
|
||||
});
|
||||
delete job.joblines;
|
||||
|
||||
const updatedJob = await client.mutate({
|
||||
mutation: UPDATE_JOB,
|
||||
variables: { jobId: existingJobs.data.jobs[0].id, job: job },
|
||||
});
|
||||
|
||||
console.log("UpsertEstimate -> updatedJob", updatedJob);
|
||||
} else {
|
||||
console.log("Insert a new job recort.", job);
|
||||
const result = await client.mutate({
|
||||
mutation: INSERT_NEW_JOB,
|
||||
variables: {
|
||||
job: { ...job, bodyshopid: shopId },
|
||||
},
|
||||
refetchQueries: ["QUERY_ALL_JOBS_PAGINATED"],
|
||||
});
|
||||
console.log("UpsertEstimate -> result", result);
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
});
|
||||
//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
|
||||
}`;
|
||||
};
|
||||
|
||||
@@ -4,7 +4,6 @@ import {
|
||||
setWatcherStatus,
|
||||
} from "../redux/application/application.actions";
|
||||
import { store } from "../redux/store";
|
||||
import { message } from "antd";
|
||||
import { UpsertEstimate } from "./ipc-estimate-utils";
|
||||
|
||||
const { ipcRenderer } = window;
|
||||
@@ -16,7 +15,7 @@ ipcRenderer.on("test-toRenderer", (event, obj) => {
|
||||
|
||||
ipcRenderer.on(
|
||||
ipcTypes.default.fileWatcher.toRenderer.filepathsList,
|
||||
(event, ...obj) => {
|
||||
(event, obj) => {
|
||||
store.dispatch(setWatchedPaths(obj));
|
||||
}
|
||||
);
|
||||
@@ -24,37 +23,32 @@ ipcRenderer.on(
|
||||
//Filewatcher Status Section
|
||||
ipcRenderer.on(
|
||||
ipcTypes.default.fileWatcher.toRenderer.startSuccess,
|
||||
(event, ...obj) => {
|
||||
console.log("Watcher ready.");
|
||||
message.success("Watcher started!");
|
||||
store.dispatch(setWatcherStatus("READY!"));
|
||||
(event, obj) => {
|
||||
store.dispatch(setWatcherStatus("Started"));
|
||||
}
|
||||
);
|
||||
ipcRenderer.on(
|
||||
ipcTypes.default.fileWatcher.toRenderer.stopSuccess,
|
||||
(event, ...obj) => {
|
||||
store.dispatch(setWatcherStatus("STOPPED"));
|
||||
}
|
||||
);
|
||||
ipcRenderer.on(
|
||||
ipcTypes.default.fileWatcher.toRenderer.error,
|
||||
(event, ...obj) => {
|
||||
store.dispatch(setWatcherStatus(obj));
|
||||
(event, obj) => {
|
||||
store.dispatch(setWatcherStatus("Stopped"));
|
||||
}
|
||||
);
|
||||
ipcRenderer.on(ipcTypes.default.fileWatcher.toRenderer.error, (event, obj) => {
|
||||
store.dispatch(setWatcherStatus(obj));
|
||||
});
|
||||
|
||||
//Estimate Section
|
||||
ipcRenderer.on(
|
||||
ipcTypes.default.estimate.toRenderer.estimateDecodeStart,
|
||||
(event, ...obj) => {
|
||||
(event, obj) => {
|
||||
console.log("Decoding started!");
|
||||
}
|
||||
);
|
||||
|
||||
ipcRenderer.on(
|
||||
ipcTypes.default.estimate.toRenderer.estimateDecodeSuccess,
|
||||
async (event, ...obj) => {
|
||||
console.log("Decoding success!", obj[0]);
|
||||
await UpsertEstimate(obj[0]);
|
||||
async (event, obj) => {
|
||||
console.log("obj", obj);
|
||||
await UpsertEstimate(obj);
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user