refactor(invhist): reformated file
This commit is contained in:
@@ -1,115 +1,108 @@
|
|||||||
|
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) {
|
||||||
createLog(
|
createLog(
|
||||||
"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.
|
||||||
const today = new Date();
|
const today = new Date();
|
||||||
//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) {
|
||||||
createLog(
|
createLog(
|
||||||
"error",
|
"error",
|
||||||
"eom",
|
"eom",
|
||||||
"eom",
|
"eom",
|
||||||
`There was an error getting the sql data`
|
`There was an error getting the sql data`,
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inv.data.length === 0) {
|
if (inv.data.length === 0) {
|
||||||
createLog("error", "eom", "eom", inv.message);
|
createLog("error", "eom", "eom", inv.message);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
const importInv = inv.data ? inv.data : [];
|
const importInv = inv.data ? inv.data : [];
|
||||||
const eomImportData = importInv.map((i: any) => {
|
const eomImportData = importInv.map((i: any) => {
|
||||||
return {
|
return {
|
||||||
//histDate: sql`(NOW() - INTERVAL '1 day')::date`,
|
//histDate: sql`(NOW() - INTERVAL '1 day')::date`,
|
||||||
histDate: sql`(NOW())::date`,
|
histDate: sql`(NOW())::date`,
|
||||||
plantToken: plantToken[0].value,
|
plantToken: plantToken[0].value,
|
||||||
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
|
: "Item not defined",
|
||||||
)[0]?.TypeOfMaterial
|
total_QTY: i.Total_PalletQTY,
|
||||||
: "Item not defined",
|
avaliable_QTY: i.Avaliable_PalletQTY,
|
||||||
total_QTY: i.Total_PalletQTY,
|
coa_QTY: i.COA_QTY,
|
||||||
avaliable_QTY: i.Avaliable_PalletQTY,
|
held_QTY: i.Held_QTY,
|
||||||
coa_QTY: i.COA_QTY,
|
consignment: i.Consigment,
|
||||||
held_QTY: i.Held_QTY,
|
lot_Number: i.lot,
|
||||||
consignment: i.Consigment,
|
};
|
||||||
lot_Number: i.lot,
|
});
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
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) {
|
||||||
createLog(
|
createLog(
|
||||||
"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",
|
return;
|
||||||
"eom",
|
}
|
||||||
"eom",
|
} else {
|
||||||
`All data was imported succefully.`
|
createLog("info", "eom", "eom", `Yesterdays Data already in..`);
|
||||||
);
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
createLog("info", "eom", "eom", `Yesterdays Data already in..`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// do the check to delete old data
|
// do the check to delete old data
|
||||||
deleteHistory();
|
deleteHistory();
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user