feat(datamart): psi data has been added :D
This commit is contained in:
@@ -13,6 +13,10 @@
|
||||
*
|
||||
* when a criteria is password over we will handle it by counting how many were passed up to 3 then deal with each one respectively
|
||||
*/
|
||||
|
||||
import { and, between, inArray, notInArray } from "drizzle-orm";
|
||||
import { db } from "../db/db.controller.js";
|
||||
import { invHistoricalData } from "../db/schema/historicalInv.schema.js";
|
||||
import { prodQuery } from "../prodSql/prodSqlQuery.controller.js";
|
||||
import {
|
||||
type SqlQuery,
|
||||
@@ -29,8 +33,68 @@ type Data = {
|
||||
howManyOptionsRequired?: number;
|
||||
};
|
||||
|
||||
const lstDbRun = async (data: Data) => {
|
||||
if (data.options) {
|
||||
if (data.name === "psiInventory") {
|
||||
const ids = data.options.articles.split(",").map((id: any) => id.trim());
|
||||
const whse = data.options.whseToInclude
|
||||
? data.options.whseToInclude
|
||||
.split(",")
|
||||
.map((w: any) => w.trim())
|
||||
.filter(Boolean)
|
||||
: [];
|
||||
|
||||
const locations = data.options.exludeLanes
|
||||
? data.options.exludeLanes
|
||||
.split(",")
|
||||
.map((l: any) => l.trim())
|
||||
.filter(Boolean)
|
||||
: [];
|
||||
|
||||
const conditions = [
|
||||
inArray(invHistoricalData.article, ids),
|
||||
between(
|
||||
invHistoricalData.histDate,
|
||||
data.options.startDate,
|
||||
data.options.endDate,
|
||||
),
|
||||
];
|
||||
|
||||
// only add the warehouse condition if there are any whse values
|
||||
if (whse.length > 0) {
|
||||
conditions.push(inArray(invHistoricalData.whseId, whse));
|
||||
}
|
||||
|
||||
// locations we dont want in the system
|
||||
if (locations.length > 0) {
|
||||
conditions.push(notInArray(invHistoricalData.location, locations));
|
||||
}
|
||||
|
||||
return await db
|
||||
.select()
|
||||
.from(invHistoricalData)
|
||||
.where(and(...conditions));
|
||||
}
|
||||
}
|
||||
return [];
|
||||
};
|
||||
export const runDatamartQuery = async (data: Data) => {
|
||||
// search the query db for the query by name
|
||||
const considerLstDBRuns = ["psiInventory"];
|
||||
|
||||
if (considerLstDBRuns.includes(data.name)) {
|
||||
const lstDB = await lstDbRun(data);
|
||||
|
||||
return returnFunc({
|
||||
success: true,
|
||||
level: "info",
|
||||
module: "datamart",
|
||||
subModule: "lstDBrn",
|
||||
message: `Data for: ${data.name}`,
|
||||
data: lstDB,
|
||||
notify: false,
|
||||
});
|
||||
}
|
||||
const sqlQuery = sqlQuerySelector(`datamart.${data.name}`) as SqlQuery;
|
||||
|
||||
const getDataMartInfo = datamartData.filter((x) => x.endpoint === data.name);
|
||||
@@ -103,13 +167,26 @@ export const runDatamartQuery = async (data: Data) => {
|
||||
`${data.options.includeRunningNumbers ? `,l.RunningNumber` : `--,l.RunningNumber`}`,
|
||||
)
|
||||
.replaceAll(
|
||||
"--,l.MachineLocation,l.MachineName,l.ProductionLotRunningNumber",
|
||||
`${data.options.lots ? `,l.MachineLocation,l.MachineName,l.ProductionLotRunningNumber` : `--,l.MachineLocation,l.MachineName,l.ProductionLotRunningNumber`}`,
|
||||
"--,l.MachineLocation,l.MachineName,l.ProductionLotRunningNumber as lot",
|
||||
`${data.options.lots ? `,l.MachineLocation,l.MachineName,l.ProductionLotRunningNumber as lot` : `--,l.MachineLocation,l.MachineName,l.ProductionLotRunningNumber as lot`}`,
|
||||
)
|
||||
.replaceAll(
|
||||
"--,l.WarehouseDescription,l.LaneDescription",
|
||||
`${data.options.locations ? `,l.WarehouseDescription,l.LaneDescription` : `--,l.WarehouseDescription,l.LaneDescription`}`,
|
||||
);
|
||||
|
||||
// adding in a test for historical check.
|
||||
if (data.options.historical) {
|
||||
datamartQuery = datamartQuery
|
||||
.replace(
|
||||
"--,l.ProductionLotRunningNumber as lot,l.warehousehumanreadableid as warehouseId,l.WarehouseDescription as warehouseDescription,l.lanehumanreadableid as locationId,l.lanedescription as laneDescription",
|
||||
",l.ProductionLotRunningNumber as lot,l.warehousehumanreadableid as warehouseId,l.WarehouseDescription as warehouseDescription,l.lanehumanreadableid as locationId,l.lanedescription as laneDescription",
|
||||
)
|
||||
.replace(
|
||||
"--,l.ProductionLotRunningNumber,l.warehousehumanreadableid,l.WarehouseDescription,l.lanehumanreadableid,l.lanedescription",
|
||||
",l.ProductionLotRunningNumber,l.warehousehumanreadableid,l.WarehouseDescription,l.lanehumanreadableid,l.lanedescription",
|
||||
);
|
||||
}
|
||||
break;
|
||||
case "fakeEDIUpdate":
|
||||
datamartQuery = datamartQuery.replace(
|
||||
@@ -118,6 +195,58 @@ export const runDatamartQuery = async (data: Data) => {
|
||||
);
|
||||
|
||||
break;
|
||||
case "forecast":
|
||||
datamartQuery = datamartQuery.replace(
|
||||
"where DeliveryAddressHumanReadableId in ([customers])",
|
||||
data.options.customers
|
||||
? `where DeliveryAddressHumanReadableId in (${data.options.customers})`
|
||||
: "--where DeliveryAddressHumanReadableId in ([customers])",
|
||||
);
|
||||
|
||||
break;
|
||||
case "activeArticles2":
|
||||
datamartQuery = datamartQuery.replace(
|
||||
"and a.HumanReadableId in ([articles])",
|
||||
data.options.articles
|
||||
? `and a.HumanReadableId in (${data.options.articles})`
|
||||
: "--and a.HumanReadableId in ([articles])",
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
case "psiDeliveryData":
|
||||
datamartQuery = datamartQuery
|
||||
.replace("[startDate]", `${data.options.startDate}`)
|
||||
.replace("[endDate]", `${data.options.endDate}`)
|
||||
.replace(
|
||||
"and IdArtikelVarianten in ([articles])",
|
||||
data.options.articles
|
||||
? `and IdArtikelVarianten in (${data.options.articles})`
|
||||
: "--and IdArtikelVarianten in ([articles])",
|
||||
);
|
||||
break;
|
||||
case "productionData":
|
||||
datamartQuery = datamartQuery
|
||||
.replace("[startDate]", `${data.options.startDate}`)
|
||||
.replace("[endDate]", `${data.options.endDate}`)
|
||||
.replace(
|
||||
"and ArticleHumanReadableId in ([articles])",
|
||||
data.options.articles
|
||||
? `and ArticleHumanReadableId in (${data.options.articles})`
|
||||
: "--and ArticleHumanReadableId in ([articles])",
|
||||
);
|
||||
break;
|
||||
case "psiPlanningData":
|
||||
datamartQuery = datamartQuery
|
||||
.replace("[startDate]", `${data.options.startDate}`)
|
||||
.replace("[endDate]", `${data.options.endDate}`)
|
||||
.replace(
|
||||
"and p.IdArtikelvarianten in ([articles])",
|
||||
data.options.articles
|
||||
? `and p.IdArtikelvarianten in (${data.options.articles})`
|
||||
: "--and p.IdArtikelvarianten in ([articles])",
|
||||
);
|
||||
break;
|
||||
default:
|
||||
return returnFunc({
|
||||
success: false,
|
||||
|
||||
Reference in New Issue
Block a user