@@ -13,6 +13,7 @@ async function JobCosting(req, res) {
|
||||
const { jobid } = req.body;
|
||||
|
||||
const BearerToken = req.headers.authorization;
|
||||
|
||||
logger.log("job-costing-start", "DEBUG", req.user.email, jobid, null);
|
||||
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {
|
||||
headers: {
|
||||
|
||||
@@ -1,5 +1,17 @@
|
||||
const _ = require("lodash");
|
||||
const jobLifecycle = (req, res) => {
|
||||
return res.status(200).send("jobLifecycle");
|
||||
const {jobids} = req.body;
|
||||
return _.isArray(jobids) ?
|
||||
handleMultipleJobs(jobids, req, res) :
|
||||
handleSingleJob(jobids, req, res);
|
||||
};
|
||||
|
||||
const handleMultipleJobs = (jobIDs, req, res) => {
|
||||
return res.status(200).send(jobIDs);
|
||||
}
|
||||
|
||||
const handleSingleJob = (req, res) => {
|
||||
return res.status(200).send(req.body);
|
||||
}
|
||||
|
||||
module.exports = jobLifecycle;
|
||||
@@ -9,89 +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;
|
||||
}
|
||||
|
||||
// return res.sendStatus(200);
|
||||
|
||||
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,
|
||||
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, {
|
||||
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, {
|
||||
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",
|
||||
},
|
||||
});
|
||||
|
||||
//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;
|
||||
|
||||
Reference in New Issue
Block a user