79 lines
2.1 KiB
TypeScript
79 lines
2.1 KiB
TypeScript
import axios from "axios";
|
|
import { db } from "../../../../../database/dbclient.js";
|
|
import { commandLog } from "../../../../../database/schema/commandLog.js";
|
|
import { prodEndpointCreation } from "../../../../globalUtils/createUrl.js";
|
|
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
|
import { createLog } from "../../../logger/logger.js";
|
|
import { query } from "../../../sqlServer/prodSqlServer.js";
|
|
import { labelData } from "../../../sqlServer/querys/materialHelpers/labelInfo.js";
|
|
import { labelInfo } from "../../../sqlServer/querys/warehouse/labelInfo.js";
|
|
|
|
type Data = {
|
|
runningNr: string;
|
|
lotNum: number;
|
|
};
|
|
export const consumeMaterial = async (data: Data) => {
|
|
const { runningNr, lotNum } = data;
|
|
// replace the rn
|
|
|
|
// console.log(data);
|
|
|
|
const rnReplace = labelInfo.replaceAll("[runningNr]", runningNr);
|
|
|
|
let barcode;
|
|
// get the barcode from the running number
|
|
try {
|
|
const r: any = await query(rnReplace, "labelData");
|
|
//console.log(r);
|
|
barcode = r?.data;
|
|
} catch (error) {
|
|
console.log(error);
|
|
createLog("error", "", "logistics", `Error getting barcode: ${error}`);
|
|
}
|
|
|
|
if (barcode.length === 0) {
|
|
return {
|
|
success: false,
|
|
message: "The running number you've entered not on stock.",
|
|
};
|
|
//throw Error("The provided runningNr is not in stock");
|
|
}
|
|
// create the url to post
|
|
const url = await prodEndpointCreation(
|
|
"/public/v1.0/IssueMaterial/ConsumeNonPreparedManualMaterial",
|
|
);
|
|
|
|
const consumeSomething = {
|
|
productionLot: lotNum,
|
|
barcode: barcode[0]?.barcode,
|
|
};
|
|
|
|
try {
|
|
const results = await axios.post(url, consumeSomething, {
|
|
headers: {
|
|
"X-API-Key": process.env.TEC_API_KEY || "",
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
|
|
const { data: commandL, error: ce } = await tryCatch(
|
|
db.insert(commandLog).values({
|
|
commandUsed: "consumeMaterial",
|
|
bodySent: data,
|
|
}),
|
|
);
|
|
return {
|
|
success: true,
|
|
message: "Material was consumed",
|
|
status: results.status,
|
|
};
|
|
} catch (error: any) {
|
|
console.log(error);
|
|
return {
|
|
success: false,
|
|
status: 200,
|
|
message: error.response?.data.errors[0].message,
|
|
};
|
|
}
|
|
};
|