refactor(materials): added in if staged just pass the check

This commit is contained in:
2025-10-28 19:40:29 -05:00
parent e9ff8bb4cb
commit 147d189a81

View File

@@ -6,289 +6,273 @@ import { machineCheck } from "../../../sqlServer/querys/ocp/machineId.js";
import { mmQuery } from "../../../sqlServer/querys/ocp/mainMaterial.js"; import { mmQuery } from "../../../sqlServer/querys/ocp/mainMaterial.js";
export const isMainMatStaged = async (lot: any) => { export const isMainMatStaged = async (lot: any) => {
const set = serverSettings.length === 0 ? [] : serverSettings; const set = serverSettings.length === 0 ? [] : serverSettings;
// make staged false by deefault and error logged if theres an issue // make staged false by deefault and error logged if theres an issue
let isStaged = { message: "Material is staged", success: true }; let isStaged = { message: "Material is staged", success: true };
const { data, error } = (await tryCatch( const { data, error } = (await tryCatch(
query( query(
machineCheck.replace("where Active = 1 and [Location] = [loc]", ""), machineCheck.replace("where Active = 1 and [Location] = [loc]", ""),
"check machine needs mm" "check machine needs mm",
) ),
)) as any; )) as any;
const machine = data.data.filter( const machine = data.data.filter(
(m: any) => m.HumanReadableId === lot.machineID (m: any) => m.HumanReadableId === lot.machineID,
); );
// we have a check on ksc side to ignore the tetra machine for now as its not updating in 2.0 // we have a check on ksc side to ignore the tetra machine for now as its not updating in 2.0
if (!machine[0].StagingMainMaterialMandatory) { if (!machine[0].StagingMainMaterialMandatory) {
createLog( createLog(
"info", "info",
"mainMaterial", "mainMaterial",
"ocp", "ocp",
`The machine dose not require mm to print and book in.` `The machine dose not require mm to print and book in.`,
); );
return { return {
message: "Machine dose not require material to be staged", message: "Machine dose not require material to be staged",
success: true, success: true,
}; };
} }
// strangly the lot is not always sending over in slc so adding this in for now to see what line is cauing this issue // strangly the lot is not always sending over in slc so adding this in for now to see what line is cauing this issue
if (!lot) { if (!lot) {
createLog( createLog("info", "mainMaterial", "ocp", "No lot was passed correctly.");
"info", return isStaged;
"mainMaterial", }
"ocp",
"No lot was passed correctly."
);
return isStaged;
}
if (typeof lot !== "object" || lot === null || Array.isArray(lot)) { if (typeof lot !== "object" || lot === null || Array.isArray(lot)) {
createLog( createLog(
"info", "info",
"mainMaterial", "mainMaterial",
"ocp", "ocp",
`The lot sent over is not an object: ${JSON.stringify(lot)}` `The lot sent over is not an object: ${JSON.stringify(lot)}`,
); );
return isStaged; return isStaged;
} }
const updateQuery = mmQuery.replaceAll("[lotNumber]", lot.lot); const updateQuery = mmQuery.replaceAll("[lotNumber]", lot.lot);
try { try {
const r: any = await query(updateQuery, "Main Material Check"); const r: any = await query(updateQuery, "Main Material Check");
const res: any = r.data; const res: any = r.data;
// if (res[0].Staged >= 1) { // if (res[0].Staged >= 1) {
// isStaged = true; // isStaged = true;
// } // }
type CheckConditionArgs = { type CheckConditionArgs = {
results: any[]; results: any[];
filterFn: (n: any) => boolean; filterFn: (n: any) => boolean;
failCondition: (n: any) => boolean; failCondition: (n: any) => boolean;
failMessage: string; failMessage: string;
successMessage: string; successMessage: string;
lot: { lot: string | number }; lot: { lot: string | number };
}; };
const checkCondition = ({ const checkCondition = ({
results, results,
filterFn, filterFn,
failCondition, failCondition,
failMessage, failMessage,
successMessage, successMessage,
lot, lot,
}: CheckConditionArgs): { message: string; success: boolean } => { }: CheckConditionArgs): { message: string; success: boolean } => {
const subset = results.filter(filterFn); const subset = results.filter(filterFn);
if (subset.some(failCondition)) { if (subset.some(failCondition)) {
const failing = subset.filter(failCondition); const failing = subset.filter(failCondition);
createLog( createLog(
"info", "info",
"mainMaterial", "mainMaterial",
"ocp", "ocp",
`lot: ${lot.lot}, is missing: ${failing `lot: ${lot.lot}, is missing: ${failing
.map( .map(
(o: any) => (o: any) =>
`${o.MaterialHumanReadableId} - ${o.MaterialDescription}` `${o.MaterialHumanReadableId} - ${o.MaterialDescription}`,
) )
.join(",\n ")} ${failMessage}` .join(",\n ")} ${failMessage}`,
); );
return { return {
message: `lot: ${lot.lot}, is missing: ${failing message: `lot: ${lot.lot}, is missing: ${failing
.map( .map(
(o: any) => (o: any) =>
`${o.MaterialHumanReadableId} - ${o.MaterialDescription}` `${o.MaterialHumanReadableId} - ${o.MaterialDescription}`,
) )
.join(",\n ")} ${failMessage}`, .join(",\n ")} ${failMessage}`,
success: false, success: false,
}; };
} else { } else {
createLog( createLog(
"info", "info",
"mainMaterial", "mainMaterial",
"ocp", "ocp",
`lot: ${lot.lot}, ${JSON.stringify(results)}` `lot: ${lot.lot}, ${JSON.stringify(results)}`,
); );
return { message: successMessage, success: true }; return { message: successMessage, success: true };
} }
}; };
createLog("info", "mainMaterial", "ocp", `Maint material query ran.`); createLog("info", "mainMaterial", "ocp", `Maint material query ran.`);
const mainMaterial = res.find((n: any) => n.IsMainMaterial); const mainMaterial = res.find((n: any) => n.IsMainMaterial);
if (mainMaterial?.noMMShortage === "noMM") { if (mainMaterial?.Staged === 1) {
createLog( createLog(
"info", "info",
"mainMaterial", "mainMaterial",
"ocp", "ocp",
`Main material: ${mainMaterial.MaterialHumanReadableId} - ${mainMaterial.MaterialDescription}: is not staged for ${lot.lot}` `Main material: ${mainMaterial.MaterialHumanReadableId} - ${mainMaterial.MaterialDescription}: is staged for ${lot.lot}`,
); );
return { return {
message: `Main material: ${mainMaterial.MaterialHumanReadableId} - ${mainMaterial.MaterialDescription}: is not staged for ${lot.lot}`, message: `Main material: ${mainMaterial.MaterialHumanReadableId} - ${mainMaterial.MaterialDescription}: is staged for ${lot.lot}`,
success: false, success: true,
}; };
} }
if (mainMaterial?.noMMShortage === "noMM") {
createLog(
"info",
"mainMaterial",
"ocp",
`Main material: ${mainMaterial.MaterialHumanReadableId} - ${mainMaterial.MaterialDescription}: is not staged for ${lot.lot}`,
);
return {
message: `Main material: ${mainMaterial.MaterialHumanReadableId} - ${mainMaterial.MaterialDescription}: is not staged for ${lot.lot}`,
success: false,
};
}
// we need to filter the color stuff and then look for includes instead of a standard name. this way we can capture a everything and not a single type // we need to filter the color stuff and then look for includes instead of a standard name. this way we can capture a everything and not a single type
// for manual consume color if active to check colors // for manual consume color if active to check colors
const checkColorSetting = set.filter((n) => n.name === "checkColor"); const checkColorSetting = set.filter((n) => n.name === "checkColor");
// 2. Auto color // 2. Auto color
if (checkColorSetting[0].value === "1") { if (checkColorSetting[0].value === "1") {
// auto check // auto check
// 2. Auto color // 2. Auto color
const autoColor = checkCondition({ const autoColor = checkCondition({
results: res, results: res,
lot, lot,
filterFn: (n) => filterFn: (n) =>
n.isManual && n.isManual &&
!("noPKGAutoShortage" in n) && !("noPKGAutoShortage" in n) &&
!("noPKGManualShortage" in n), // pool = non-main, auto !("noPKGManualShortage" in n), // pool = non-main, auto
failCondition: (n) => n.autoConsumeCheck === "autoConsumeNOK", // column = autoConsumeCheck failCondition: (n) => n.autoConsumeCheck === "autoConsumeNOK", // column = autoConsumeCheck
failMessage: "for autoconsume", failMessage: "for autoconsume",
successMessage: "auto color is good", successMessage: "auto color is good",
}); });
if (!autoColor.success) return autoColor; if (!autoColor.success) return autoColor;
createLog( createLog(
"info", "info",
"mainMaterial", "mainMaterial",
"ocp", "ocp",
`Auto Color: ${JSON.stringify(autoColor)}` `Auto Color: ${JSON.stringify(autoColor)}`,
); );
// 3. Manual color // 3. Manual color
const manualColor = checkCondition({ const manualColor = checkCondition({
results: res, results: res,
lot, lot,
filterFn: (n) => filterFn: (n) =>
!n.IsMainMaterial && !n.IsMainMaterial &&
n.isManual && n.isManual &&
!("noPKGAutoShortage" in n) && !("noPKGAutoShortage" in n) &&
!("noPKGManualShortage" in n), // pool = non-main, manual !("noPKGManualShortage" in n), // pool = non-main, manual
failCondition: (n) => n.noManualShortage === "noOK", // column = noManualShortage failCondition: (n) => n.noManualShortage === "noOK", // column = noManualShortage
failMessage: "for manual material", failMessage: "for manual material",
successMessage: "manual color is good", successMessage: "manual color is good",
}); });
if (!manualColor.success) return manualColor; if (!manualColor.success) return manualColor;
createLog( createLog(
"info", "info",
"mainMaterial", "mainMaterial",
"ocp", "ocp",
`Manual Color: ${JSON.stringify(manualColor)}` `Manual Color: ${JSON.stringify(manualColor)}`,
); );
} else { } else {
createLog( createLog("info", "mainMaterial", "ocp", "Color check is not active.");
"info", }
"mainMaterial",
"ocp",
"Color check is not active."
);
}
// // if we want to check the packaging // // if we want to check the packaging
const checkPKGSetting = set.filter((n) => n.name === "checkPKG"); const checkPKGSetting = set.filter((n) => n.name === "checkPKG");
if (checkPKGSetting[0].value === "1") { if (checkPKGSetting[0].value === "1") {
const pkgAuto = checkCondition({ const pkgAuto = checkCondition({
results: res, results: res,
lot, lot,
filterFn: (n) => filterFn: (n) =>
!n.IsMainMaterial && !n.IsMainMaterial && !n.isManual && "noPKGAutoShortage" in n,
!n.isManual && failCondition: (n) => n.noPKGAutoShortage === "noAutoPkg",
"noPKGAutoShortage" in n, failMessage: "for pkg",
failCondition: (n) => n.noPKGAutoShortage === "noAutoPkg", successMessage: "auto PKG is good",
failMessage: "for pkg", });
successMessage: "auto PKG is good", if (!pkgAuto.success) return pkgAuto;
}); createLog(
if (!pkgAuto.success) return pkgAuto; "info",
createLog( "mainMaterial",
"info", "ocp",
"mainMaterial", `PKG Auto: ${JSON.stringify(pkgAuto)}`,
"ocp", );
`PKG Auto: ${JSON.stringify(pkgAuto)}` // 5. Packaging manual
); const pkgManual = checkCondition({
// 5. Packaging manual results: res,
const pkgManual = checkCondition({ lot,
results: res, filterFn: (n) =>
lot, !n.IsMainMaterial && n.isManual && "noPKGManualShortage" in n,
filterFn: (n) => failCondition: (n) => n.noPKGManualShortage === "noManPkg",
!n.IsMainMaterial && failMessage: "for pkg",
n.isManual && successMessage: "manual PKG is good",
"noPKGManualShortage" in n, });
failCondition: (n) => n.noPKGManualShortage === "noManPkg",
failMessage: "for pkg",
successMessage: "manual PKG is good",
});
if (!pkgManual.success) return pkgManual; if (!pkgManual.success) return pkgManual;
createLog( createLog(
"info", "info",
"mainMaterial", "mainMaterial",
"ocp", "ocp",
`PKG Manual: ${JSON.stringify(pkgManual)}` `PKG Manual: ${JSON.stringify(pkgManual)}`,
); );
} else { } else {
createLog( createLog("info", "mainMaterial", "ocp", "PKG check is not active.");
"info", }
"mainMaterial",
"ocp",
"PKG check is not active."
);
}
// manual pkg // manual pkg
if (checkPKGSetting[0].value === "1") { if (checkPKGSetting[0].value === "1") {
const packagingCheck = res.filter( const packagingCheck = res.filter(
(n: any) => (n: any) =>
!n.IsMainMaterial && !n.IsMainMaterial && n.isManual && "noPKGManualShortage" in n,
n.isManual && );
"noPKGManualShortage" in n if (
); packagingCheck.some((n: any) => n.noPKGManualShortage === "noManPkg")
if ( ) {
packagingCheck.some( createLog(
(n: any) => n.noPKGManualShortage === "noManPkg" "info",
) "mainMaterial",
) { "ocp",
createLog( `lot: ${lot.lot}, is missing: ${packagingCheck
"info", .map(
"mainMaterial", (o: any) =>
"ocp", `${o.MaterialHumanReadableId} - ${o.MaterialDescription}`,
`lot: ${lot.lot}, is missing: ${packagingCheck )
.map( .join(",\n ")} for pkg`,
(o: any) => );
`${o.MaterialHumanReadableId} - ${o.MaterialDescription}` return (isStaged = {
) message: `lot: ${lot.lot}, is missing: ${packagingCheck
.join(",\n ")} for pkg` .map(
); (o: any) =>
return (isStaged = { `${o.MaterialHumanReadableId} - ${o.MaterialDescription}`,
message: `lot: ${lot.lot}, is missing: ${packagingCheck )
.map( .join(",\n ")} for pkg`,
(o: any) => success: false,
`${o.MaterialHumanReadableId} - ${o.MaterialDescription}` });
) }
.join(",\n ")} for pkg`, } else {
success: false, createLog("info", "mainMaterial", "ocp", "PKG check is not active.");
}); }
} } catch (err) {
} else { createLog(
createLog( "error",
"info", "mainMaterial",
"mainMaterial", "ocp",
"ocp", `Error from running the Main Material query: ${err}`,
"PKG check is not active." );
); }
} return isStaged;
} catch (err) {
createLog(
"error",
"mainMaterial",
"ocp",
`Error from running the Main Material query: ${err}`
);
}
return isStaged;
}; };