IO-3077 Implement import rules engine.
This commit is contained in:
@@ -2241,6 +2241,7 @@ exports.QUERY_PARTS_SCAN = `query QUERY_PARTS_SCAN ($id: uuid!) {
|
||||
mod_lb_hrs
|
||||
oem_partno
|
||||
alt_partno
|
||||
op_code_desc
|
||||
}
|
||||
}
|
||||
}`;
|
||||
@@ -2252,7 +2253,7 @@ exports.UPDATE_PARTS_CRITICAL = `mutation UPDATE_PARTS_CRITICAL ($IdsToMarkCriti
|
||||
notcritical: update_joblines(where: {id: {_nin: $IdsToMarkCritical}, jobid: {_eq: $jobid}}, _set: {critical: false}) {
|
||||
affected_rows
|
||||
}
|
||||
}`
|
||||
}`;
|
||||
|
||||
exports.ACTIVE_SHOP_BY_USER = `query ACTIVE_SHOP_BY_USER($user: String) {
|
||||
associations(where: {active: {_eq: true}, useremail: {_eq: $user}}) {
|
||||
@@ -2618,7 +2619,6 @@ exports.CREATE_CONVERSATION = `mutation CREATE_CONVERSATION($conversation: [conv
|
||||
}
|
||||
`;
|
||||
|
||||
|
||||
exports.STATUS_UPDATE = `query STATUS_UPDATE($period: timestamptz!, $today: timestamptz!) {
|
||||
bodyshops(where: { created_at: { _gte: $period } }) {
|
||||
shopname
|
||||
@@ -2689,4 +2689,19 @@ exports.STATUS_UPDATE = `query STATUS_UPDATE($period: timestamptz!, $today: time
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
`;
|
||||
|
||||
exports.INSERT_AUDIT_TRAIL = `
|
||||
mutation INSERT_AUDIT_TRAIL($auditObj: audit_trail_insert_input!) {
|
||||
insert_audit_trail_one(object: $auditObj) {
|
||||
id
|
||||
jobid
|
||||
billid
|
||||
bodyshopid
|
||||
created
|
||||
operation
|
||||
type
|
||||
useremail
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
@@ -1,16 +1,25 @@
|
||||
const queries = require("../graphql-client/queries");
|
||||
const logger = require("../utils/logger");
|
||||
|
||||
const predefinedPartTypes = [
|
||||
"PAN", "PAC", "PAR", "PAL", "PAA", "PAM", "PAP", "PAS", "PASL", "PAG",
|
||||
];
|
||||
const predefinedPartTypes = ["PAN", "PAC", "PAR", "PAL", "PAA", "PAM", "PAP", "PAS", "PASL", "PAG"];
|
||||
const predefinedModLbrTypes = [
|
||||
"LAA", "LAB", "LAD", "LAE", "LAF", "LAG", "LAM", "LAR", "LAS", "LAU",
|
||||
"LA1", "LA2", "LA3", "LA4",
|
||||
"LAA",
|
||||
"LAB",
|
||||
"LAD",
|
||||
"LAE",
|
||||
"LAF",
|
||||
"LAG",
|
||||
"LAM",
|
||||
"LAR",
|
||||
"LAS",
|
||||
"LAU",
|
||||
"LA1",
|
||||
"LA2",
|
||||
"LA3",
|
||||
"LA4"
|
||||
];
|
||||
|
||||
exports.partsScan = async function (req, res) {
|
||||
console.log('hello')
|
||||
const { jobid } = req.body;
|
||||
const BearerToken = req.BearerToken;
|
||||
const client = req.userGraphQLClient;
|
||||
@@ -18,10 +27,9 @@ exports.partsScan = async function (req, res) {
|
||||
logger.log("job-parts-scan", "DEBUG", req.user?.email, jobid, null);
|
||||
|
||||
try {
|
||||
const data = await client.setHeaders({ Authorization: BearerToken }).request(
|
||||
queries.QUERY_PARTS_SCAN,
|
||||
{ id: jobid }
|
||||
);
|
||||
const data = await client
|
||||
.setHeaders({ Authorization: BearerToken })
|
||||
.request(queries.QUERY_PARTS_SCAN, { id: jobid });
|
||||
|
||||
const rules = data.jobs_by_pk.bodyshop.md_parts_scan || [];
|
||||
if (!Array.isArray(rules)) {
|
||||
@@ -36,21 +44,22 @@ exports.partsScan = async function (req, res) {
|
||||
? new RegExp(rule.value, "i")
|
||||
: typeof rule.value === "string"
|
||||
? new RegExp(rule.value)
|
||||
: null,
|
||||
: null
|
||||
}));
|
||||
|
||||
const criticalIds = new Set();
|
||||
|
||||
for (const jobline of data.jobs_by_pk.joblines) {
|
||||
for (const { field, regex, operation, value } of compiledRules) {
|
||||
if (criticalIds.has(jobline.id)) break; // Skip further evaluation if already critical
|
||||
for (const { field, regex, operation, value, mark_critical, update_field, update_value } of compiledRules) {
|
||||
// IO-3077 - Remove skip as this is extended to include line updates.
|
||||
// if (criticalIds.has(jobline.id)) break; // Skip further evaluation if already critical
|
||||
|
||||
let jobValue = jobline[field];
|
||||
let match = false;
|
||||
|
||||
if (field === "part_number") {
|
||||
match = regex
|
||||
? regex.test(jobline.oem_partno || '') || regex.test(jobline.alt_partno || '')
|
||||
? regex.test(jobline.oem_partno || "") || regex.test(jobline.alt_partno || "")
|
||||
: jobline.oem_partno === value || jobline.alt_partno === value;
|
||||
} else if (field === "part_type") {
|
||||
match = predefinedPartTypes.includes(value) && value === jobValue;
|
||||
@@ -68,22 +77,44 @@ exports.partsScan = async function (req, res) {
|
||||
}
|
||||
|
||||
if (match) {
|
||||
criticalIds.add(jobline.id);
|
||||
break; // No need to evaluate further rules for this jobline
|
||||
if (mark_critical) {
|
||||
//Could technically lead to duplicates, but they'd only be n positives, ultimately leading a positive.
|
||||
criticalIds.add(jobline.id);
|
||||
}
|
||||
if (update_field && update_value) {
|
||||
const result = await client.setHeaders({ Authorization: BearerToken }).request(queries.UPDATE_JOB_LINE, {
|
||||
lineId: jobline.id,
|
||||
line: { [update_field]: update_value, manual_line: true }
|
||||
});
|
||||
|
||||
const auditResult = await client
|
||||
.setHeaders({ Authorization: BearerToken })
|
||||
.request(queries.INSERT_AUDIT_TRAIL, {
|
||||
auditObj: {
|
||||
bodyshopid: data.jobs_by_pk.bodyshop.id,
|
||||
jobid,
|
||||
operation: `Jobline (${jobline.line_desc}/${jobline.id}) ${update_field} updated from ${jobline[update_field]} to ${update_value}. Lined marked as manual line.`,
|
||||
type: "partscanupdate",
|
||||
useremail: req.user.email
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//break; // No need to evaluate further rules for this jobline
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const result = await client.setHeaders({ Authorization: BearerToken }).request(
|
||||
queries.UPDATE_PARTS_CRITICAL,
|
||||
{ IdsToMarkCritical: Array.from(criticalIds), jobid }
|
||||
);
|
||||
const result = await client
|
||||
.setHeaders({ Authorization: BearerToken })
|
||||
.request(queries.UPDATE_PARTS_CRITICAL, { IdsToMarkCritical: Array.from(criticalIds), jobid });
|
||||
|
||||
res.status(200).json(result);
|
||||
} catch (error) {
|
||||
logger.log("job-parts-scan-error", "ERROR", req.user?.email, jobid, {
|
||||
jobid,
|
||||
error: error.message,
|
||||
stack: error.stack
|
||||
});
|
||||
res.status(400).json(JSON.stringify({ message: error?.message }));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user