From ac27a286c07733333703d8421cfa525691363e54 Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Tue, 2 Dec 2025 15:24:10 -0600 Subject: [PATCH] fix(lot transfer): changes to make it so the reprint and return do not happen instantly --- lstV2/server/globalUtils/runProdApi.ts | 160 +++++++++--------- .../ocp/controller/materials/lotTransfer.ts | 37 ++-- 2 files changed, 107 insertions(+), 90 deletions(-) diff --git a/lstV2/server/globalUtils/runProdApi.ts b/lstV2/server/globalUtils/runProdApi.ts index f196c11..ad8a01c 100644 --- a/lstV2/server/globalUtils/runProdApi.ts +++ b/lstV2/server/globalUtils/runProdApi.ts @@ -1,92 +1,94 @@ import axios from "axios"; +import { createLog } from "../services/logger/logger.js"; import { prodEndpointCreation } from "./createUrl.js"; import { tryCatch } from "./tryCatch.js"; -import { createLog } from "../services/logger/logger.js"; type bodyData = any; type Data = { - endpoint: string; - data: bodyData[]; + endpoint: string; + data: bodyData[]; }; +/** + * + * @param data + * @param timeoutDelay + * @returns + */ export const runProdApi = async (data: Data) => { - /** - * Detachs a silo - */ + let url = await prodEndpointCreation(data.endpoint); - let url = await prodEndpointCreation(data.endpoint); + const { data: d, error } = await tryCatch( + axios.post(url, data.data[0], { + headers: { + "X-API-Key": process.env.TEC_API_KEY || "", + "Content-Type": "application/json", + }, + }), + ); - const { data: d, error } = await tryCatch( - axios.post(url, data.data[0], { - headers: { - "X-API-Key": process.env.TEC_API_KEY || "", - "Content-Type": "application/json", - }, - }) - ); + let e = error as any; + if (e) { + //console.log(e.response); + if (e.status === 401) { + createLog( + "error", + "lst", + "logistics", + `Not authorized: ${JSON.stringify(e.response?.data)}`, + ); + const data = { + success: false, + message: `Not authorized: ${JSON.stringify(e.response?.data)}`, + data: { + status: e.response?.status, + statusText: e.response?.statusText, + data: e.response?.data, + }, + }; + return data; + } else { + createLog( + "error", + "lst", + "logistics", + `There was an error processing the endpoint: ${JSON.stringify( + e.response?.data, + )}`, + ); + return { + success: false, + message: `There was an error processing the endpoint: ${JSON.stringify( + e.response?.data, + )}`, + data: { + status: e.response?.status, + statusText: e.response?.statusText, + data: e.response?.data, + }, + }; + } + } - let e = error as any; - if (e) { - //console.log(e.response); - if (e.status === 401) { - createLog( - "error", - "lst", - "logistics", - `Not autorized: ${JSON.stringify(e.response?.data)}` - ); - const data = { - success: false, - message: `Not autorized: ${JSON.stringify(e.response?.data)}`, - data: { - status: e.response?.status, - statusText: e.response?.statusText, - data: e.response?.data, - }, - }; - return data; - } else { - createLog( - "error", - "lst", - "logistics", - `There was an error processing the endpoint: ${JSON.stringify( - e.response?.data - )}` - ); - return { - success: false, - message: `There was an error processing the endpoint: ${JSON.stringify( - e.response?.data - )}`, - data: { - status: e.response?.status, - statusText: e.response?.statusText, - data: e.response?.data, - }, - }; - } - } - - if (d?.status !== 200) { - return { - success: false, - message: "Error processing endpoint", - data: { - status: d?.status, - statusText: d?.statusText, - data: d?.data, - }, - }; - } else { - return { - success: true, - message: "Endpoint was processed", - data: { - status: d.status, - statusText: d.statusText, - data: d.data, - }, - }; - } + if (d?.status !== 200) { + return { + success: false, + message: "Error processing endpoint", + data: { + status: d?.status, + statusText: d?.statusText, + data: d?.data, + }, + }; + } else { + return { + success: true, + message: "Endpoint was processed", + data: { + status: d.status, + statusText: d.statusText, + data: d.data, + }, + }; + } }; diff --git a/lstV2/server/services/ocp/controller/materials/lotTransfer.ts b/lstV2/server/services/ocp/controller/materials/lotTransfer.ts index e1c5e6f..1247a58 100644 --- a/lstV2/server/services/ocp/controller/materials/lotTransfer.ts +++ b/lstV2/server/services/ocp/controller/materials/lotTransfer.ts @@ -3,6 +3,7 @@ import { eq } from "drizzle-orm"; import { success } from "zod/v4"; import { db } from "../../../../../database/dbclient.js"; import { printerData } from "../../../../../database/schema/printers.js"; +import { delay } from "../../../../globalUtils/delay.js"; import { runProdApi } from "../../../../globalUtils/runProdApi.js"; import { tryCatch } from "../../../../globalUtils/tryCatch.js"; import { createLog } from "../../../logger/logger.js"; @@ -158,7 +159,7 @@ export const lotMaterialTransfer = async (data: NewLotData) => { }; } - let timeoutTrans: number = data.type === "lot" ? 30 : 10; + let timeoutTrans: number = data.type === "lot" ? 5 : 5; // get the barcode, and layoutID from the running number const { data: label, error: labelError } = (await tryCatch( query( @@ -278,13 +279,10 @@ export const lotMaterialTransfer = async (data: NewLotData) => { }; //console.log(matReturnData); - const { data: matReturn, error: matReturError } = (await tryCatch( - runProdApi({ - endpoint: - "/public/v1.0/IssueMaterial/ReturnPartiallyConsumedManualMaterial", - data: [matReturnData], - }), - )) as any; + + // set a delay for when we try to return the label + + const matReturn = await returnMaterial(matReturnData); if (!matReturn.success) { createLog( @@ -305,12 +303,12 @@ export const lotMaterialTransfer = async (data: NewLotData) => { barcode: label?.data[0].Barcode, }; - const delay = + const delayTimer = data.type === "lot" ? timeoutTrans * 1000 : target.getTime() - now.getTime(); - const transfer = await transferMaterial(delay, data, consumeLot, newQty); + const transfer = await transferMaterial(delayTimer, data, consumeLot, newQty); if (!transfer.success) { return { @@ -340,6 +338,22 @@ export const lotMaterialTransfer = async (data: NewLotData) => { } }; +const returnMaterial = async (matReturnData: any) => { + console.log("Getting ready to Returning Materials"); + await delay(5 * 1000); + console.log("doing the return"); + const { data: matReturn, error: matReturError } = (await tryCatch( + runProdApi({ + endpoint: + "/public/v1.0/IssueMaterial/ReturnPartiallyConsumedManualMaterial", + data: [matReturnData], + }), + )) as any; + + if (matReturError) return matReturError; + return matReturn; +}; + const transferMaterial = async ( delay: number, data: any, @@ -347,12 +361,13 @@ const transferMaterial = async ( newQty: any, ) => { //console.log(data); + console.log("Transferring the material"); if (pendingJobs.has(data.runningNumber)) { createLog( "error", "materials", "ocp", - `${data.runningNumber} is pending to be transfered already`, + `${data.runningNumber} is pending to be transferred already`, ); return { success: false,