feat(eom): added in historical inv data split accordingly

This commit is contained in:
2025-05-28 17:01:22 -05:00
parent 25cfee58d0
commit 96deca15f0
16 changed files with 6385 additions and 36 deletions

View File

@@ -0,0 +1,91 @@
import { db } from "../../../../../database/dbclient.js";
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
import { createLog } from "../../../logger/logger.js";
import { query } from "../../../sqlServer/prodSqlServer.js";
import { totalInvNoRn } from "../../../sqlServer/querys/dataMart/totalINV.js";
import { invHistoricalData } from "../../../../../database/schema/historicalINV.js";
import { format } from "date-fns-tz";
import { settings } from "../../../../../database/schema/settings.js";
import { sql } from "drizzle-orm";
import { createLogisticsJob } from "../../utils/logisticsIntervals.js";
export const runHistoricalData = async () => {
/**
* Runs a query at shift change on first shift each day this will be the closest date to the true historical data for blocked, consignment
*/
const { data: set, error: setError } = await tryCatch(
db.select().from(settings)
);
if (setError) {
createLog(
"error",
"lst",
"eom",
"There was an error getting eom historical inv data."
);
return;
}
const timeZone = set.filter((n: any) => n.name === "timezone");
createLogisticsJob("histInv", `0 6 * * *`, timeZone[0].value, async () => {
// remove the lotnumber from the query
const updatedQuery = totalInvNoRn.replaceAll(
",IdProdPlanung",
"--,IdProdPlanung"
);
const { data: inv, error: invError } = await tryCatch(
query(updatedQuery, "EOM historical inv")
);
if (invError) {
createLog(
"error",
"lst",
"eom",
"There was an error getting eom historical inv data."
);
return;
}
/**
* add the inv into the hist table
*/
const setting: any = set;
for (let i = 0; i < inv?.data.length; i++) {
const current = inv?.data[i];
const { data, error } = await tryCatch(
db.insert(invHistoricalData).values({
histDate: format(new Date(), "MM-dd-yyyy"),
plantToken: setting.filter(
(n: any) => n.name === "plantToken"
)[0].value,
article: current.av,
articleDescription: current.Alias,
total_QTY: current.Total_PalletQTY,
avaliable_QTY: current.Avaliable_PalletQTY,
coa_QTY: current.COA_QTY,
held_QTY: current.Held_QTY,
consignment: current.Consigment,
//location: integer("location"),
upd_user: "LST",
upd_date: sql`NOW()`,
})
);
if (error) {
createLog(
"error",
"lst",
"eom",
`Error addeding historical data, ${error}`
);
}
}
});
};

View File

@@ -16,6 +16,7 @@ import standardTemplate from "./route/dm/getStandardTemplate.js";
import standardForcasttemplate from "./route/dm/getStandardForecastTemplate.js";
import postForecast from "./route/dm/forecastIn.js";
import outbound from "./route/getOutbound.js";
import { runHistoricalData } from "./controller/eom/historicalInv.js";
const app = new OpenAPIHono();
@@ -49,7 +50,8 @@ const appRoutes = routes.forEach((route) => {
setTimeout(() => {
migrateAdjustments();
}, 120 * 1000);
runHistoricalData();
}, 120 * 1000); // starts 2 min after a server restart or crash.
/**
* Start the cycle count check

View File

@@ -0,0 +1,61 @@
import { Cron } from "croner";
import type { JobInfo } from "../../../types/JobInfo.js";
import { createLog } from "../../logger/logger.js";
export let runningLogisticsCrons: Record<string, Cron> = {};
export const createLogisticsJob = (
id: string, // this is just the name of the job running
schedule: string, // `*/30 * * * *`; // default to be every 30 min
timezone: string,
task: () => Promise<void>
) => {
// Destroy existing job if it exists
if (runningLogisticsCrons[id]) {
runningLogisticsCrons[id].stop(); // Croner uses .stop() instead of .destroy()
}
// Create new job with Croner
runningLogisticsCrons[id] = new Cron(
schedule,
{
timezone: timezone,
catch: true, // Prevents unhandled rejections
},
task
);
createLog(
"info",
"lst",
"logistics",
`Cron setup for ${id}, trigger time: ${schedule}`
);
// Optional: Add error handling (Croner emits 'error' events)
// runningNotifications[id].on("error", (err) => {
// console.error(`Job ${id} failed:`, err);
// });
};
export const getAllLogisticsJobs = (): JobInfo[] => {
return Object.entries(runningLogisticsCrons).map(([id, job]) => ({
id,
schedule: job.getPattern() || "invalid",
nextRun: job.nextRun() || null,
lastRun: job.previousRun() || null,
isRunning: job ? !job.isStopped() : false,
}));
};
// const removeNotification = (id: any) => {
// if (runningLogisticsCrons[id]) {
// runningLogisticsCrons[id].stop();
// delete runningLogisticsCrons[id];
// }
// };
export const stopAllLogisticsJobs = () => {
Object.values(runningLogisticsCrons).forEach((job: any) => job.stop());
runningLogisticsCrons = {}; // Clear the object
};