refactor(sql): improved the return function to show data [] when not connected, prevents crashes

This commit is contained in:
2025-04-24 21:24:39 -05:00
parent 3573fd1a5b
commit ead63d4b41
36 changed files with 323 additions and 141 deletions

View File

@@ -2,12 +2,20 @@ import { tryCatch } from "../../../../globalUtils/tryCatch.js";
import { query } from "../../../sqlServer/prodSqlServer.js";
import { lotQuery } from "../../../sqlServer/querys/ocp/lots.js";
type LotInfo = {
success: boolean;
message: string;
data: any[];
};
export const getLots = async () => {
const { data: lotInfo, error: lotError } = await tryCatch(
const { data: l, error: lotError } = await tryCatch(
query(lotQuery, "Alplalabel online lots")
);
const lotInfo = l as LotInfo;
if (lotError) {
console.log("lot error", lotError);
return {
success: false,
message: "There was an error getting the lots",
@@ -15,9 +23,17 @@ export const getLots = async () => {
};
}
return {
success: true,
message: "Current active lots that are technically released.",
data: lotInfo,
};
if (lotInfo.success) {
return {
success: true,
message: "Current active lots that are technically released.",
data: lotInfo,
};
} else {
return {
success: true,
message: `Error getting lot info due to: ${lotInfo.message}.`,
data: [],
};
}
};

View File

@@ -42,7 +42,8 @@ export const prolinkCheck = async (lot: any) => {
// run the query
try {
const prolink = await query(prolinkQuery, "Prolink Checks");
const p: any = await query(prolinkQuery, "Prolink Checks");
const prolink = p.data;
// filter out the lot
const filterdLot = prolink.filter(
(p: any) => p.AlplaLabelOnline === lot

View File

@@ -14,7 +14,9 @@ export const isMainMatStaged = async (lot: any) => {
const updateQuery = mmQuery.replaceAll("[lotNumber]", lot.lot);
try {
const res = await query(updateQuery, "Main Material Check");
const r: any = await query(updateQuery, "Main Material Check");
const res: any = r.data;
createLog(
"info",

View File

@@ -15,6 +15,10 @@ export const printStatus = [
code: 8,
text: "First time printing",
},
{
code: 9,
text: "Offline",
},
];
export const printerUpdate = async (printer: any, status: any) => {

View File

@@ -163,28 +163,55 @@ export const printerStatus = async (p: any) => {
resolve({ success: true, message: "Print cycle completed." });
});
printer.on("error", async (error) => {
// just going to say theres an error with the printer
// as a safety destory it if its still there
printer.on("end", () => {
setTimeout(() => {
if (!printer.destroyed) {
createLog(
"info",
"printerState",
"ocp",
`${p.name}: was force closed, during normal cycle counting`
);
printer.destroy();
}
}, 1000);
});
if (!errorCheck) {
printer.on("error", async (error: any) => {
// just going to say theres an error with the printer
//console.log(error.code);
if (error.code.includes("ETIMEDOUT") && !errorCheck) {
createLog("error", "ocp", "ocp", `${p.name} is offline`);
await printerUpdate(p, 9);
errorCheck = true;
printer.end();
resolve({
success: false,
message: "The printer is offline.",
});
}
if (!error.code.includes("ETIMEDOUT") && !errorCheck) {
createLog(
"error",
"ocp",
"ocp",
`${p.name} encountered an error: ${error}`
);
await printerUpdate(p, 7);
errorCheck = true;
// send log data
// fake line
printer.end();
resolve({
success: false,
message: "There was an error with the printer.",
});
}
await printerUpdate(p, 7);
errorCheck = true;
// send log data
// fake line
printer.end();
resolve({
success: false,
message: "There was an error with the printer.",
});
});
});
};
@@ -230,13 +257,13 @@ export const autoLabelingStats = async (p: any) => {
printer.on("error", async (error) => {
// just going to say theres an error with the printer
console.log(error);
if (!errorCheck) {
createLog(
"error",
"ocp",
"ocp",
`${p.name} encountered an error: ${error}`
`${p.name}, encountered an error: ${error}`
);
}

View File

@@ -38,8 +38,8 @@ export const dualPrintingProcess = async (lotInfo: any) => {
try {
// get what was last printed
const result = await query(printedQuery, "Last Printed Query");
lastPrinted = result[0].IdMaschine;
const result: any = await query(printedQuery, "Last Printed Query");
lastPrinted = result.data[0].IdMaschine;
} catch (err) {
createLog(
"error",
@@ -51,8 +51,8 @@ export const dualPrintingProcess = async (lotInfo: any) => {
try {
// get if if running or down
const first = await query(fisrtLineDT, "First line DT Check");
firstLine = first[0].LineCheck;
const first: any = await query(fisrtLineDT, "First line DT Check");
firstLine = first.data[0].LineCheck;
} catch (err) {
createLog(
"error",
@@ -63,8 +63,8 @@ export const dualPrintingProcess = async (lotInfo: any) => {
}
try {
const second = await query(secondLineDT, "Second line DT Check");
secondLine = second[0].LineCheck;
const second: any = await query(secondLineDT, "Second line DT Check");
secondLine = second.data[0].LineCheck;
} catch (err) {
createLog(
"error",