Files
bodyshop/client/src/components/jobs-detail-header-actions/jobs-detail-header-actions.duplicate.util.js
2021-06-01 16:49:54 -07:00

61 lines
1.5 KiB
JavaScript

import Axios from "axios";
import _ from "lodash";
import { logImEXEvent } from "../../firebase/firebase.utils";
import { INSERT_NEW_JOB, QUERY_JOB_FOR_DUPE } 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_JOB_FOR_DUPE,
variables: { id: jobId },
});
console.log("res", res);
const { jobs_by_pk } = res.data;
const existingJob = _.cloneDeep(jobs_by_pk);
delete existingJob.__typename;
delete existingJob.id;
delete existingJob.createdat;
delete existingJob.updatedat;
const newJob = {
...existingJob,
status: defaultOpenStatus,
};
const _tempLines = _.cloneDeep(existingJob.joblines);
_tempLines.forEach((line) => {
delete line.id;
delete line.__typename;
line.manual_line = true;
});
newJob.joblines = keepJobLines ? _tempLines : [];
delete newJob.joblines;
newJob.joblines = keepJobLines ? { data: _tempLines } : null;
const res2 = await apolloClient.mutate({
mutation: INSERT_NEW_JOB,
variables: { job: [newJob] },
});
await Axios.post("/job/totalsssu", {
id: res2.data.insert_jobs.returning[0].id,
});
if (completionCallback)
completionCallback(res2.data.insert_jobs.returning[0].id);
//insert the new job. call the callback with the returned ID when done.
return;
}