feat(scanner): tcp scanner connection based on env var no more db stuff
This commit is contained in:
155
lstV2/server/services/logistics/controller/commands/bookout.ts
Normal file
155
lstV2/server/services/logistics/controller/commands/bookout.ts
Normal file
@@ -0,0 +1,155 @@
|
||||
import axios from "axios";
|
||||
import net from "net";
|
||||
import { db } from "../../../../../database/dbclient.js";
|
||||
import { commandLog } from "../../../../../database/schema/commandLog.js";
|
||||
import { createSSCC } from "../../../../globalUtils/createSSCC.js";
|
||||
import { prodEndpointCreation } from "../../../../globalUtils/createUrl.js";
|
||||
import { scanner } from "../../../../globalUtils/scannerConnect.js";
|
||||
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
||||
import { createLog } from "../../../logger/logger.js";
|
||||
import { query } from "../../../sqlServer/prodSqlServer.js";
|
||||
import { sqlQuerySelector } from "../../../sqlServer/utils/querySelector.utils.js";
|
||||
|
||||
type Data = {
|
||||
runningNr: number;
|
||||
reason: string;
|
||||
user: string;
|
||||
};
|
||||
export const bookOutPallet = async (data: Data) => {
|
||||
const { runningNr, reason, user } = data;
|
||||
|
||||
if (!reason || reason.length < 4) {
|
||||
return {
|
||||
success: false,
|
||||
status: 400,
|
||||
message: "The reason provided is to short",
|
||||
data: [],
|
||||
};
|
||||
}
|
||||
|
||||
const queryCheck = sqlQuerySelector("inventoryInfo.query");
|
||||
|
||||
if (!queryCheck.success) {
|
||||
return {
|
||||
success: false,
|
||||
status: 400,
|
||||
message: queryCheck.message,
|
||||
data: data,
|
||||
};
|
||||
}
|
||||
const { data: label, error: labelError } = (await tryCatch(
|
||||
query(
|
||||
queryCheck.query!.replace("[runningNr]", `${runningNr}`),
|
||||
"labelQuery",
|
||||
),
|
||||
)) as any;
|
||||
|
||||
if (labelError) {
|
||||
return {
|
||||
success: false,
|
||||
status: 400,
|
||||
message: labelError.message,
|
||||
data: labelError,
|
||||
};
|
||||
}
|
||||
|
||||
// check if we are in ppoo
|
||||
if (label.data.length <= 0) {
|
||||
return {
|
||||
success: false,
|
||||
status: 400,
|
||||
message: `${runningNr} is not currently in ppoo, please move to ppoo before trying to book-out`,
|
||||
data: [],
|
||||
};
|
||||
}
|
||||
|
||||
// check if the label is blocked for coa.
|
||||
if (
|
||||
label.data[0].blockingReason &&
|
||||
!label.data[0].blockingReason?.includes("COA")
|
||||
) {
|
||||
return {
|
||||
success: false,
|
||||
status: 400,
|
||||
message: `${runningNr} is not currently blocked for coa, to get this pallet booked out please take the label to quality to be released then you can book-out.`,
|
||||
data: [],
|
||||
};
|
||||
}
|
||||
|
||||
if (label.data[0].blockingReason) {
|
||||
await scanner.scan("AlplaPRODcmd89");
|
||||
await scanner.scan(`${label.data[0].barcode}`);
|
||||
}
|
||||
|
||||
// create the url to post
|
||||
const url = await prodEndpointCreation(
|
||||
"/public/v1.1/Manufacturing/ProductionControlling/BookOut",
|
||||
);
|
||||
const SSCC = await createSSCC(runningNr);
|
||||
|
||||
const bookOutData = {
|
||||
sscc: SSCC.slice(2),
|
||||
scannerId: "666",
|
||||
};
|
||||
|
||||
try {
|
||||
const results = await axios.post(url, bookOutData, {
|
||||
headers: {
|
||||
"X-API-Key": process.env.TEC_API_KEY || "",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
if (results.data.Errors) {
|
||||
return {
|
||||
success: false,
|
||||
status: 400,
|
||||
message: results.data.Errors.Error.Description,
|
||||
};
|
||||
}
|
||||
|
||||
// if (results.data.Result !== 0) {
|
||||
// console.log("stopping here and closing to soon", results);
|
||||
// return {
|
||||
// success: false,
|
||||
// status: 400,
|
||||
// message: results.data.Message,
|
||||
// };
|
||||
// }
|
||||
|
||||
const { data: commandL, error: ce } = await tryCatch(
|
||||
db.insert(commandLog).values({
|
||||
commandUsed: "book out",
|
||||
bodySent: data,
|
||||
reasonUsed: reason,
|
||||
}),
|
||||
);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `${runningNr} was booked out`,
|
||||
status: results.status,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.log(bookOutData);
|
||||
|
||||
return {
|
||||
success: false,
|
||||
status: 400,
|
||||
message: error.response?.data,
|
||||
data: error.response?.data,
|
||||
};
|
||||
}
|
||||
|
||||
// });
|
||||
|
||||
/**
|
||||
* book out the label with
|
||||
* url /public/v1.1/Manufacturing/ProductionControlling/BookOut
|
||||
* {
|
||||
* "sscc": "string",
|
||||
* "scannerId": "string"
|
||||
* }
|
||||
*/
|
||||
//---------------------------------------------------------------------------------------\\
|
||||
};
|
||||
@@ -1,120 +1,50 @@
|
||||
import axios from "axios";
|
||||
import { commandLog } from "../../../../../database/schema/commandLog.js";
|
||||
import { prodEndpointCreation } from "../../../../globalUtils/createUrl.js";
|
||||
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
||||
import { lstAuth } from "../../../../index.js";
|
||||
import { createSSCC } from "../../../../globalUtils/createSSCC.js";
|
||||
import { db } from "../../../../../database/dbclient.js";
|
||||
import net from "net";
|
||||
import { commandLog } from "../../../../../database/schema/commandLog.js";
|
||||
import { scanner } from "../../../../globalUtils/scannerConnect.js";
|
||||
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
||||
import { query } from "../../../sqlServer/prodSqlServer.js";
|
||||
import { labelInfo } from "../../../sqlServer/querys/warehouse/labelInfo.js";
|
||||
import { settings } from "../../../../../database/schema/settings.js";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { serverData } from "../../../../../database/schema/serverData.js";
|
||||
|
||||
export const removeAsNonReusable = async (data: any) => {
|
||||
// const removalUrl = await prodEndpointCreation(
|
||||
// "/public/v1.0/Warehousing/RemoveAsNonReusableMaterial"
|
||||
// );
|
||||
// get the label info
|
||||
const { data: label, error: labelError } = (await tryCatch(
|
||||
query(labelInfo.replaceAll("[runningNr]", data.runningNr), "Label Info"),
|
||||
)) as any;
|
||||
|
||||
// const sscc = await createSSCC(data.runningNr);
|
||||
if (label.data[0].stockStatus === "notOnStock") {
|
||||
return {
|
||||
success: false,
|
||||
message: `The label: ${data.runningNr} is not currently in stock`,
|
||||
data: [],
|
||||
};
|
||||
}
|
||||
|
||||
// const { data: remove, error } = await tryCatch(
|
||||
// axios.post(
|
||||
// removalUrl,
|
||||
// { scannerId: "500", sscc: sscc.slice(2) },
|
||||
// {
|
||||
// headers: { Authorization: `Basic ${lstAuth}` },
|
||||
// }
|
||||
// )
|
||||
// );
|
||||
if (label.data[0].blockingReason) {
|
||||
return {
|
||||
success: false,
|
||||
status: 400,
|
||||
message: `${data.runningNr} is currently blocked, to get this pallet removed please take the label to quality to be released then you can remove.`,
|
||||
data: [],
|
||||
};
|
||||
}
|
||||
|
||||
// use a scanner tcp connection to trigger this process
|
||||
const STX = "\x02";
|
||||
const ETX = "\x03";
|
||||
const scanner = new net.Socket();
|
||||
let stage = 0;
|
||||
// get the label info
|
||||
const { data: label, error: labelError } = (await tryCatch(
|
||||
query(labelInfo.replaceAll("[runningNr]", data.runningNr), "Label Info")
|
||||
)) as any;
|
||||
await scanner.scan("AlplaPRODcmd23");
|
||||
await scanner.scan(`${label.data[0].barcode}`);
|
||||
|
||||
if (label.data[0].stockStatus === "notOnStock") {
|
||||
return {
|
||||
success: false,
|
||||
message: `The label: ${data.runningNr} is not currently in stock`,
|
||||
data: [],
|
||||
};
|
||||
}
|
||||
let reason = data.reason || "";
|
||||
delete data.reason;
|
||||
|
||||
// get the server ip based on the token.
|
||||
const setting = await db.select().from(settings);
|
||||
const { data: commandL, error: ce } = await tryCatch(
|
||||
db.insert(commandLog).values({
|
||||
commandUsed: "removeAsNonReusable",
|
||||
bodySent: data,
|
||||
reasonUsed: reason,
|
||||
}),
|
||||
);
|
||||
|
||||
const plantInfo = await db.select().from(serverData);
|
||||
const plantToken = setting.filter((n: any) => n.name === "plantToken");
|
||||
const scannerID = setting.filter((n: any) => n.name === "scannerID");
|
||||
const scannerPort = setting.filter((n: any) => n.name === "scannerPort");
|
||||
const plantData = plantInfo.filter(
|
||||
(p: any) => p.plantToken === plantToken[0].value
|
||||
);
|
||||
|
||||
scanner.connect(
|
||||
parseInt(scannerPort[0].value),
|
||||
plantData[0].idAddress!,
|
||||
async () => {
|
||||
// need to get the ip from the server data and scanner port
|
||||
//console.log(`connected to scanner`);
|
||||
scanner.write(`${STX}${scannerID[0].value}@AlplaPRODcmd23${ETX}`);
|
||||
}
|
||||
);
|
||||
scanner.on("data", (data) => {
|
||||
const response = data.toString();
|
||||
//console.log("Received:", response.trimStart());
|
||||
if (stage === 0) {
|
||||
stage = 1;
|
||||
scanner.write(
|
||||
`${STX}${scannerID[0].value}@${label.data[0].Barcode}${ETX}`
|
||||
);
|
||||
} else if (stage === 1) {
|
||||
scanner.end();
|
||||
}
|
||||
});
|
||||
scanner.on("close", () => {
|
||||
//console.log("Connection closed");
|
||||
scanner.destroy();
|
||||
});
|
||||
scanner.on("error", (err) => {
|
||||
//console.error("Scanner error:", err);
|
||||
scanner.destroy();
|
||||
return {
|
||||
success: false,
|
||||
message: `The label: ${data.runningNr} encountering an error while being removed, please try again`,
|
||||
data: [],
|
||||
};
|
||||
});
|
||||
|
||||
// if (error) {
|
||||
// //console.log(error);
|
||||
// return {
|
||||
// success: false,
|
||||
// message: `There was an error removing ${data.runningNr}`,
|
||||
// data: [],
|
||||
// };
|
||||
// }
|
||||
|
||||
let reason = data.reason || "";
|
||||
delete data.reason;
|
||||
|
||||
const { data: commandL, error: ce } = await tryCatch(
|
||||
db.insert(commandLog).values({
|
||||
commandUsed: "removeAsNonReusable",
|
||||
bodySent: data,
|
||||
reasonUsed: reason,
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `The label: ${data.runningNr}, was removed`,
|
||||
data: [],
|
||||
};
|
||||
return {
|
||||
success: true,
|
||||
message: `The label: ${data.runningNr}, was removed`,
|
||||
data: [],
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user