85 lines
2.6 KiB
JavaScript
85 lines
2.6 KiB
JavaScript
const Dinero = require("dinero.js");
|
|
const queries = require("../graphql-client/queries");
|
|
//const client = require("../graphql-client/graphql-client").client;
|
|
const _ = require("lodash");
|
|
const GraphQLClient = require("graphql-request").GraphQLClient;
|
|
const logger = require("../utils/logger");
|
|
// Dinero.defaultCurrency = "USD";
|
|
// Dinero.globalLocale = "en-CA";
|
|
Dinero.globalRoundingMode = "HALF_EVEN";
|
|
|
|
async function StatusTransition(req, res) {
|
|
const { jobid, value, bodyshopid } = 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: {
|
|
Authorization: BearerToken,
|
|
},
|
|
});
|
|
|
|
try {
|
|
const { update_transitions } = await client
|
|
.setHeaders({ Authorization: BearerToken })
|
|
.request(queries.UPDATE_OLD_TRANSITION, {
|
|
jobid: jobid,
|
|
existingTransition: {
|
|
end: new Date(),
|
|
next_value: value,
|
|
|
|
//duration
|
|
},
|
|
});
|
|
|
|
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
|
|
.setHeaders({ Authorization: BearerToken })
|
|
.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.
|
|
|
|
//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.
|
|
|
|
res.sendStatus(200); //.json(ret);
|
|
} catch (error) {
|
|
logger.log("job-costing-error", "ERROR", req.user.email, jobid, {
|
|
message: error.message,
|
|
stack: error.stack,
|
|
});
|
|
|
|
res.status(400).send(JSON.stringify(error));
|
|
}
|
|
}
|
|
|
|
exports.statustransition = StatusTransition;
|