Reformat all project files to use the prettier config file.
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -6,106 +6,103 @@ const calculateStatusDuration = require("../utils/calculateStatusDuration");
|
||||
const getLifecycleStatusColor = require("../utils/getLifecycleStatusColor");
|
||||
|
||||
const jobLifecycle = async (req, res) => {
|
||||
// Grab the jobids and statuses from the request body
|
||||
const {
|
||||
jobids,
|
||||
statuses
|
||||
} = req.body;
|
||||
// Grab the jobids and statuses from the request body
|
||||
const { jobids, statuses } = req.body;
|
||||
|
||||
if (!jobids) {
|
||||
return res.status(400).json({
|
||||
error: "Missing jobids"
|
||||
});
|
||||
}
|
||||
|
||||
const jobIDs = _.isArray(jobids) ? jobids : [jobids];
|
||||
const client = req.userGraphQLClient;
|
||||
const resp = await client.request(queries.QUERY_TRANSITIONS_BY_JOBID, {jobids: jobIDs,});
|
||||
|
||||
const transitions = resp.transitions;
|
||||
|
||||
if (!transitions) {
|
||||
return res.status(200).json({
|
||||
jobIDs,
|
||||
transitions: []
|
||||
});
|
||||
}
|
||||
|
||||
const transitionsByJobId = _.groupBy(resp.transitions, 'jobid');
|
||||
|
||||
const groupedTransitions = {};
|
||||
const allDurations = [];
|
||||
|
||||
for (let jobId in transitionsByJobId) {
|
||||
let lifecycle = transitionsByJobId[jobId].map(transition => {
|
||||
transition.start_readable = transition.start ? moment(transition.start).fromNow() : 'N/A';
|
||||
transition.end_readable = transition.end ? moment(transition.end).fromNow() : 'N/A';
|
||||
|
||||
if (transition.duration) {
|
||||
transition.duration_seconds = Math.round(transition.duration / 1000);
|
||||
transition.duration_minutes = Math.round(transition.duration_seconds / 60);
|
||||
let duration = moment.duration(transition.duration);
|
||||
transition.duration_readable = durationToHumanReadable(duration);
|
||||
} else {
|
||||
transition.duration_seconds = 0;
|
||||
transition.duration_minutes = 0;
|
||||
transition.duration_readable = 'N/A';
|
||||
}
|
||||
return transition;
|
||||
});
|
||||
|
||||
const durations = calculateStatusDuration(lifecycle, statuses);
|
||||
|
||||
groupedTransitions[jobId] = {
|
||||
lifecycle,
|
||||
durations
|
||||
};
|
||||
|
||||
if (durations?.summations) {
|
||||
allDurations.push(durations.summations);
|
||||
}
|
||||
}
|
||||
|
||||
const finalSummations = [];
|
||||
const flatGroupedAllDurations = _.groupBy(allDurations.flat(),'status');
|
||||
|
||||
const finalStatusCounts = Object.keys(flatGroupedAllDurations).reduce((acc, status) => {
|
||||
acc[status] = flatGroupedAllDurations[status].length;
|
||||
return acc;
|
||||
}, {});
|
||||
// Calculate total value of all statuses
|
||||
const finalTotal = Object.values(flatGroupedAllDurations).reduce((total, statusArr) => {
|
||||
return total + statusArr.reduce((acc, curr) => acc + curr.value, 0);
|
||||
}, 0);
|
||||
|
||||
Object.keys(flatGroupedAllDurations).forEach(status => {
|
||||
const value = flatGroupedAllDurations[status].reduce((acc, curr) => acc + curr.value, 0);
|
||||
const humanReadable = durationToHumanReadable(moment.duration(value));
|
||||
const percentage = (value / finalTotal) * 100;
|
||||
const color = getLifecycleStatusColor(status);
|
||||
const roundedPercentage = `${Math.round(percentage)}%`;
|
||||
finalSummations.push({
|
||||
status,
|
||||
value,
|
||||
humanReadable,
|
||||
percentage,
|
||||
color,
|
||||
roundedPercentage
|
||||
});
|
||||
if (!jobids) {
|
||||
return res.status(400).json({
|
||||
error: "Missing jobids"
|
||||
});
|
||||
}
|
||||
|
||||
const jobIDs = _.isArray(jobids) ? jobids : [jobids];
|
||||
const client = req.userGraphQLClient;
|
||||
const resp = await client.request(queries.QUERY_TRANSITIONS_BY_JOBID, { jobids: jobIDs });
|
||||
|
||||
const transitions = resp.transitions;
|
||||
|
||||
if (!transitions) {
|
||||
return res.status(200).json({
|
||||
jobIDs,
|
||||
transition: groupedTransitions,
|
||||
durations: {
|
||||
jobs: jobIDs.length,
|
||||
summations: finalSummations,
|
||||
totalStatuses: finalSummations.length,
|
||||
total: finalTotal,
|
||||
statusCounts: finalStatusCounts,
|
||||
humanReadable: durationToHumanReadable(moment.duration(finalTotal))
|
||||
}
|
||||
jobIDs,
|
||||
transitions: []
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = jobLifecycle;
|
||||
const transitionsByJobId = _.groupBy(resp.transitions, "jobid");
|
||||
|
||||
const groupedTransitions = {};
|
||||
const allDurations = [];
|
||||
|
||||
for (let jobId in transitionsByJobId) {
|
||||
let lifecycle = transitionsByJobId[jobId].map((transition) => {
|
||||
transition.start_readable = transition.start ? moment(transition.start).fromNow() : "N/A";
|
||||
transition.end_readable = transition.end ? moment(transition.end).fromNow() : "N/A";
|
||||
|
||||
if (transition.duration) {
|
||||
transition.duration_seconds = Math.round(transition.duration / 1000);
|
||||
transition.duration_minutes = Math.round(transition.duration_seconds / 60);
|
||||
let duration = moment.duration(transition.duration);
|
||||
transition.duration_readable = durationToHumanReadable(duration);
|
||||
} else {
|
||||
transition.duration_seconds = 0;
|
||||
transition.duration_minutes = 0;
|
||||
transition.duration_readable = "N/A";
|
||||
}
|
||||
return transition;
|
||||
});
|
||||
|
||||
const durations = calculateStatusDuration(lifecycle, statuses);
|
||||
|
||||
groupedTransitions[jobId] = {
|
||||
lifecycle,
|
||||
durations
|
||||
};
|
||||
|
||||
if (durations?.summations) {
|
||||
allDurations.push(durations.summations);
|
||||
}
|
||||
}
|
||||
|
||||
const finalSummations = [];
|
||||
const flatGroupedAllDurations = _.groupBy(allDurations.flat(), "status");
|
||||
|
||||
const finalStatusCounts = Object.keys(flatGroupedAllDurations).reduce((acc, status) => {
|
||||
acc[status] = flatGroupedAllDurations[status].length;
|
||||
return acc;
|
||||
}, {});
|
||||
// Calculate total value of all statuses
|
||||
const finalTotal = Object.values(flatGroupedAllDurations).reduce((total, statusArr) => {
|
||||
return total + statusArr.reduce((acc, curr) => acc + curr.value, 0);
|
||||
}, 0);
|
||||
|
||||
Object.keys(flatGroupedAllDurations).forEach((status) => {
|
||||
const value = flatGroupedAllDurations[status].reduce((acc, curr) => acc + curr.value, 0);
|
||||
const humanReadable = durationToHumanReadable(moment.duration(value));
|
||||
const percentage = (value / finalTotal) * 100;
|
||||
const color = getLifecycleStatusColor(status);
|
||||
const roundedPercentage = `${Math.round(percentage)}%`;
|
||||
finalSummations.push({
|
||||
status,
|
||||
value,
|
||||
humanReadable,
|
||||
percentage,
|
||||
color,
|
||||
roundedPercentage
|
||||
});
|
||||
});
|
||||
|
||||
return res.status(200).json({
|
||||
jobIDs,
|
||||
transition: groupedTransitions,
|
||||
durations: {
|
||||
jobs: jobIDs.length,
|
||||
summations: finalSummations,
|
||||
totalStatuses: finalSummations.length,
|
||||
total: finalTotal,
|
||||
statusCounts: finalStatusCounts,
|
||||
humanReadable: durationToHumanReadable(moment.duration(finalTotal))
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = jobLifecycle;
|
||||
|
||||
@@ -11,91 +11,68 @@ const path = require("path");
|
||||
const client = require("../graphql-client/graphql-client").client;
|
||||
|
||||
require("dotenv").config({
|
||||
path: path.resolve(
|
||||
process.cwd(),
|
||||
`.env.${process.env.NODE_ENV || "development"}`
|
||||
),
|
||||
path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`)
|
||||
});
|
||||
|
||||
async function StatusTransition(req, res) {
|
||||
const {
|
||||
id: jobid,
|
||||
status: value,
|
||||
shopid: bodyshopid,
|
||||
} = req.body.event.data.new;
|
||||
const { id: jobid, status: value, shopid: bodyshopid } = req.body.event.data.new;
|
||||
|
||||
// Create record OPEN on new item, enter state
|
||||
// If change to SCHEDULE, update the last record and create a new record (update status and end time on old record, create a new record saying we came from previous status going to previous status
|
||||
// (Timeline)
|
||||
// Final status is exported, there is no end date as there is no further transition (has no end date)
|
||||
try {
|
||||
const {update_transitions} = await client.request(
|
||||
queries.UPDATE_OLD_TRANSITION,
|
||||
{
|
||||
jobid: jobid,
|
||||
existingTransition: {
|
||||
end: new Date(),
|
||||
next_value: value,
|
||||
// Create record OPEN on new item, enter state
|
||||
// If change to SCHEDULE, update the last record and create a new record (update status and end time on old record, create a new record saying we came from previous status going to previous status
|
||||
// (Timeline)
|
||||
// Final status is exported, there is no end date as there is no further transition (has no end date)
|
||||
try {
|
||||
const { update_transitions } = await client.request(queries.UPDATE_OLD_TRANSITION, {
|
||||
jobid: jobid,
|
||||
existingTransition: {
|
||||
end: new Date(),
|
||||
next_value: value
|
||||
|
||||
//duration
|
||||
},
|
||||
}
|
||||
);
|
||||
//duration
|
||||
}
|
||||
});
|
||||
|
||||
let duration =
|
||||
update_transitions.affected_rows === 0
|
||||
? 0
|
||||
: new Date(update_transitions.returning[0].end) -
|
||||
new Date(update_transitions.returning[0].start);
|
||||
let duration =
|
||||
update_transitions.affected_rows === 0
|
||||
? 0
|
||||
: new Date(update_transitions.returning[0].end) - new Date(update_transitions.returning[0].start);
|
||||
|
||||
const resp2 = await client.request(
|
||||
queries.INSERT_NEW_TRANSITION(update_transitions.affected_rows > 0),
|
||||
{
|
||||
...(update_transitions.affected_rows > 0
|
||||
? {
|
||||
oldTransitionId:
|
||||
update_transitions.affected_rows === 0
|
||||
? null
|
||||
: update_transitions.returning[0].id,
|
||||
duration,
|
||||
}
|
||||
: {}),
|
||||
newTransition: {
|
||||
bodyshopid: bodyshopid,
|
||||
jobid: jobid,
|
||||
start:
|
||||
update_transitions.affected_rows === 0
|
||||
? new Date()
|
||||
: update_transitions.returning[0].end,
|
||||
prev_value:
|
||||
update_transitions.affected_rows === 0
|
||||
? null
|
||||
: update_transitions.returning[0].value,
|
||||
value: value,
|
||||
type: "status",
|
||||
},
|
||||
}
|
||||
);
|
||||
const resp2 = await client.request(queries.INSERT_NEW_TRANSITION(update_transitions.affected_rows > 0), {
|
||||
...(update_transitions.affected_rows > 0
|
||||
? {
|
||||
oldTransitionId: update_transitions.affected_rows === 0 ? null : update_transitions.returning[0].id,
|
||||
duration
|
||||
}
|
||||
: {}),
|
||||
newTransition: {
|
||||
bodyshopid: bodyshopid,
|
||||
jobid: jobid,
|
||||
start: update_transitions.affected_rows === 0 ? new Date() : update_transitions.returning[0].end,
|
||||
prev_value: update_transitions.affected_rows === 0 ? null : update_transitions.returning[0].value,
|
||||
value: value,
|
||||
type: "status"
|
||||
}
|
||||
});
|
||||
|
||||
logger.log("job-transition-update-result", "DEBUG", null, jobid, resp2);
|
||||
logger.log("job-transition-update-result", "DEBUG", null, jobid, resp2);
|
||||
|
||||
//Check to see if there is an existing status transition record.
|
||||
//Query using Job ID, start is not null, end is null.
|
||||
//Check to see if there is an existing status transition record.
|
||||
//Query using Job ID, start is not null, end is null.
|
||||
|
||||
//If there is no existing record, this is the start of the transition life cycle.
|
||||
// Create the initial transition record.
|
||||
//If there is no existing record, this is the start of the transition life cycle.
|
||||
// Create the initial transition record.
|
||||
|
||||
//If there is a current status transition record, update it with the end date, duration, and next value.
|
||||
//If there is a current status transition record, update it with the end date, duration, and next value.
|
||||
|
||||
res.sendStatus(200); //.json(ret);
|
||||
} catch (error) {
|
||||
logger.log("job-status-transition-error", "ERROR", req.user?.email, jobid, {
|
||||
message: error.message,
|
||||
stack: error.stack,
|
||||
});
|
||||
res.sendStatus(200); //.json(ret);
|
||||
} catch (error) {
|
||||
logger.log("job-status-transition-error", "ERROR", req.user?.email, jobid, {
|
||||
message: error.message,
|
||||
stack: error.stack
|
||||
});
|
||||
|
||||
res.status(400).send(JSON.stringify(error));
|
||||
}
|
||||
res.status(400).send(JSON.stringify(error));
|
||||
}
|
||||
}
|
||||
|
||||
exports.statustransition = StatusTransition;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,16 +1,16 @@
|
||||
const RenderInstanceManager = require('../utils/instanceMgr').default;
|
||||
const RenderInstanceManager = require("../utils/instanceMgr").default;
|
||||
|
||||
exports.totals = RenderInstanceManager({
|
||||
imex: require('./job-totals').default,
|
||||
rome: require('./job-totals-USA').default,
|
||||
promanager: require('./job-totals-USA').default,
|
||||
imex: require("./job-totals").default,
|
||||
rome: require("./job-totals-USA").default,
|
||||
promanager: require("./job-totals-USA").default
|
||||
});
|
||||
exports.totalsSsu = RenderInstanceManager({
|
||||
imex: require('./job-totals').totalsSsu,
|
||||
rome: require('./job-totals-USA').totalsSsu,
|
||||
promanager: require('./job-totals-USA').totalsSsu,
|
||||
imex: require("./job-totals").totalsSsu,
|
||||
rome: require("./job-totals-USA").totalsSsu,
|
||||
promanager: require("./job-totals-USA").totalsSsu
|
||||
});
|
||||
exports.costing = require('./job-costing').JobCosting;
|
||||
exports.costingmulti = require('./job-costing').JobCostingMulti;
|
||||
exports.statustransition = require('./job-status-transition').statustransition;
|
||||
exports.lifecycle = require('./job-lifecycle');
|
||||
exports.costing = require("./job-costing").JobCosting;
|
||||
exports.costingmulti = require("./job-costing").JobCostingMulti;
|
||||
exports.statustransition = require("./job-status-transition").statustransition;
|
||||
exports.lifecycle = require("./job-lifecycle");
|
||||
|
||||
Reference in New Issue
Block a user