File diff suppressed because it is too large
Load Diff
68
server/job/job-lifecycle.js
Normal file
68
server/job/job-lifecycle.js
Normal file
@@ -0,0 +1,68 @@
|
||||
const _ = require("lodash");
|
||||
const queries = require("../graphql-client/queries");
|
||||
const moment = require("moment");
|
||||
const durationToHumanReadable = require("../utils/durationToHumanReadable");
|
||||
const calculateStatusDuration = require("../utils/calculateStatusDuration");
|
||||
|
||||
const jobLifecycle = async (req, res) => {
|
||||
// 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 = {};
|
||||
|
||||
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;
|
||||
});
|
||||
|
||||
groupedTransitions[jobId] = {
|
||||
lifecycle: lifecycle,
|
||||
durations: calculateStatusDuration(lifecycle, statuses),
|
||||
};
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
jobIDs,
|
||||
transition: groupedTransitions,
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = jobLifecycle;
|
||||
@@ -9,83 +9,84 @@ const logger = require("../utils/logger");
|
||||
Dinero.globalRoundingMode = "HALF_EVEN";
|
||||
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) {
|
||||
if (req.headers["event-secret"] !== process.env.EVENT_SECRET) {
|
||||
res.status(401).send("Unauthorized");
|
||||
return;
|
||||
}
|
||||
res.sendStatus(200);
|
||||
return;
|
||||
const {
|
||||
id: jobid,
|
||||
status: value,
|
||||
shopid: bodyshopid,
|
||||
} = req.body.event.data.new;
|
||||
try {
|
||||
const { update_transitions } = await client.request(
|
||||
queries.UPDATE_OLD_TRANSITION,
|
||||
{
|
||||
jobid: jobid,
|
||||
existingTransition: {
|
||||
end: new Date(),
|
||||
next_value: value,
|
||||
const {
|
||||
id: jobid,
|
||||
status: value,
|
||||
shopid: bodyshopid,
|
||||
} = req.body.event.data.new;
|
||||
|
||||
//duration
|
||||
},
|
||||
}
|
||||
);
|
||||
// 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,
|
||||
|
||||
let duration =
|
||||
update_transitions.affected_rows === 0
|
||||
? 0
|
||||
: new Date(update_transitions.returning[0].end) -
|
||||
new Date(update_transitions.returning[0].start);
|
||||
//duration
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const resp2 = await client.request(queries.INSERT_NEW_TRANSITION, {
|
||||
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",
|
||||
},
|
||||
});
|
||||
let duration =
|
||||
update_transitions.affected_rows === 0
|
||||
? 0
|
||||
: new Date(update_transitions.returning[0].end) -
|
||||
new Date(update_transitions.returning[0].start);
|
||||
|
||||
//Check to see if there is an existing status transition record.
|
||||
//Query using Job ID, start is not null, end is null.
|
||||
const resp2 = await client.request(queries.INSERT_NEW_TRANSITION, {
|
||||
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",
|
||||
},
|
||||
});
|
||||
|
||||
//If there is no existing record, this is the start of the transition life cycle.
|
||||
// Create the initial transition record.
|
||||
//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 a current status transition record, update it with the end date, duration, and next value.
|
||||
//If there is no existing record, this is the start of the transition life cycle.
|
||||
// Create the initial transition record.
|
||||
|
||||
res.sendStatus(200); //.json(ret);
|
||||
} catch (error) {
|
||||
logger.log("job-status-transition-error", "ERROR", req.user?.email, jobid, {
|
||||
message: error.message,
|
||||
stack: error.stack,
|
||||
});
|
||||
//If there is a current status transition record, update it with the end date, duration, and next value.
|
||||
|
||||
res.status(400).send(JSON.stringify(error));
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
exports.statustransition = StatusTransition;
|
||||
|
||||
@@ -1,22 +1,20 @@
|
||||
const Dinero = require("dinero.js");
|
||||
const queries = require("../graphql-client/queries");
|
||||
const GraphQLClient = require("graphql-request").GraphQLClient;
|
||||
const adminClient = require("../graphql-client/graphql-client").client;
|
||||
const _ = require("lodash");
|
||||
const logger = require("../utils/logger");
|
||||
|
||||
// Dinero.defaultCurrency = "USD";
|
||||
// Dinero.globalLocale = "en-CA";
|
||||
Dinero.globalRoundingMode = "HALF_EVEN";
|
||||
|
||||
exports.totalsSsu = async function (req, res) {
|
||||
const BearerToken = req.headers.authorization;
|
||||
const { id } = req.body;
|
||||
|
||||
const BearerToken = req.BearerToken;
|
||||
const client = req.userGraphQLClient;
|
||||
|
||||
logger.log("job-totals-ssu", "DEBUG", req.user.email, id, null);
|
||||
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {
|
||||
headers: {
|
||||
Authorization: BearerToken,
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
const job = await client
|
||||
@@ -77,21 +75,19 @@ async function TotalsServerSide(req, res) {
|
||||
}
|
||||
|
||||
async function Totals(req, res) {
|
||||
const { job } = req.body;
|
||||
const { job, id } = req.body;
|
||||
|
||||
const logger = req.logger;
|
||||
const client = req.userGraphQLClient;
|
||||
|
||||
logger.log("job-totals", "DEBUG", req.user.email, job.id, {
|
||||
jobid: job.id,
|
||||
});
|
||||
|
||||
const BearerToken = req.headers.authorization;
|
||||
const { id } = req.body;
|
||||
logger.log("job-totals-ssu", "DEBUG", req.user.email, id, null);
|
||||
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {
|
||||
headers: {
|
||||
Authorization: BearerToken,
|
||||
},
|
||||
});
|
||||
|
||||
await AutoAddAtsIfRequired({ job, client });
|
||||
|
||||
try {
|
||||
let ret = {
|
||||
rates: await CalculateRatesTotals({ job, client }),
|
||||
|
||||
@@ -3,3 +3,4 @@ exports.totalsSsu = require("./job-totals").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');
|
||||
Reference in New Issue
Block a user