Files
bodyshop/client/src/components/jobs-detail-header-actions/jobs-detail-header-actions.duplicate.util.js
2021-02-25 11:32:07 -08:00

80 lines
2.0 KiB
JavaScript

import Axios from "axios";
import _ from "lodash";
import { logImEXEvent } from "../../firebase/firebase.utils";
import {
INSERT_NEW_JOB,
QUERY_ALL_JOB_FIELDS,
} from "../../graphql/jobs.queries";
export default async function DuplicateJob(
apolloClient,
jobId,
config,
completionCallback,
keepJobLines = false
) {
logImEXEvent("job_duplicate");
const { defaultOpenStatus } = config;
//get a list of all fields on the job
const res = await apolloClient.query({
query: QUERY_ALL_JOB_FIELDS,
variables: { id: jobId },
});
console.log("res", res);
const { jobs_by_pk: existingJob } = res.data;
const newJob = {
date_estimated: new Date(),
shopid: existingJob.shopid,
status: defaultOpenStatus,
ownerid: existingJob.ownerid,
ownr_fn: existingJob.ownr_fn,
ownr_ln: existingJob.ownr_ln,
ownr_co_nm: existingJob.ownr_co_nm,
ownr_addr1: existingJob.ownr_addr1,
ownr_addr2: existingJob.ownr_addr2,
ownr_st: existingJob.ownr_st,
ownr_zip: existingJob.ownr_zip,
ownr_ctry: existingJob.ownr_ctry,
ownr_ph1: existingJob.ownr_ph1,
vehicleid: existingJob.vehicleid,
v_vin: existingJob.v_vin,
v_make_desc: existingJob.v_make_desc,
v_model_desc: existingJob.v_model_desc,
v_model_yr: existingJob.v_model_yr,
};
const _tempLines = _.cloneDeep(existingJob.joblines);
_tempLines.forEach((line) => {
delete line.id;
delete line.__typename;
line.manual_line = true;
});
newJob.joblines = keepJobLines ? _tempLines : [];
newJob.job_totals = (
await Axios.post("/job/totals", {
job: newJob,
})
).data;
delete newJob.joblines;
newJob.joblines = keepJobLines ? { data: _tempLines } : null;
apolloClient
.mutate({
mutation: INSERT_NEW_JOB,
variables: { job: [newJob] },
})
.then((res2) => {
if (completionCallback)
completionCallback(res2.data.insert_jobs.returning[0].id);
});
//insert the new job. call the callback with the returned ID when done.
return;
}