refactor(invhist): reformated file

This commit is contained in:
2025-10-29 17:01:33 -05:00
parent 006ec1bfc0
commit be6510f912

View File

@@ -1,19 +1,19 @@
import { format } from "date-fns-tz";
import { sql } from "drizzle-orm"; import { sql } from "drizzle-orm";
import { db } from "../../../../database/dbclient.js"; import { db } from "../../../../database/dbclient.js";
import { invHistoricalData } from "../../../../database/schema/historicalINV.js"; import { invHistoricalData } from "../../../../database/schema/historicalINV.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 { totalInvNoRn } from "../../sqlServer/querys/dataMart/totalINV.js";
import { format } from "date-fns-tz";
import { serverSettings } from "../../server/controller/settings/getSettings.js"; import { serverSettings } from "../../server/controller/settings/getSettings.js";
import { deleteHistory } from "./removeHistorical.js"; import { query } from "../../sqlServer/prodSqlServer.js";
import { activeArticle } from "../../sqlServer/querys/dataMart/article.js"; import { activeArticle } from "../../sqlServer/querys/dataMart/article.js";
import { totalInvNoRn } from "../../sqlServer/querys/dataMart/totalINV.js";
import { deleteHistory } from "./removeHistorical.js";
export const historicalInvIMmport = async () => { export const historicalInvIMmport = async () => {
const plantToken = serverSettings.filter((n) => n.name === "plantToken"); const plantToken = serverSettings.filter((n) => n.name === "plantToken");
const { data, error } = (await tryCatch( const { data, error } = (await tryCatch(
db.select().from(invHistoricalData) db.select().from(invHistoricalData),
)) as any; )) as any;
if (error) { if (error) {
@@ -21,7 +21,7 @@ export const historicalInvIMmport = async () => {
"error", "error",
"eom", "eom",
"eom", "eom",
`There was an error getting the historical data` `There was an error getting the historical data`,
); );
} }
// check if we have data already for today this way we dont duplicate anything. // check if we have data already for today this way we dont duplicate anything.
@@ -29,13 +29,13 @@ export const historicalInvIMmport = async () => {
//today.setDate(today.getDate() - 1); //today.setDate(today.getDate() - 1);
const dateCheck = data?.filter( const dateCheck = data?.filter(
(i: any) => i.histDate === format(today, "yyyy-MM-dd") (i: any) => i.histDate === format(today, "yyyy-MM-dd"),
); );
if (dateCheck.length === 0) { if (dateCheck.length === 0) {
// get the historical data from the sql // get the historical data from the sql
const { data: inv, error: invError } = (await tryCatch( const { data: inv, error: invError } = (await tryCatch(
query(totalInvNoRn, "eom historical data") query(totalInvNoRn.replace("(6, 1)", "(6)"), "eom historical data"),
)) as any; )) as any;
if (invError) { if (invError) {
@@ -43,7 +43,7 @@ export const historicalInvIMmport = async () => {
"error", "error",
"eom", "eom",
"eom", "eom",
`There was an error getting the sql data` `There was an error getting the sql data`,
); );
return; return;
} }
@@ -54,7 +54,7 @@ export const historicalInvIMmport = async () => {
} }
const { data: articles, error: avError } = (await tryCatch( const { data: articles, error: avError } = (await tryCatch(
query(activeArticle, "Get active articles") query(activeArticle, "Get active articles"),
)) as any; )) as any;
const av = articles.data.length > 0 ? articles.data : ([] as any); const av = articles.data.length > 0 ? articles.data : ([] as any);
@@ -68,11 +68,9 @@ export const historicalInvIMmport = async () => {
article: i.av, article: i.av,
articleDescription: i.Alias, articleDescription: i.Alias,
materialType: materialType:
av.filter((a: any) => a.IdArtikelvarianten === i.av) av.filter((a: any) => a.IdArtikelvarianten === i.av).length > 0
.length > 0 ? av.filter((a: any) => a.IdArtikelvarianten === i.av)[0]
? av.filter( ?.TypeOfMaterial
(a: any) => a.IdArtikelvarianten === i.av
)[0]?.TypeOfMaterial
: "Item not defined", : "Item not defined",
total_QTY: i.Total_PalletQTY, total_QTY: i.Total_PalletQTY,
avaliable_QTY: i.Avaliable_PalletQTY, avaliable_QTY: i.Avaliable_PalletQTY,
@@ -84,7 +82,7 @@ export const historicalInvIMmport = async () => {
}); });
const { data: dataImport, error: errorImport } = await tryCatch( const { data: dataImport, error: errorImport } = await tryCatch(
db.insert(invHistoricalData).values(eomImportData) db.insert(invHistoricalData).values(eomImportData),
); );
if (errorImport) { if (errorImport) {
@@ -92,18 +90,13 @@ export const historicalInvIMmport = async () => {
"error", "error",
"eom", "eom",
"eom", "eom",
`There was an error importing all the inventory data.` `There was an error importing all the inventory data.`,
); );
return; return;
} }
if (dataImport) { if (dataImport) {
createLog( createLog("info", "eom", "eom", `All data was imported succefully.`);
"info",
"eom",
"eom",
`All data was imported succefully.`
);
return; return;
} }
} else { } else {