build(notification): fixed fifo index ts errors

This commit is contained in:
2026-02-16 09:38:10 -06:00
parent 16edf58025
commit 29b3be41a1
2 changed files with 96 additions and 93 deletions

View File

@@ -1,7 +1,9 @@
import { OpenAPIHono } from "@hono/zod-openapi";
import { migrateAdjustments } from "./controller/siloAdjustments/migrateAdjustments.js";
import { getLanesToCycleCount } from "./controller/warehouse/cycleCountChecks/cyclecountCheck.js";
import attachSilo from "./route/attachSilo.js";
import bookOutPallet from "./route/bookout.js";
import comsumeMaterial from "./route/consumeMaterial.js";
import detachSilo from "./route/detachSilo.js";
import postBulkOrders from "./route/dm/bulkOrdersIn.js";
@@ -56,6 +58,7 @@ const routes = [
// logisitcs
removeAsNonReable,
getSSCC,
bookOutPallet,
] as const;
// app.route("/server", modules);

View File

@@ -1,112 +1,112 @@
import { isBefore } from "date-fns";
import { db } from "../../../../../database/dbclient.js";
import { fifoIndex } from "../../../../../database/schema/fifoIndex.js";
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
import { query } from "../../../sqlServer/prodSqlServer.js";
import { currentInv } from "../../../sqlServer/querys/notifications/fifoIndex/currentInv.js";
import { shippedPallets } from "../../../sqlServer/querys/notifications/fifoIndex/shippedPallets.js";
import { db } from "../../../../../database/dbclient.js";
import { fifoIndex } from "../../../../../database/schema/fifoIndex.js";
export default async function fifoIndexCheck() {
/**
* getting the shipped pallets
*/
const { data: shipped, error: eShipped } = await tryCatch(
query(shippedPallets, "notify shipped pallets")
);
/**
* getting the shipped pallets
*/
const { data: shipped, error: eShipped } = await tryCatch(
query(shippedPallets, "notify shipped pallets"),
);
const { data: currentStuff, error: eCurrentInv } = await tryCatch(
query(currentInv, "notify shipped pallets")
);
const { data: currentStuff, error: eCurrentInv } = await tryCatch(
query(currentInv, "notify shipped pallets"),
);
// console.log(shipped?.data[2]);
// console.log(currentStuff?.data[2]);
// console.log(shipped?.data[2]);
// console.log(currentStuff?.data[2]);
/**
* We want to check if the each shippened pallet is out of fifo
*/
const check = shipped?.data.map((n: any) => {
/**
* Returns all data so we know if we are in or out.
*/
//check if there are pallets older than the current one we are mapped on.
const fifoCheck = currentStuff?.data.filter(
(i: any) => isBefore(i.prodDate, n.prodDate) && i.av === n.av
);
//console.log(fifoCheck.length);
if (fifoCheck.length > 0) {
// console.log("Out of fifo", {
// av: n.av,
// rn: n.runningNr,
// fRn: fifoCheck[0].runningNr,
// dates: [fifoCheck[0].prodDate, n.prodDate],
// });
}
/**
* We want to check if the each shippened pallet is out of fifo
*/
const check: any = shipped?.data.map((n: any) => {
/**
* Returns all data so we know if we are in or out.
*/
//check if there are pallets older than the current one we are mapped on.
const fifoCheck = currentStuff?.data.filter(
(i: any) => isBefore(i.prodDate, n.prodDate) && i.av === n.av,
) as any;
//console.log(fifoCheck.length);
if (fifoCheck.length > 0) {
// console.log("Out of fifo", {
// av: n.av,
// rn: n.runningNr,
// fRn: fifoCheck[0].runningNr,
// dates: [fifoCheck[0].prodDate, n.prodDate],
// });
}
return {
...n,
// currentInv: fifoCheck[0],
fifoFollowed: fifoCheck.length === 0 ? true : false,
};
});
return {
...n,
// currentInv: fifoCheck[0],
fifoFollowed: fifoCheck.length === 0 ? true : false,
};
});
/**
* lets see just the av that is our or in
*/
/**
* lets see just the av that is our or in
*/
const avCheck = (check: any) => {
/**
* This will only return the data based on out of fifo.
*/
// check how many times each av showed up
const avCounts = check.reduce((a: any, c: any) => {
if (c.fifoFollowed === false) {
const avValue = c.av;
a[avValue] = (a[avValue] || 0) + 1;
}
return a;
}, {});
const avCheck = (check: any) => {
/**
* This will only return the data based on out of fifo.
*/
// check how many times each av showed up
const avCounts = check.reduce((a: any, c: any) => {
if (c.fifoFollowed === false) {
const avValue = c.av;
a[avValue] = (a[avValue] || 0) + 1;
}
return a;
}, {});
// transform them back to an avCount Object
const result = Object.keys(avCounts).map((av) => ({
av: parseInt(av, 10),
count: avCounts[av],
}));
// transform them back to an avCount Object
const result = Object.keys(avCounts).map((av) => ({
av: parseInt(av, 10),
count: avCounts[av],
}));
return result;
};
return result;
};
const outOfFifo: any = avCheck(check);
const totalOut = outOfFifo.reduce((sum: any, c: any) => {
return sum + c.count;
}, 0);
const outOfFifo: any = avCheck(check);
const totalOut = outOfFifo.reduce((sum: any, c: any) => {
return sum + c.count;
}, 0);
/**
* add the data to the db
*/
for (let i = 0; i < check.length; i++) {
const { data: dbInsert, error: dbE } = await tryCatch(
db
.insert(fifoIndex)
.values({
lot: check[i].lot,
av: check[i].av,
runningNr: check[i].runningNr,
prodDate: check[i].prodDate,
fifoFollowed: check[i].fifoFollowed,
add_Date: check[i].add_Date,
})
.onConflictDoNothing()
);
}
/**
* add the data to the db
*/
for (let i = 0; i < check!.length; i++) {
const { data: dbInsert, error: dbE } = await tryCatch(
db
.insert(fifoIndex)
.values({
lot: check[i].lot,
av: check[i].av,
runningNr: check[i].runningNr,
prodDate: check[i].prodDate,
fifoFollowed: check[i].fifoFollowed,
add_Date: check[i].add_Date,
})
.onConflictDoNothing(),
);
}
return {
success: true,
message: "Fifo index data",
data: {
palletsOut: check,
totalShipped: shipped?.data.length,
inFifo: shipped?.data.length - totalOut,
outOfFifoData: outOfFifo,
},
};
return {
success: true,
message: "Fifo index data",
data: {
palletsOut: check,
totalShipped: shipped?.data.length,
inFifo: shipped!.data.length - totalOut,
outOfFifoData: outOfFifo,
},
};
}