Merged in release/2024-01-29 (pull request #1229)

IO-1532 resolve update logic issue for status timings.
This commit is contained in:
Patrick Fic
2024-01-29 17:06:19 +00:00
2 changed files with 82 additions and 65 deletions

View File

@@ -1989,12 +1989,20 @@ exports.UPDATE_OLD_TRANSITION = `mutation UPDATE_OLD_TRANSITION($jobid: uuid!, $
} }
}`; }`;
exports.INSERT_NEW_TRANSITION = `mutation INSERT_NEW_TRANSITION($newTransition: transitions_insert_input!, $oldTransitionId: uuid, $duration: numeric) { exports.INSERT_NEW_TRANSITION = (
includeOldTransition
) => `mutation INSERT_NEW_TRANSITION($newTransition: transitions_insert_input!, ${
includeOldTransition ? `$oldTransitionId: uuid!, $duration: numeric` : ""
}) {
insert_transitions_one(object: $newTransition) { insert_transitions_one(object: $newTransition) {
id id
} }
update_transitions(where: {id: {_eq: $oldTransitionId}}, _set: {duration: $duration}) { ${
includeOldTransition
? `update_transitions(where: {id: {_eq: $oldTransitionId}}, _set: {duration: $duration}) {
affected_rows affected_rows
}`
: ""
} }
}`; }`;

View File

@@ -11,82 +11,91 @@ const path = require("path");
const client = require("../graphql-client/graphql-client").client; const client = require("../graphql-client/graphql-client").client;
require("dotenv").config({ require("dotenv").config({
path: path.resolve( path: path.resolve(
process.cwd(), process.cwd(),
`.env.${process.env.NODE_ENV || "development"}` `.env.${process.env.NODE_ENV || "development"}`
), ),
}); });
async function StatusTransition(req, res) { async function StatusTransition(req, res) {
const { const {
id: jobid, id: jobid,
status: value, status: value,
shopid: bodyshopid, shopid: bodyshopid,
} = req.body.event.data.new; } = req.body.event.data.new;
// Create record OPEN on new item, enter state // 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 // 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) // (Timeline)
// Final status is exported, there is no end date as there is no further transition (has no end date) // Final status is exported, there is no end date as there is no further transition (has no end date)
try { try {
const {update_transitions} = await client.request( const { update_transitions } = await client.request(
queries.UPDATE_OLD_TRANSITION, queries.UPDATE_OLD_TRANSITION,
{ {
jobid: jobid, jobid: jobid,
existingTransition: { existingTransition: {
end: new Date(), end: new Date(),
next_value: value, next_value: value,
//duration //duration
}, },
} }
); );
let duration = let duration =
update_transitions.affected_rows === 0 update_transitions.affected_rows === 0
? 0 ? 0
: new Date(update_transitions.returning[0].end) - : new Date(update_transitions.returning[0].end) -
new Date(update_transitions.returning[0].start); new Date(update_transitions.returning[0].start);
const resp2 = await client.request(queries.INSERT_NEW_TRANSITION, { const resp2 = await client.request(
oldTransitionId: queries.INSERT_NEW_TRANSITION(update_transitions.affected_rows > 0),
{
...(update_transitions.affected_rows > 0
? {
oldTransitionId:
update_transitions.affected_rows === 0 update_transitions.affected_rows === 0
? null ? null
: update_transitions.returning[0].id, : update_transitions.returning[0].id,
duration, duration,
newTransition: { }
bodyshopid: bodyshopid, : {}),
jobid: jobid, newTransition: {
start: bodyshopid: bodyshopid,
update_transitions.affected_rows === 0 jobid: jobid,
? new Date() start:
: update_transitions.returning[0].end, update_transitions.affected_rows === 0
prev_value: ? new Date()
update_transitions.affected_rows === 0 : update_transitions.returning[0].end,
? null prev_value:
: update_transitions.returning[0].value, update_transitions.affected_rows === 0
value: value, ? null
type: "status", : update_transitions.returning[0].value,
}, value: value,
}); type: "status",
},
}
);
//Check to see if there is an existing status transition record. logger.log("job-transition-update-result", "DEBUG", null, jobid, resp2);
//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. //Check to see if there is an existing status transition record.
// Create the initial 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); //If there is a current status transition record, update it with the end date, duration, and next value.
} 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.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; exports.statustransition = StatusTransition;