87 lines
2.0 KiB
TypeScript
87 lines
2.0 KiB
TypeScript
import { and, between, inArray, notInArray, sql } from "drizzle-orm";
|
|
import { db } from "../../../../database/dbclient.js";
|
|
import { invHistoricalData } from "../../../../database/schema/historicalINV.js";
|
|
import { tryCatch } from "../../../globalUtils/tryCatch.js";
|
|
import { createLog } from "../../logger/logger.js";
|
|
|
|
// type ArticleData = {
|
|
// id: string
|
|
// }
|
|
export const psiGetInventory = async (
|
|
avs: string,
|
|
startDate: string,
|
|
endDate: string,
|
|
whseToInclude: string,
|
|
exludeLanes: string
|
|
) => {
|
|
let articles: any = [];
|
|
|
|
if (!avs) {
|
|
return {
|
|
success: false,
|
|
message: `Missing av's please send at least one over`,
|
|
data: [],
|
|
};
|
|
}
|
|
|
|
const ids = avs.split(",").map((id) => id.trim());
|
|
const whse = whseToInclude
|
|
? whseToInclude
|
|
.split(",")
|
|
.map((w) => w.trim())
|
|
.filter(Boolean)
|
|
: [];
|
|
|
|
const locations = exludeLanes
|
|
? exludeLanes.split(",").map((l) => l.trim()).filter(Boolean)
|
|
: [];
|
|
|
|
const conditions = [
|
|
inArray(invHistoricalData.article, ids),
|
|
between(invHistoricalData.histDate, startDate, endDate),
|
|
];
|
|
|
|
// only add the warehouse condition if there are any whse values
|
|
if (whse.length > 0) {
|
|
console.log("adding whse to include in");
|
|
conditions.push(inArray(invHistoricalData.whseId, whse));
|
|
}
|
|
|
|
// locations we dont want in the system
|
|
if (locations.length > 0) {
|
|
console.log("adding excluded lanes in ",locations);
|
|
|
|
conditions.push(notInArray(invHistoricalData.location, locations));
|
|
}
|
|
|
|
const query = db
|
|
.select()
|
|
.from(invHistoricalData)
|
|
.where(and(...conditions));
|
|
|
|
// optional tryCatch or await as you had
|
|
const { data, error } = (await tryCatch(query)) as any;
|
|
|
|
if (error) {
|
|
createLog(
|
|
"error",
|
|
"datamart",
|
|
"datamart",
|
|
`There was an error getting the planning info: ${JSON.stringify(error)}`,
|
|
);
|
|
return {
|
|
success: false,
|
|
messsage: `There was an error getting the planning info`,
|
|
data: error,
|
|
};
|
|
}
|
|
|
|
articles = data;
|
|
console.log(articles.length);
|
|
return {
|
|
success: true,
|
|
message: "PSI planning Data",
|
|
data: articles,
|
|
};
|
|
};
|