156 lines
3.6 KiB
TypeScript
156 lines
3.6 KiB
TypeScript
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"
|
|
* }
|
|
*/
|
|
//---------------------------------------------------------------------------------------\\
|
|
};
|