Files
bodyshop/client/src/components/jobs-detail-header-actions/jobs-detail-header-actions.duplicate.util.js

51 lines
1.3 KiB
JavaScript

import {
QUERY_ALL_JOB_FIELDS,
INSERT_NEW_JOB,
} from "../../graphql/jobs.queries";
import { logImEXEvent } from "../../firebase/firebase.utils";
export default function DuplicateJob(
apolloClient,
jobId,
config,
completionCallback
) {
logImEXEvent("job_duplicate");
const { defaultOpenStatus } = config;
//get a list of all fields on the job
apolloClient
.query({ query: QUERY_ALL_JOB_FIELDS, variables: { id: jobId } })
.then((res) => {
const { jobs_by_pk: existingJob } = res.data;
delete existingJob.__typename;
delete existingJob.id;
existingJob.date_estimated = new Date();
existingJob.status = defaultOpenStatus;
const _tempLines = existingJob.joblines;
_tempLines.forEach((line) => {
delete line.id;
delete line.__typename;
});
delete existingJob.joblines;
existingJob.joblines = { data: _tempLines };
apolloClient
.mutate({
mutation: INSERT_NEW_JOB,
variables: { job: [existingJob] },
})
.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;
}