fix(historicalinv): removed the second running one that caused duplicates

This commit is contained in:
2025-11-01 06:54:28 -05:00
parent ebe5c0bd5a
commit a6cc17ccb1
4 changed files with 143 additions and 132 deletions

View File

@@ -48,4 +48,14 @@ export const addListeners = async () => {
} catch (e) {
log.info({}, "label listener exists");
}
try {
log.info({}, "Bad data removing bad history data");
await client.query(`
DELETE FROM public."invHistoricalData"
WHERE upd_user = 'LST'
`);
} catch (e) {
log.info({}, "Error deleting bad inv data");
}
};

View File

@@ -96,7 +96,7 @@ export const historicalInvIMmport = async () => {
}
if (dataImport) {
createLog("info", "eom", "eom", `All data was imported succefully.`);
createLog("info", "eom", "eom", `All data was imported successfully.`);
return;
}
} else {
@@ -104,5 +104,5 @@ export const historicalInvIMmport = async () => {
}
// do the check to delete old data
deleteHistory();
//deleteHistory();
};

View File

@@ -1,93 +1,93 @@
import { format } from "date-fns-tz";
import { sql } from "drizzle-orm";
import { db } from "../../../../../database/dbclient.js";
import { invHistoricalData } from "../../../../../database/schema/historicalINV.js";
import { settings } from "../../../../../database/schema/settings.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
*/
// 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)
);
// 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;
}
// 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");
// 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"
);
// 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")
);
// 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;
}
// if (invError) {
// createLog(
// "error",
// "lst",
// "eom",
// "There was an error getting eom historical inv data."
// );
// return;
// }
/**
* add the inv into the hist table
*/
// /**
// * add the inv into the hist table
// */
const setting: any = set;
// 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()`,
})
);
// 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()`,
// })
// );
createLog("info", "lst", "eom", ` historical data was just added.`);
// createLog("info", "lst", "eom", ` historical data was just added.`);
if (error) {
createLog(
"error",
"lst",
"eom",
`Error addeding historical data, ${error}`
);
}
}
});
};
// if (error) {
// createLog(
// "error",
// "lst",
// "eom",
// `Error addeding historical data, ${error}`
// );
// }
// }
// });
// };

View File

@@ -1,72 +1,70 @@
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 comsumeMaterial from "./route/consumeMaterial.js";
import detachSilo from "./route/detachSilo.js";
import postBulkOrders from "./route/dm/bulkOrdersIn.js";
import postForecast from "./route/dm/forecastIn.js";
import standardForcasttemplate from "./route/dm/getStandardForecastTemplate.js";
import standardTemplate from "./route/dm/getStandardTemplate.js";
import getActiveLanes from "./route/getActiveLanes.js";
import intervalChecks from "./route/getActiveLogistics.js";
import getCycleCountCheck from "./route/getCycleCountChecks.js";
import getcyclecount from "./route/getCycleCountLanes.js";
import outbound from "./route/getOutbound.js";
import getPPOO from "./route/getPPOO.js";
import getConnectionType from "./route/getSiloConnectionData.js";
import getSSCC from "./route/getSSCCNumber.js";
import removeAsNonReable from "./route/removeAsNonReusable.js";
import returnMat from "./route/returnMaterial.js";
import createSiloAdjustment from "./route/siloAdjustments/createSiloAdjustment.js";
import postComment from "./route/siloAdjustments/postComment.js";
import getStockSilo from "./route/siloAdjustments/getStockData.js";
import { migrateAdjustments } from "./controller/siloAdjustments/migrateAdjustments.js";
import getSiloAdjustments from "./route/siloAdjustments/getSiloAdjustments.js";
import { getLanesToCycleCount } from "./controller/warehouse/cycleCountChecks/cyclecountCheck.js";
import getCycleCountCheck from "./route/getCycleCountChecks.js";
import getPPOO from "./route/getPPOO.js";
import getcyclecount from "./route/getCycleCountLanes.js";
import postBulkOrders from "./route/dm/bulkOrdersIn.js";
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";
import intervalChecks from "./route/getActiveLogistics.js";
import getActiveLanes from "./route/getActiveLanes.js";
import removeAsNonReable from "./route/removeAsNonReusable.js";
import getSSCC from "./route/getSSCCNumber.js";
import getConnectionType from "./route/getSiloConnectionData.js";
import detachSilo from "./route/detachSilo.js";
import attachSilo from "./route/attachSilo.js";
import getStockSilo from "./route/siloAdjustments/getStockData.js";
import postComment from "./route/siloAdjustments/postComment.js";
const app = new OpenAPIHono();
const routes = [
comsumeMaterial,
returnMat,
comsumeMaterial,
returnMat,
// silo
createSiloAdjustment,
postComment,
getStockSilo,
getSiloAdjustments,
getConnectionType,
detachSilo,
attachSilo,
//lanes
getCycleCountCheck,
//warehouse
getPPOO,
getcyclecount,
getActiveLanes,
//DM
postBulkOrders,
standardTemplate,
postForecast,
standardForcasttemplate,
// outbound deliveries
outbound,
intervalChecks,
// silo
createSiloAdjustment,
postComment,
getStockSilo,
getSiloAdjustments,
getConnectionType,
detachSilo,
attachSilo,
//lanes
getCycleCountCheck,
//warehouse
getPPOO,
getcyclecount,
getActiveLanes,
//DM
postBulkOrders,
standardTemplate,
postForecast,
standardForcasttemplate,
// outbound deliveries
outbound,
intervalChecks,
// logisitcs
removeAsNonReable,
getSSCC,
// logisitcs
removeAsNonReable,
getSSCC,
] as const;
// app.route("/server", modules);
const appRoutes = routes.forEach((route) => {
app.route("/logistics", route);
app.route("/logistics", route);
});
setTimeout(() => {
migrateAdjustments();
runHistoricalData();
migrateAdjustments();
//runHistoricalData();
}, 120 * 1000); // starts 2 min after a server restart or crash.
/**
@@ -74,10 +72,13 @@ setTimeout(() => {
*/
setTimeout(() => {
getLanesToCycleCount();
getLanesToCycleCount();
}, 5 * 1000);
setInterval(async () => {
getLanesToCycleCount();
}, 15 * 60 * 1000);
setInterval(
async () => {
getLanesToCycleCount();
},
15 * 60 * 1000,
);
export default app;