278 lines
8.1 KiB
JavaScript
278 lines
8.1 KiB
JavaScript
const GraphQLClient = require("graphql-request").GraphQLClient;
|
|
const path = require("path");
|
|
const queries = require("../graphql-client/queries");
|
|
const Dinero = require("dinero.js");
|
|
const moment = require("moment");
|
|
const logger = require("../utils/logger");
|
|
const _ = require("lodash");
|
|
require("dotenv").config({
|
|
path: path.resolve(
|
|
process.cwd(),
|
|
`.env.${process.env.NODE_ENV || "development"}`
|
|
),
|
|
});
|
|
|
|
exports.job = async (req, res) => {
|
|
const BearerToken = req.headers.authorization;
|
|
const { jobId } = req.body;
|
|
try {
|
|
logger.log("smart-scheduling-start", "DEBUG", req.user.email, jobId, null);
|
|
|
|
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {
|
|
headers: {
|
|
Authorization: BearerToken,
|
|
},
|
|
});
|
|
|
|
const result = await client
|
|
.setHeaders({ Authorization: BearerToken })
|
|
.request(queries.QUERY_UPCOMING_APPOINTMENTS, {
|
|
now: moment().startOf("day"),
|
|
jobId: jobId,
|
|
});
|
|
|
|
const { jobs_by_pk, blockedDays, prodJobs, arrJobs, compJobs } = result;
|
|
const { ssbuckets, workingdays } = result.jobs_by_pk.bodyshop;
|
|
const jobHrs = result.jobs_by_pk.jobhrs.aggregate.sum.mod_lb_hrs;
|
|
|
|
const JobBucket = ssbuckets.filter(
|
|
(bucket) =>
|
|
bucket.gte <= jobHrs && (!!bucket.lt ? bucket.lt > jobHrs : true)
|
|
)[0];
|
|
|
|
const load = {
|
|
productionTotal: {},
|
|
};
|
|
//Set the current load.
|
|
ssbuckets.forEach((bucket) => {
|
|
load.productionTotal[bucket.id] = { count: 0, label: bucket.label };
|
|
});
|
|
|
|
const filteredProdJobsList = prodJobs.filter(
|
|
(j) => JobBucket.id === CheckJobBucket(ssbuckets, j)
|
|
);
|
|
|
|
filteredProdJobsList.forEach((item) => {
|
|
//Add all of the jobs currently in production to the buckets so that we have a starting point.
|
|
const bucketId = CheckJobBucket(ssbuckets, item);
|
|
if (bucketId) {
|
|
load.productionTotal[bucketId].count =
|
|
load.productionTotal[bucketId].count + 1;
|
|
} else {
|
|
console.log("Uh oh, this job doesn't fit in a bucket!", item);
|
|
}
|
|
});
|
|
|
|
const filteredArrJobs = arrJobs.filter(
|
|
(j) => JobBucket.id === CheckJobBucket(ssbuckets, j)
|
|
);
|
|
|
|
filteredArrJobs.forEach((item) => {
|
|
const itemDate = moment(item.scheduled_in).format("yyyy-MM-DD");
|
|
if (!!load[itemDate]) {
|
|
load[itemDate].hoursIn =
|
|
(load[itemDate].hoursIn || 0) +
|
|
item.labhrs.aggregate.sum.mod_lb_hrs +
|
|
item.larhrs.aggregate.sum.mod_lb_hrs;
|
|
load[itemDate].jobsIn.push(item);
|
|
} else {
|
|
load[itemDate] = {
|
|
jobsIn: [item],
|
|
jobsOut: [],
|
|
hoursIn:
|
|
item.labhrs.aggregate.sum.mod_lb_hrs +
|
|
item.larhrs.aggregate.sum.mod_lb_hrs,
|
|
};
|
|
}
|
|
});
|
|
|
|
//Get the completing jobs.
|
|
let problemJobs = [];
|
|
const filteredCompJobs = compJobs.filter(
|
|
(j) => JobBucket.id === CheckJobBucket(ssbuckets, j)
|
|
);
|
|
|
|
filteredCompJobs.forEach((item) => {
|
|
const inProdJobs = filteredProdJobsList.find((p) => p.id === item.id);
|
|
const inArrJobs = filteredArrJobs.find((p) => p.id === item.id);
|
|
|
|
if (
|
|
!(inProdJobs || inArrJobs) &&
|
|
!moment(item.actual_completion || item.scheduled_completion).isSame(
|
|
moment(),
|
|
"day"
|
|
)
|
|
) {
|
|
// NOT FOUND!
|
|
console.log("PROBLEM JOB", item);
|
|
problemJobs.push({
|
|
...item,
|
|
code: "Job is scheduled for completion, but it is not marked in production nor is it an arriving job in this period. Check the scheduled in and completion dates",
|
|
});
|
|
return;
|
|
} else {
|
|
const itemDate = moment(
|
|
item.actual_completion || item.scheduled_completion
|
|
).format("yyyy-MM-DD");
|
|
if (!!load[itemDate]) {
|
|
load[itemDate].hoursOut =
|
|
(load[itemDate].hoursOut || 0) +
|
|
item.labhrs.aggregate.sum.mod_lb_hrs +
|
|
item.larhrs.aggregate.sum.mod_lb_hrs;
|
|
load[itemDate].jobsOut.push(item);
|
|
} else {
|
|
load[itemDate] = {
|
|
jobsOut: [item],
|
|
hoursOut:
|
|
item.labhrs.aggregate.sum.mod_lb_hrs +
|
|
item.larhrs.aggregate.sum.mod_lb_hrs,
|
|
};
|
|
}
|
|
}
|
|
});
|
|
|
|
//Propagate the expected load to each day.
|
|
const yesterday = moment().subtract(1, "day");
|
|
const today = moment().startOf("day");
|
|
|
|
const end = moment.max([
|
|
...filteredArrJobs.map((a) => moment(a.scheduled_in)),
|
|
...filteredCompJobs
|
|
.map((p) => moment(p.actual_completion || p.scheduled_completion))
|
|
.filter((p) => p.isValid() && p.isAfter(yesterday)),
|
|
]);
|
|
const range = Math.round(moment.duration(end.diff(today)).asDays());
|
|
for (var day = 0; day < range; day++) {
|
|
const current = moment(today).add(day, "days").format("yyyy-MM-DD");
|
|
const prev = moment(today)
|
|
.add(day - 1, "days")
|
|
.format("yyyy-MM-DD");
|
|
if (!!!load[current]) {
|
|
load[current] = {};
|
|
}
|
|
if (day === 0) {
|
|
//Starting on day 1. The load is current.
|
|
load[current].expectedLoad = CalculateLoad(
|
|
load.productionTotal,
|
|
ssbuckets,
|
|
load[current].jobsIn || [],
|
|
load[current].jobsOut || []
|
|
);
|
|
} else {
|
|
load[current].expectedLoad = CalculateLoad(
|
|
load[prev].expectedLoad,
|
|
ssbuckets,
|
|
load[current].jobsIn || [],
|
|
load[current].jobsOut || []
|
|
);
|
|
}
|
|
}
|
|
|
|
//Add in all of the blocked days.
|
|
|
|
blockedDays.forEach((b) => {
|
|
//Find it in the load, set it as blocked.
|
|
const startIsoFormat = moment(b.start).format("YYYY-MM-DD");
|
|
if (load[startIsoFormat]) load[startIsoFormat].blocked = true;
|
|
else {
|
|
load[startIsoFormat] = { blocked: true };
|
|
}
|
|
});
|
|
// //Propose the first 5 dates where we are below target.
|
|
|
|
const possibleDates = [];
|
|
delete load.productionTotal;
|
|
const loadKeys = Object.keys(load).sort((a, b) =>
|
|
moment(a).isAfter(moment(b)) ? 1 : -1
|
|
);
|
|
|
|
loadKeys.forEach((loadKey) => {
|
|
const isShopOpen =
|
|
(workingdays[dayOfWeekMapper(moment(loadKey).day())] || false) &&
|
|
!load[loadKey].blocked;
|
|
|
|
if (
|
|
load[loadKey].expectedLoad &&
|
|
load[loadKey].expectedLoad[JobBucket.id] &&
|
|
JobBucket.target > load[loadKey].expectedLoad[JobBucket.id].count &&
|
|
isShopOpen
|
|
)
|
|
possibleDates.push(new Date(loadKey).toISOString().substr(0, 10));
|
|
});
|
|
|
|
if (possibleDates.length < 6) {
|
|
res.json(possibleDates);
|
|
} else {
|
|
res.json(possibleDates.slice(0, 5));
|
|
}
|
|
} catch (error) {
|
|
logger.log("smart-scheduling-error", "ERROR", req.user.email, jobId, {
|
|
error,
|
|
});
|
|
res.status(400).send(error);
|
|
}
|
|
};
|
|
|
|
const dayOfWeekMapper = (numberOfDay) => {
|
|
switch (numberOfDay) {
|
|
case 0:
|
|
return "sunday";
|
|
case 1:
|
|
return "monday";
|
|
case 2:
|
|
return "tuesday";
|
|
case 3:
|
|
return "wednesday";
|
|
case 4:
|
|
return "thursday";
|
|
case 5:
|
|
return "friday";
|
|
case 6:
|
|
return "saturday";
|
|
}
|
|
};
|
|
|
|
const CheckJobBucket = (buckets, job) => {
|
|
const jobHours =
|
|
job.labhrs.aggregate.sum.mod_lb_hrs + job.larhrs.aggregate.sum.mod_lb_hrs;
|
|
|
|
const matchingBucket = buckets.filter((b) =>
|
|
b.gte <= jobHours && b.lt ? b.lt > jobHours : true
|
|
);
|
|
|
|
return matchingBucket[0] && matchingBucket[0].id;
|
|
};
|
|
|
|
const CalculateLoad = (currentLoad, buckets, jobsIn, jobsOut) => {
|
|
//Add the jobs coming
|
|
const newLoad = _.cloneDeep(currentLoad);
|
|
jobsIn.forEach((job) => {
|
|
const bucketId = CheckJobBucket(buckets, job);
|
|
if (bucketId) {
|
|
newLoad[bucketId].count = newLoad[bucketId].count + 1;
|
|
} else {
|
|
console.log(
|
|
"[Util Arr Job]Uh oh, this job doesn't fit in a bucket!",
|
|
job
|
|
);
|
|
}
|
|
});
|
|
|
|
jobsOut.forEach((job) => {
|
|
const bucketId = CheckJobBucket(buckets, job);
|
|
if (bucketId) {
|
|
newLoad[bucketId].count = newLoad[bucketId].count - 1;
|
|
if (newLoad[bucketId].count < 0) {
|
|
console.log("***ERROR: NEGATIVE LOAD Bucket =>", bucketId, job);
|
|
}
|
|
} else {
|
|
console.log(
|
|
"[Util Out Job]Uh oh, this job doesn't fit in a bucket!",
|
|
job
|
|
);
|
|
}
|
|
});
|
|
|
|
return newLoad;
|
|
};
|