feat(missing inv): adding a way to check for missing data in case it dose pull on the correct days
This commit is contained in:
22
LogisticsSupportTool_API_DOCS/LstV2/EOM/Get Inv.bru
Normal file
22
LogisticsSupportTool_API_DOCS/LstV2/EOM/Get Inv.bru
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
meta {
|
||||||
|
name: Get Inv
|
||||||
|
type: http
|
||||||
|
seq: 2
|
||||||
|
}
|
||||||
|
|
||||||
|
get {
|
||||||
|
url: {{urlv2}}/api/eom/productionconsumption?startDate=9/1/2025&endDate=9/30/2025&includePlantToken=true
|
||||||
|
body: none
|
||||||
|
auth: inherit
|
||||||
|
}
|
||||||
|
|
||||||
|
params:query {
|
||||||
|
startDate: 9/1/2025
|
||||||
|
endDate: 9/30/2025
|
||||||
|
includePlantToken: true
|
||||||
|
}
|
||||||
|
|
||||||
|
settings {
|
||||||
|
encodeUrl: true
|
||||||
|
timeout: 0
|
||||||
|
}
|
||||||
@@ -1,139 +0,0 @@
|
|||||||
// import cron from "node-cron";
|
|
||||||
// import {runQuery, prisma, totalInvNoRn, activeArticle, getShiftTime, historicalInv} from "database";
|
|
||||||
// import {createLog} from "logging";
|
|
||||||
// import {deleteHistory} from "./deleteHistory.js";
|
|
||||||
|
|
||||||
// export const historyInv = async (date) => {
|
|
||||||
// //console.log(date);
|
|
||||||
// if (!date) {
|
|
||||||
// return `Missing Data`;
|
|
||||||
// }
|
|
||||||
// // date should be sent over as a string IE: 2024-01-01
|
|
||||||
// let inv = [];
|
|
||||||
// try {
|
|
||||||
// inv = await prisma.historyInventory.findMany({where: {histDate: date}});
|
|
||||||
// console.log(inv.length);
|
|
||||||
// // if the date returns nothing we need to pull the historical data
|
|
||||||
// if (inv.length === 0) {
|
|
||||||
// const result = await prisma.settings.findFirst({where: {name: "plantToken"}});
|
|
||||||
// try {
|
|
||||||
// const plantUpdate = historicalInv.replaceAll("test1", result.value);
|
|
||||||
// const queryDate = plantUpdate.replaceAll("[date]", date);
|
|
||||||
// inv = await runQuery(queryDate, "Get histical inv");
|
|
||||||
|
|
||||||
// return inv;
|
|
||||||
// } catch (error) {
|
|
||||||
// createLog("general/eom", "error", "There was an error getting the historical inv.");
|
|
||||||
// return error;
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// return inv;
|
|
||||||
// }
|
|
||||||
// //return inv;
|
|
||||||
// } catch (error) {
|
|
||||||
// console.log(error);
|
|
||||||
// return error;
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
// // start the cron job for getting the hostrical inv based on the plants shift time
|
|
||||||
// export const startCronHist = () => {
|
|
||||||
// let shiftTime = ["06", "00", "00"];
|
|
||||||
// const startProcess = async () => {
|
|
||||||
// let inv = [];
|
|
||||||
// let articles = [];
|
|
||||||
// let plantToken = "test1";
|
|
||||||
// const date = new Date();
|
|
||||||
// const dateString = date.toISOString().split("T")[0];
|
|
||||||
// date.setDate(date.getDate() - 30);
|
|
||||||
// const oldDate = date.toISOString().split("T")[0];
|
|
||||||
|
|
||||||
// // checking if even need to run this
|
|
||||||
// // before adding more make sure we dont already have data
|
|
||||||
// const checkInv = await prisma.historyInventory.findFirst({where: {histDate: dateString}});
|
|
||||||
// if (checkInv) {
|
|
||||||
// createLog(
|
|
||||||
// "general/eom",
|
|
||||||
// "warn",
|
|
||||||
// `There seems to already be inventory added for ${dateString}, no new data will be added`
|
|
||||||
// );
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
// // get plant token
|
|
||||||
// try {
|
|
||||||
// const result = await prisma.settings.findFirst({where: {name: "plantToken"}});
|
|
||||||
// plantToken = result.value;
|
|
||||||
// } catch (error) {
|
|
||||||
// createLog("general/eom", "error", "failed to get planttoken");
|
|
||||||
// }
|
|
||||||
// //get shift time
|
|
||||||
// try {
|
|
||||||
// const result = await runQuery(getShiftTime.replaceAll("test1", plantToken), "GettingShift time");
|
|
||||||
// shiftTime = result[0].shiftStartTime.split(":");
|
|
||||||
// } catch (error) {
|
|
||||||
// createLog("general/eom", "error", `Error running getShift Query: ${error}`);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // get inventory
|
|
||||||
// try {
|
|
||||||
// const result = await runQuery(totalInvNoRn.replaceAll("test1", plantToken), "getting inventory");
|
|
||||||
// inv = result;
|
|
||||||
// } catch (error) {
|
|
||||||
// createLog("general/eom", "error", `Error running get inventory Query: ${error}`);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // get active articles
|
|
||||||
// try {
|
|
||||||
// const result = await runQuery(activeArticle.replaceAll("test1", plantToken), "Get active articles");
|
|
||||||
// articles = result;
|
|
||||||
// } catch (error) {
|
|
||||||
// createLog("general/eom", "error", `Error running get article: ${error}`);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// //add the inventory to the historical table
|
|
||||||
// try {
|
|
||||||
// let hist = Object.entries(inv).map(([key, value]) => {
|
|
||||||
// // remove the values we dont want in the historical view
|
|
||||||
// const {total_Pallets, avalible_Pallets, coa_Pallets, held_Pallets, ...histData} = value;
|
|
||||||
|
|
||||||
// // get av tyep
|
|
||||||
// const avType = articles.filter((a) => (a.IdArtikelvarianten = inv[key].av))[0].TypeOfMaterial;
|
|
||||||
// // add in the new fields
|
|
||||||
// const hist = {
|
|
||||||
// ...histData,
|
|
||||||
// histDate: dateString, //new Date(Date.now()).toISOString().split("T")[0],
|
|
||||||
// avType,
|
|
||||||
// };
|
|
||||||
// return hist;
|
|
||||||
// });
|
|
||||||
|
|
||||||
// try {
|
|
||||||
// const addHistData = await prisma.historyInventory.createMany({data: hist});
|
|
||||||
// createLog(
|
|
||||||
// "general/eom",
|
|
||||||
// "info",
|
|
||||||
// `${addHistData.count} were just added to the historical inventory for date ${dateString}`
|
|
||||||
// );
|
|
||||||
// } catch (error) {
|
|
||||||
// createLog("general/eom", "error", `Adding new historical inventory error: ${error}`);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // delete the older inventory
|
|
||||||
// deleteHistory(oldDate);
|
|
||||||
// } catch (error) {
|
|
||||||
// createLog("general/eom", "error", `Adding new historical inventory error: ${error}`);
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
// // actaully run the process once after restaart just to make sure we have inventory
|
|
||||||
// startProcess();
|
|
||||||
|
|
||||||
// // setup the cron stuff
|
|
||||||
// const startHour = shiftTime[0];
|
|
||||||
// const startMin = shiftTime[1];
|
|
||||||
// createLog("general/eom", "info", `Historical Data will run at ${shiftTime[0]}:${shiftTime[1]} daily`);
|
|
||||||
// cron.schedule(`${startMin} ${startHour} * * *`, () => {
|
|
||||||
// createLog("general/eom", "info", "Running historical invnetory.");
|
|
||||||
// startProcess();
|
|
||||||
// });
|
|
||||||
// };
|
|
||||||
|
|||||||
@@ -1,55 +1,142 @@
|
|||||||
import { eq } from "drizzle-orm";
|
import { format } from "date-fns";
|
||||||
|
import { eq, sql } from "drizzle-orm";
|
||||||
|
import { success } from "zod/v4";
|
||||||
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 { format } from "date-fns";
|
|
||||||
import { settings } from "../../../../database/schema/settings.js";
|
import { settings } from "../../../../database/schema/settings.js";
|
||||||
|
import { tryCatch } from "../../../globalUtils/tryCatch.js";
|
||||||
|
import { createLog } from "../../logger/logger.js";
|
||||||
|
import { serverSettings } from "../../server/controller/settings/getSettings.js";
|
||||||
|
import { query } from "../../sqlServer/prodSqlServer.js";
|
||||||
|
import { activeArticle } from "../../sqlServer/querys/dataMart/article.js";
|
||||||
|
import { missingHistInv } from "../../sqlServer/querys/eom/missingHistInv.js";
|
||||||
|
|
||||||
|
const missingHistoricalData = async (date: string) => {
|
||||||
|
const plantToken = serverSettings.filter((n) => n.name === "plantToken");
|
||||||
|
const { data: inv, error: invError } = (await tryCatch(
|
||||||
|
query(
|
||||||
|
missingHistInv.replace("[eomHistDate]", date),
|
||||||
|
"eom missing historical data",
|
||||||
|
),
|
||||||
|
)) as any;
|
||||||
|
|
||||||
|
if (invError) {
|
||||||
|
createLog("error", "eom", "eom", `There was an error getting the sql data`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data: articles, error: avError } = (await tryCatch(
|
||||||
|
query(activeArticle, "Get active articles"),
|
||||||
|
)) as any;
|
||||||
|
|
||||||
|
const av = articles.data.length > 0 ? articles.data : ([] as any);
|
||||||
|
|
||||||
|
const importInv = inv.data ? inv.data : [];
|
||||||
|
|
||||||
|
const remap = importInv.map((i: any) => {
|
||||||
|
return {
|
||||||
|
//histDate: sql`(NOW() - INTERVAL '1 day')::date`,
|
||||||
|
histDate: date,
|
||||||
|
plantToken: plantToken[0].value,
|
||||||
|
article: i.article,
|
||||||
|
articleDescription: i.articleDescription,
|
||||||
|
materialType:
|
||||||
|
av.filter((a: any) => a.IdArtikelvarianten === i.article).length > 0
|
||||||
|
? av.filter((a: any) => a.IdArtikelvarianten === i.article)[0]
|
||||||
|
?.TypeOfMaterial
|
||||||
|
: "Item not defined",
|
||||||
|
total_QTY: i.total_QTY,
|
||||||
|
avaliable_QTY: 0,
|
||||||
|
coa_QTY: 0,
|
||||||
|
held_QTY: 0,
|
||||||
|
consignment: 0,
|
||||||
|
lot_Number: 0,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
const { data: dataImport, error: errorImport } = await tryCatch(
|
||||||
|
db.insert(invHistoricalData).values(remap),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (errorImport) {
|
||||||
|
console.log(errorImport);
|
||||||
|
createLog(
|
||||||
|
"error",
|
||||||
|
"eom",
|
||||||
|
"eom",
|
||||||
|
`There was an error importing all the inventory data.`,
|
||||||
|
);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dataImport) {
|
||||||
|
createLog("info", "eom", "eom", `All data was imported successfully.`);
|
||||||
|
|
||||||
|
return remap;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export const historicalInvByDate = async (
|
export const historicalInvByDate = async (
|
||||||
date: string,
|
date: string,
|
||||||
includePlantToken: boolean = false
|
includePlantToken: boolean = false,
|
||||||
) => {
|
) => {
|
||||||
const histDate = new Date(date);
|
const histDate = new Date(date);
|
||||||
|
|
||||||
const { data, error } = (await tryCatch(
|
const { data, error } = (await tryCatch(
|
||||||
db
|
db
|
||||||
.select()
|
.select()
|
||||||
.from(invHistoricalData)
|
.from(invHistoricalData)
|
||||||
.where(
|
.where(eq(invHistoricalData.histDate, format(histDate, "yyyy-MM-dd"))),
|
||||||
eq(invHistoricalData.histDate, format(histDate, "yyyy-MM-dd"))
|
)) as any;
|
||||||
)
|
|
||||||
)) as any;
|
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
message: "There was an error with getting the inventory",
|
message: "There was an error with getting the inventory",
|
||||||
data: error,
|
data: error,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (includePlantToken) {
|
if (includePlantToken) {
|
||||||
const { data: s, error: se } = (await tryCatch(
|
const { data: s, error: se } = (await tryCatch(
|
||||||
db.select().from(settings).where(eq(settings.name, "plantToken"))
|
db.select().from(settings).where(eq(settings.name, "plantToken")),
|
||||||
)) as any;
|
)) as any;
|
||||||
|
|
||||||
if (se) {
|
if (se) {
|
||||||
console.log("Error getting articles");
|
console.log("Error getting articles");
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
if (data.length === 0) {
|
||||||
success: true,
|
// pass over the old lame
|
||||||
message: `Historical inventory for ${date}`,
|
|
||||||
data: data.map((n: any) => {
|
return {
|
||||||
return { plantToken: s[0].value, ...n };
|
success: true,
|
||||||
}),
|
message: `Historical inventory is missing for ${date}`,
|
||||||
};
|
data: await missingHistoricalData(date),
|
||||||
} else {
|
};
|
||||||
return {
|
}
|
||||||
success: true,
|
return {
|
||||||
message: `Historical inventory for ${date}`,
|
success: true,
|
||||||
data: data,
|
message: `Historical inventory for ${date}`,
|
||||||
};
|
data: data.map((n: any) => {
|
||||||
}
|
return { plantToken: s[0].value, ...n };
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
if (data.length === 0) {
|
||||||
|
// pass over the old lame
|
||||||
|
//const oldData = missingHistoricalData(date)
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
message: `Historical inventory is missing for ${date}`,
|
||||||
|
data: await missingHistoricalData(date),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
message: `Historical inventory for ${date}`,
|
||||||
|
data: data,
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,45 +5,45 @@ import { historicalInvByDate } from "../controller/getHistoricalInvByDate.js";
|
|||||||
|
|
||||||
const app = new OpenAPIHono({ strict: false });
|
const app = new OpenAPIHono({ strict: false });
|
||||||
const EomStat = z.object({
|
const EomStat = z.object({
|
||||||
plant: z.string().openapi({ example: "Salt Lake City" }),
|
plant: z.string().openapi({ example: "Salt Lake City" }),
|
||||||
userRan: z.string().openapi({ example: "smith034" }),
|
userRan: z.string().openapi({ example: "smith034" }),
|
||||||
eomSheetVersion: z.string().openapi({ example: "0.0.223" }),
|
eomSheetVersion: z.string().openapi({ example: "0.0.223" }),
|
||||||
});
|
});
|
||||||
|
|
||||||
app.openapi(
|
app.openapi(
|
||||||
createRoute({
|
createRoute({
|
||||||
tags: ["eom"],
|
tags: ["eom"],
|
||||||
summary: "Gets History Data by date.",
|
summary: "Gets History Data by date.",
|
||||||
method: "get",
|
method: "get",
|
||||||
path: "/histinv",
|
path: "/histinv",
|
||||||
responses: responses(),
|
responses: responses(),
|
||||||
}),
|
}),
|
||||||
async (c) => {
|
async (c) => {
|
||||||
//const body = await c.req.json();
|
//const body = await c.req.json();
|
||||||
// make sure we have a vaid user being accessed thats really logged in
|
// make sure we have a vaid user being accessed thats really logged in
|
||||||
const q: any = c.req.queries();
|
const q: any = c.req.queries();
|
||||||
|
|
||||||
apiHit(c, { endpoint: "/histinv" });
|
apiHit(c, { endpoint: "/histinv" });
|
||||||
try {
|
try {
|
||||||
const res = await historicalInvByDate(
|
const res = await historicalInvByDate(
|
||||||
q["month"] ? q["month"][0] : null,
|
q["month"] ? q["month"][0] : null,
|
||||||
q["includePlantToken"] ? true : false
|
q["includePlantToken"] ? true : false,
|
||||||
);
|
);
|
||||||
|
|
||||||
return c.json(
|
return c.json(
|
||||||
{ success: res.success, message: res.message, data: res.data },
|
{ success: res.success, message: res.message, data: res.data },
|
||||||
200
|
200,
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return c.json(
|
return c.json(
|
||||||
{
|
{
|
||||||
success: false,
|
success: false,
|
||||||
message: "There was an error posting the eom stat.",
|
message: "There was an error getting the eom data.",
|
||||||
data: error,
|
data: error,
|
||||||
},
|
},
|
||||||
400
|
400,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
export default app;
|
export default app;
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import { serverSettings } from "../../server/controller/settings/getSettings.js"
|
|||||||
import { query } from "../../sqlServer/prodSqlServer.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 { 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");
|
||||||
|
|||||||
@@ -20,32 +20,32 @@ import { createLog } from "../../logger/logger.js";
|
|||||||
// }
|
// }
|
||||||
// };
|
// };
|
||||||
|
|
||||||
export const deleteHistory = async () => {
|
// export const deleteHistory = async () => {
|
||||||
const { data, error } = await tryCatch(
|
// const { data, error } = await tryCatch(
|
||||||
db
|
// db
|
||||||
.delete(invHistoricalData)
|
// .delete(invHistoricalData)
|
||||||
.where(
|
// .where(
|
||||||
lte(
|
// lte(
|
||||||
invHistoricalData.histDate,
|
// invHistoricalData.histDate,
|
||||||
sql`(NOW() - INTERVAL '365 day')::date`
|
// sql`(NOW() - INTERVAL '365 day')::date`
|
||||||
)
|
// )
|
||||||
)
|
// )
|
||||||
);
|
// );
|
||||||
|
|
||||||
if (error) {
|
// if (error) {
|
||||||
createLog(
|
// createLog(
|
||||||
"error",
|
// "error",
|
||||||
"eom",
|
// "eom",
|
||||||
"eom",
|
// "eom",
|
||||||
"There was an error deleting the historical data."
|
// "There was an error deleting the historical data."
|
||||||
);
|
// );
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
createLog(
|
// createLog(
|
||||||
"info",
|
// "info",
|
||||||
"eom",
|
// "eom",
|
||||||
"eom",
|
// "eom",
|
||||||
"Data older than 45 days has been deleted."
|
// "Data older than 45 days has been deleted."
|
||||||
);
|
// );
|
||||||
};
|
// };
|
||||||
|
|||||||
18
lstV2/server/services/sqlServer/querys/eom/missingHistInv.ts
Normal file
18
lstV2/server/services/sqlServer/querys/eom/missingHistInv.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
export const missingHistInv = `
|
||||||
|
use AlplaPROD_test1
|
||||||
|
declare @histDate date = '[eomHistDate]'
|
||||||
|
SELECT
|
||||||
|
IdArtikelVarianten AS article,
|
||||||
|
ArtikelVariantenBez AS articleDescription,
|
||||||
|
ArtikelVariantenTypBez,
|
||||||
|
IdArtikelVariantenTyp,
|
||||||
|
Datum,
|
||||||
|
Menge AS total_QTY,
|
||||||
|
x.IdWarenlager,
|
||||||
|
w.LagerTyp,
|
||||||
|
w.Bezeichnung
|
||||||
|
FROM dbo.V_LagerBestandsHistorie (nolock) x
|
||||||
|
join
|
||||||
|
dbo.T_WarenLager as w on x.IdWarenlager=w.IdWarenLager
|
||||||
|
where w.LagerTyp NOT IN ('4','5','6') AND CONVERT(DATE, Datum) LIKE @histDate
|
||||||
|
`;
|
||||||
Reference in New Issue
Block a user