lml-main
Version:
This is now a mono repository published into many standalone packages.
142 lines • 6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const moment = require("moment");
const actions_1 = require("../actions");
// NOTE: importing this from the top level jobs/selectors package
// will cause a circular dep that makes extract-text-webpack-plugin fail
// which actually makes 0 sense whatsoever - this import has nothing to do with styles
// we need to investigate why this circular dep issue is happening
const job_1 = require("../../jobs/selectors/job");
const DATE_FORMAT = 'YYY-MM-DDThh:mm:ssZ';
const defaultJobFields = {
label: {},
vehicleType: 'small-van',
customer: {},
metadata: {},
stops: [],
};
const getDefaultCollectionStop = () => ({
type: 'collection',
contact: {},
metadata: {},
order: 1,
});
const getDefaultDeliveryStop = () => ({
type: 'delivery',
contact: {},
metadata: {},
order: 2,
});
/**
* Create the request body for editing or posting a job
* Right now you can either clone or edit, but in the future
* there may be other types as well
*/
exports.mkJobRequestBody = (form, jobFormType, baseJob) => {
const isPostRequest = jobFormType !== actions_1.JOB_FORM_TYPE_EDIT;
// BASE JOB
// if a baseJob is provided then the new job will be cloned from that
// otherwise instantiate a new job from default fields
const job = baseJob ? lodash_1.cloneDeep(baseJob) : lodash_1.cloneDeep(defaultJobFields);
// STOPS
let collectionStop;
let deliveryStop;
if (baseJob) {
// otherwise use the appropriate stop from the baseJob
collectionStop = lodash_1.cloneDeep(job_1.getCollectionStop(baseJob));
deliveryStop = lodash_1.cloneDeep(job_1.getDeliveryStop(baseJob));
}
else {
// if no base job is provided then use the default template
collectionStop = getDefaultCollectionStop();
deliveryStop = getDefaultDeliveryStop();
}
// REF IDS
const oldRefId = job.refId;
// if you are editing a job the refIds should not be changed or removed
// if you are posting a new job then you must change the refIds
// for now we simply add the timestamp to the refId
// if this seems hacky and weird then complain to julien about it
if (isPostRequest) {
const timestamp = Date.now();
const { jobNumber } = job;
// modify the existing refId rather than deleting it
// this will ensure the cloned job's refId has the
// same namespace as the original job
job.refId = job.refId + timestamp;
// also have to change the refId of the stops
// courier out needs to be able to see the job number inside the stop refId
// however the job service needs it to be a new stop refId
// therefore we mock the jobNumber with the current timestamp
collectionStop.refId = collectionStop.refId + timestamp + '-1';
deliveryStop.refId = deliveryStop.refId + timestamp + '-2';
}
// TIMESTAMPS
collectionStop.stopTime = collectionStop.stopTime || {};
deliveryStop.stopTime = deliveryStop.stopTime || {};
const collectionEarliestTime = moment(`${form.collectDate.value}T${form.collectEarliestTime.value}Z`).toISOString();
const collectionLatestTime = moment(`${form.collectDate.value}T${form.collectLatestTime.value}Z`).toISOString();
const deliveryEarliestTime = moment(`${form.deliverDate.value}T${form.deliverEarliestTime.value}Z`).toISOString();
const deliveryLatestTime = moment(`${form.deliverDate.value}T${form.deliverLatestTime.value}Z`).toISOString();
collectionStop.stopTime = {
earliestTime: collectionEarliestTime,
latestTime: collectionLatestTime,
};
deliveryStop.stopTime = {
earliestTime: deliveryEarliestTime,
latestTime: deliveryLatestTime,
};
if (!collectionEarliestTime || !collectionLatestTime || !deliveryEarliestTime || !deliveryLatestTime) {
throw new Error(`Invalid timestamp for job: ${job.refId}`);
}
// legacy timestamp fields
job.readyAt = collectionEarliestTime;
job.pickupBy = collectionLatestTime;
job.completeBy = deliveryLatestTime;
// METADATA
// for legacy reasons the just eat order number is saved in the metadata
job.metadata.department = form.customerOrderNo.value;
job.metadata.items = form.items.value;
collectionStop.metadata = collectionStop.metadata || {};
deliveryStop.metadata = deliveryStop.metadata || {};
// COMPANY
collectionStop.metadata.company = form.collectCompanyName.value;
deliveryStop.metadata.company = form.deliverCompanyName.value;
// CONTACT
collectionStop.contact = collectionStop.contact || {};
collectionStop.contact.name = form.collectName.value;
collectionStop.contact.phone = form.collectPhone.value;
deliveryStop.contact = deliveryStop.contact || {};
deliveryStop.contact.name = form.deliverName.value;
deliveryStop.contact.phone = form.deliverPhone.value;
// INSTRUCTIONS
collectionStop.instructions = form.collectInstructions.value;
deliveryStop.instructions = form.deliverInstructions.value;
// LOCATION
// still using old format for jobs service
collectionStop.location = {
address: form.collectAddress.address,
position: {
type: 'Point',
coordinates: [
form.collectAddress.position.coordinates.longitude,
form.collectAddress.position.coordinates.latitude,
],
},
};
deliveryStop.location = {
address: form.deliverAddress.address,
position: {
type: 'Point',
coordinates: [
form.deliverAddress.position.coordinates.longitude,
form.deliverAddress.position.coordinates.latitude,
],
},
};
job.stops[0] = collectionStop;
job.stops[1] = deliveryStop;
return job;
};
//# sourceMappingURL=job-request.js.map