83 lines
2.2 KiB
JavaScript
83 lines
2.2 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");
|
|
|
|
require("dotenv").config({
|
|
path: path.resolve(
|
|
process.cwd(),
|
|
`.env.${process.env.NODE_ENV || "development"}`
|
|
),
|
|
});
|
|
|
|
exports.job = async (req, res) => {
|
|
try {
|
|
const BearerToken = req.headers.authorization;
|
|
const { jobId } = req.body;
|
|
|
|
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {
|
|
headers: {
|
|
Authorization: BearerToken,
|
|
},
|
|
});
|
|
|
|
const result = await client
|
|
.setHeaders({ Authorization: BearerToken })
|
|
.request(queries.QUERY_UPCOMING_APPOINTMENTS, {
|
|
now: new Date(),
|
|
jobId: jobId,
|
|
});
|
|
|
|
const { appointments } = result;
|
|
const { ssbuckets } = result.jobs_by_pk.bodyshop;
|
|
const jobhrs = result.jobs_by_pk.jobhrs.aggregate.sum.mod_lb_hrs;
|
|
|
|
const JobClassification = ssbuckets.filter(
|
|
(b) => b.gte <= jobhrs && b.lt > jobhrs
|
|
);
|
|
//Create a matrix of load bucket
|
|
|
|
const bucketMatrix = {};
|
|
|
|
//Get latest date + add 5 days.
|
|
|
|
const totalMatrixDays = moment
|
|
.max(appointments.map((a) => moment(a.start)))
|
|
.add("5", "days")
|
|
.diff(moment(), "days");
|
|
|
|
for (var i = 0; i++; i < totalMatrixDays) {
|
|
const theDate = moment().add(i, "days").toISOString().substr(0, 10);
|
|
console.log("theDate", theDate);
|
|
ssbuckets.forEach((bucket) => {
|
|
bucketMatrix[theDate][bucket.id] = { in: 0, out: 0 };
|
|
});
|
|
}
|
|
|
|
appointments.forEach((appointment) => {
|
|
//Get the day of the appointment.
|
|
const appDate = moment(appointment.start).toISOString().substr(0, 10);
|
|
!!bucketMatrix[appDate] ? {} : {};
|
|
});
|
|
|
|
//Calculate the load for the shop looking forward.
|
|
|
|
const possibleDates = [];
|
|
|
|
//Temp
|
|
possibleDates.push(new Date());
|
|
possibleDates.push(new Date());
|
|
possibleDates.push(new Date());
|
|
possibleDates.push(new Date());
|
|
possibleDates.push(new Date());
|
|
//Get a list of upcoming appointments
|
|
//Get the config for each day
|
|
|
|
res.json(possibleDates);
|
|
} catch (error) {
|
|
console.log("error", error);
|
|
res.status(400).send(error);
|
|
}
|
|
};
|