refactor(lottransfer): formatting changes

This commit is contained in:
2025-10-30 10:24:22 -05:00
parent c552b9eb1c
commit a19b03b8bf

View File

@@ -1,14 +1,14 @@
import { format, formatDuration, intervalToDuration } from "date-fns";
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
import { success } from "zod/v4";
import { db } from "../../../../../database/dbclient.js"; import { db } from "../../../../../database/dbclient.js";
import { printerData } from "../../../../../database/schema/printers.js"; import { printerData } from "../../../../../database/schema/printers.js";
import { runProdApi } from "../../../../globalUtils/runProdApi.js"; import { runProdApi } from "../../../../globalUtils/runProdApi.js";
import { tryCatch } from "../../../../globalUtils/tryCatch.js"; import { tryCatch } from "../../../../globalUtils/tryCatch.js";
import { createLog } from "../../../logger/logger.js"; import { createLog } from "../../../logger/logger.js";
import { query } from "../../../sqlServer/prodSqlServer.js"; import { query } from "../../../sqlServer/prodSqlServer.js";
import { labelInfo } from "../../../sqlServer/querys/warehouse/labelInfo.js";
import { format, formatDuration, intervalToDuration } from "date-fns";
import { shiftChange } from "../../../sqlServer/querys/misc/shiftChange.js"; import { shiftChange } from "../../../sqlServer/querys/misc/shiftChange.js";
import { success } from "zod/v4"; import { labelInfo } from "../../../sqlServer/querys/warehouse/labelInfo.js";
type NewLotData = { type NewLotData = {
runningNumber: number; runningNumber: number;
@@ -58,8 +58,8 @@ export const lotMaterialTransfer = async (data: NewLotData) => {
data.runningNumber data.runningNumber
} is pending to be transfered already, remaining time ${formatDuration( } is pending to be transfered already, remaining time ${formatDuration(
duration, duration,
{ format: ["hours", "minutes", "seconds"] } { format: ["hours", "minutes", "seconds"] },
)}` )}`,
); );
return { return {
success: false, success: false,
@@ -67,14 +67,14 @@ export const lotMaterialTransfer = async (data: NewLotData) => {
data.runningNumber data.runningNumber
} is pending to be transfered already, remaining time ${formatDuration( } is pending to be transfered already, remaining time ${formatDuration(
duration, duration,
{ format: ["hours", "minutes", "seconds"] } { format: ["hours", "minutes", "seconds"] },
)}`, )}`,
data: [], data: [],
}; };
} }
// get the shift time // get the shift time
const { data: shift, error: shiftError } = (await tryCatch( const { data: shift, error: shiftError } = (await tryCatch(
query(shiftChange, "shift change from material.") query(shiftChange, "shift change from material."),
)) as any; )) as any;
if (shiftError) { if (shiftError) {
@@ -82,7 +82,7 @@ export const lotMaterialTransfer = async (data: NewLotData) => {
"error", "error",
"materials", "materials",
"ocp", "ocp",
"There was an error getting the shift times will use fallback times" "There was an error getting the shift times will use fallback times",
); );
} }
@@ -100,7 +100,7 @@ export const lotMaterialTransfer = async (data: NewLotData) => {
shiftTimeSplit.length > 0 ? parseInt(shiftTimeSplit[0]) : 5, // this will parse the hour to remove teh zero shiftTimeSplit.length > 0 ? parseInt(shiftTimeSplit[0]) : 5, // this will parse the hour to remove teh zero
shiftTimeSplit.length > 0 ? parseInt(shiftTimeSplit[1]) : 3, shiftTimeSplit.length > 0 ? parseInt(shiftTimeSplit[1]) : 3,
0, 0,
0 0,
); );
console.log("target", target.toLocaleString(), "Now", now.toLocaleString()); console.log("target", target.toLocaleString(), "Now", now.toLocaleString());
@@ -113,7 +113,7 @@ export const lotMaterialTransfer = async (data: NewLotData) => {
shiftTimeSplit.length > 0 ? parseInt(shiftTimeSplit[0]) - 1 : 5, // this will parse the hour to remove teh zero shiftTimeSplit.length > 0 ? parseInt(shiftTimeSplit[0]) - 1 : 5, // this will parse the hour to remove teh zero
shiftTimeSplit.length > 0 ? parseInt(shiftTimeSplit[1]) : 0, shiftTimeSplit.length > 0 ? parseInt(shiftTimeSplit[1]) : 0,
0, 0,
0 0,
); );
console.log("early", early.toLocaleString(), "Now", now.toLocaleString()); console.log("early", early.toLocaleString(), "Now", now.toLocaleString());
@@ -126,14 +126,14 @@ export const lotMaterialTransfer = async (data: NewLotData) => {
shiftTimeSplit.length > 0 ? parseInt(shiftTimeSplit[0]) - 1 : 5, // this will parse the hour to remove teh zero shiftTimeSplit.length > 0 ? parseInt(shiftTimeSplit[0]) - 1 : 5, // this will parse the hour to remove teh zero
shiftTimeSplit.length > 0 ? parseInt(shiftTimeSplit[1]) : 0, shiftTimeSplit.length > 0 ? parseInt(shiftTimeSplit[1]) : 0,
0, 0,
0 0,
); );
console.log( console.log(
"nextMonth", "nextMonth",
nextMonth.toLocaleString(), nextMonth.toLocaleString(),
"Now", "Now",
now.toLocaleString() now.toLocaleString(),
); );
// console.log(early, target); // console.log(early, target);
@@ -145,14 +145,14 @@ export const lotMaterialTransfer = async (data: NewLotData) => {
"ocp", "ocp",
`Eom transfers is not allowed right now please try again at ${format( `Eom transfers is not allowed right now please try again at ${format(
nextMonth, nextMonth,
"M/d/yyyy hh:mm" "M/d/yyyy hh:mm",
)} ` )} `,
); );
return { return {
success: false, success: false,
message: `Eom transfers is not allowed right now please try again at ${format( message: `Eom transfers is not allowed right now please try again at ${format(
nextMonth, nextMonth,
"M/d/yyyy hh:mm" "M/d/yyyy hh:mm",
)} `, )} `,
data: [], data: [],
}; };
@@ -163,8 +163,8 @@ export const lotMaterialTransfer = async (data: NewLotData) => {
const { data: label, error: labelError } = (await tryCatch( const { data: label, error: labelError } = (await tryCatch(
query( query(
labelInfo.replace("[runningNr]", `${data.runningNumber}`), labelInfo.replace("[runningNr]", `${data.runningNumber}`),
"Get label info" "Get label info",
) ),
)) as any; )) as any;
if (labelError) { if (labelError) {
@@ -172,7 +172,7 @@ export const lotMaterialTransfer = async (data: NewLotData) => {
"error", "error",
"materials", "materials",
"ocp", "ocp",
"There was an error getting the label info" "There was an error getting the label info",
); );
return { return {
success: false, success: false,
@@ -186,7 +186,7 @@ export const lotMaterialTransfer = async (data: NewLotData) => {
"error", "error",
"materials", "materials",
"ocp", "ocp",
`${data.runningNumber}: dose not exist or no longer in stock.` `${data.runningNumber}: dose not exist or no longer in stock.`,
); );
return { return {
success: false, success: false,
@@ -202,7 +202,7 @@ export const lotMaterialTransfer = async (data: NewLotData) => {
"error", "error",
"materials", "materials",
"ocp", "ocp",
`${data.runningNumber}: currently in stock and not consumed to a lot.` `${data.runningNumber}: currently in stock and not consumed to a lot.`,
); );
return { return {
success: false, success: false,
@@ -213,7 +213,7 @@ export const lotMaterialTransfer = async (data: NewLotData) => {
// get the pdf24 printer id // get the pdf24 printer id
const { data: printer, error: printerError } = (await tryCatch( const { data: printer, error: printerError } = (await tryCatch(
db.select().from(printerData).where(eq(printerData.name, "PDF24")) db.select().from(printerData).where(eq(printerData.name, "PDF24")),
)) as any; )) as any;
if (printerError) { if (printerError) {
@@ -221,7 +221,7 @@ export const lotMaterialTransfer = async (data: NewLotData) => {
"error", "error",
"materials", "materials",
"ocp", "ocp",
"There was an error the printer info" "There was an error the printer info",
); );
return { return {
success: false, success: false,
@@ -254,7 +254,7 @@ export const lotMaterialTransfer = async (data: NewLotData) => {
runProdApi({ runProdApi({
endpoint: "/public/v1.0/ProductionLabelling/ReprintLabel", endpoint: "/public/v1.0/ProductionLabelling/ReprintLabel",
data: [reprintData], data: [reprintData],
}) }),
)) as any; )) as any;
if (!reprint.success) { if (!reprint.success) {
@@ -262,7 +262,7 @@ export const lotMaterialTransfer = async (data: NewLotData) => {
"error", "error",
"materials", "materials",
"ocp", "ocp",
`RN:${data.runningNumber}, Reprinting Error: ${reprint.data.data.message}` `RN:${data.runningNumber}, Reprinting Error: ${reprint.data.data.message}`,
); );
return { return {
success: false, success: false,
@@ -283,7 +283,7 @@ export const lotMaterialTransfer = async (data: NewLotData) => {
endpoint: endpoint:
"/public/v1.0/IssueMaterial/ReturnPartiallyConsumedManualMaterial", "/public/v1.0/IssueMaterial/ReturnPartiallyConsumedManualMaterial",
data: [matReturnData], data: [matReturnData],
}) }),
)) as any; )) as any;
if (!matReturn.success) { if (!matReturn.success) {
@@ -291,7 +291,7 @@ export const lotMaterialTransfer = async (data: NewLotData) => {
"error", "error",
"materials", "materials",
"ocp", "ocp",
`RN:${data.runningNumber}, Return Error ${matReturn.data.data.errors[0].message}` `RN:${data.runningNumber}, Return Error ${matReturn.data.data.errors[0].message}`,
); );
return { return {
success: false, success: false,
@@ -344,7 +344,7 @@ const transferMaterial = async (
delay: number, delay: number,
data: any, data: any,
consumeLot: any, consumeLot: any,
newQty: any newQty: any,
) => { ) => {
//console.log(data); //console.log(data);
if (pendingJobs.has(data.runningNumber)) { if (pendingJobs.has(data.runningNumber)) {
@@ -352,7 +352,7 @@ const transferMaterial = async (
"error", "error",
"materials", "materials",
"ocp", "ocp",
`${data.runningNumber} is pending to be transfered already` `${data.runningNumber} is pending to be transfered already`,
); );
return { return {
success: false, success: false,
@@ -365,13 +365,12 @@ const transferMaterial = async (
// sets the time out based on the type of transfer sent over. // sets the time out based on the type of transfer sent over.
const timeoutId = setTimeout(async () => { const timeoutId = setTimeout(async () => {
try { try {
const { data: matConsume, error: matConsumeError } = const { data: matConsume, error: matConsumeError } = (await tryCatch(
(await tryCatch(
runProdApi({ runProdApi({
endpoint: endpoint:
"/public/v1.0/IssueMaterial/ConsumeNonPreparedManualMaterial", "/public/v1.0/IssueMaterial/ConsumeNonPreparedManualMaterial",
data: [consumeLot], data: [consumeLot],
}) }),
)) as any; )) as any;
if (!matConsume?.success) { if (!matConsume?.success) {
@@ -380,9 +379,8 @@ const transferMaterial = async (
"materials", "materials",
"ocp", "ocp",
`RN:${data.runningNumber}, Consume Error ${ `RN:${data.runningNumber}, Consume Error ${
matConsume?.data?.data?.errors?.[0]?.message ?? matConsume?.data?.data?.errors?.[0]?.message ?? "Unknown"
"Unknown" }`,
}`
); );
return; // still hits finally return; // still hits finally
} }
@@ -391,14 +389,14 @@ const transferMaterial = async (
"info", "info",
"materials", "materials",
"ocp", "ocp",
`RN:${data.runningNumber}: qty: ${newQty}, was transferred to lot:${data.lotNumber}` `RN:${data.runningNumber}: qty: ${newQty}, was transferred to lot:${data.lotNumber}`,
); );
} catch (err) { } catch (err) {
createLog( createLog(
"error", "error",
"materials", "materials",
"ocp", "ocp",
`RN:${data.runningNumber}, ${err}` `RN:${data.runningNumber}, ${err}`,
); );
} finally { } finally {
// Always clear the pending entry, even if error // Always clear the pending entry, even if error