feat(psi): more psi work

This commit is contained in:
2025-09-22 22:41:51 -05:00
parent edbc7cefd8
commit cb2e6252e0
15 changed files with 1262 additions and 28 deletions

View File

@@ -0,0 +1,47 @@
import { tryCatch } from "../../../globalUtils/tryCatch.js";
import { createLog } from "../../logger/logger.js";
import { query } from "../../sqlServer/prodSqlServer.js";
import { forecastByAvs } from "../../sqlServer/querys/dataMart/forecast.js";
// type ArticleData = {
// id: string
// }
export const getForecastByAv = async (avs: string) => {
let articles: any = [];
if (!avs) {
return {
success: false,
message: `Missing av's please send at least one over`,
data: [],
};
}
const { data, error } = (await tryCatch(
query(forecastByAvs.replace("[articles]", avs), "ForecastData by av")
)) as any;
if (error) {
createLog(
"error",
"datamart",
"datamart",
`There was an error getting the forecast info: ${JSON.stringify(
error
)}`
);
return {
success: false,
messsage: `There was an error getting the forecast info`,
data: error,
};
}
articles = data.data;
return {
success: true,
message: "Forecast Data",
data: articles,
};
};

View File

@@ -0,0 +1,84 @@
import { query } from "../../sqlServer/prodSqlServer.js";
import { deliveryByDateRangeAndAv } from "../../sqlServer/querys/dataMart/deleveryByDateRange.js";
import { addDays, format } from "date-fns";
export const getDeliveryByDateRangeAndAv = async (
avs: string,
startDate: string,
endDate: string
) => {
// const { data: plantToken, error: plantError } = await tryCatch(
// db.select().from(settings).where(eq(settings.name, "plantToken"))
// );
// if (plantError) {
// return {
// success: false,
// message: "Error getting Settings",
// data: plantError,
// };
// }
let deliverys: any = [];
let updatedQuery = deliveryByDateRangeAndAv;
// start days can be sent over
if (startDate) {
updatedQuery = updatedQuery.replaceAll("[startDate]", startDate);
} else {
updatedQuery = updatedQuery.replaceAll("[startDate]", "1990-1-1");
}
// end days can be sent over
if (endDate) {
updatedQuery = updatedQuery.replaceAll("[endDate]", endDate);
} else {
const defaultEndDate = format(
addDays(new Date(Date.now()), 5),
"yyyy-M-d"
);
updatedQuery = updatedQuery.replaceAll("[endDate]", defaultEndDate);
}
try {
const res: any = await query(
updatedQuery.replace("[articles]", avs),
"Get Delivery by date range"
);
deliverys = res.data;
//console.log(res.data);
} catch (error) {
console.log(error);
return {
success: false,
message: "All Deliveries within the range.",
data: error,
};
}
// if (!data) {
// deliverys = deliverys.splice(1000, 0);
// }
// add plant token in
// const pOrders = deliverys.map((item: any) => {
// // const dateCon = new Date(item.loadingDate).toLocaleString("en-US", {
// // month: "numeric",
// // day: "numeric",
// // year: "numeric",
// // hour: "2-digit",
// // minute: "2-digit",
// // hour12: false,
// // });
// //const dateCon = new Date(item.loadingDate).toISOString().replace("T", " ").split(".")[0];
// const dateCon = new Date(item.loadingDate).toISOString().split("T")[0];
// //const delDate = new Date(item.deliveryDate).toISOString().replace("T", " ").split(".")[0];
// const delDate = new Date(item.deliveryDate).toISOString().split("T")[0];
// return {
// plantToken: plantToken[0].value,
// ...item,
// loadingDate: dateCon,
// deliveryDate: delDate,
// };
// });
return { success: true, message: "Current open orders", data: deliverys };
};

View File

@@ -13,7 +13,8 @@ import psiArticleData from "./route/getPsiArticleData.js";
import psiPlanningData from "./route/getPsiPlanningData.js";
import psiProductionData from "./route/getPsiProductionData.js";
import psiInventory from "./route/getPsiinventory.js";
import getForecastByAv from "./route/getForecastDataByAv.js";
import getDeliveryByDateRangeAndAv from "./route/getDeliveryDateByRangeAndAv.js";
const app = new OpenAPIHono();
const routes = [
@@ -23,6 +24,8 @@ const routes = [
getCustomerInv,
getOpenOrders,
getDeliveryByDate,
getDeliveryByDateRangeAndAv,
getForecastByAv,
fakeEDI,
addressCorrections,
fifoIndex,

View File

@@ -0,0 +1,58 @@
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
import { responses } from "../../../globalUtils/routeDefs/responses.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
import { apiHit } from "../../../globalUtils/apiHits.js";
import { getDeliveryByDateRangeAndAv } from "../controller/getDeliveryByDateRangeAndAv.js";
const app = new OpenAPIHono({ strict: false });
const Body = z.object({
includeRunnningNumbers: z.string().openapi({ example: "x" }),
});
app.openapi(
createRoute({
tags: ["dataMart"],
summary: "Returns deliverys by daterange.",
method: "get",
path: "/deliverybydaterangeandav",
request: {
body: {
content: {
"application/json": { schema: Body },
},
},
},
responses: responses(),
}),
async (c) => {
const q: any = c.req.queries();
// make sure we have a vaid user being accessed thats really logged in
apiHit(c, { endpoint: "/deliverybydaterangeandav" });
const { data, error } = await tryCatch(
getDeliveryByDateRangeAndAv(
q["avs"] ? q["avs"][0] : null,
q["startDate"] ? q["startDate"][0] : null,
q["endDate"] ? q["endDate"][0] : null
)
);
if (error) {
console.log(error);
return c.json(
{
success: false,
message: "There was an error getting the deliveries.",
data: error,
},
400
);
}
return c.json({
success: data.success,
message: data.message,
data: data.data,
});
}
);
export default app;

View File

@@ -0,0 +1,61 @@
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
import { responses } from "../../../globalUtils/routeDefs/responses.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
import { apiHit } from "../../../globalUtils/apiHits.js";
import { psiGetPlanningData } from "../controller/psiGetPlanningData.js";
import { getForecastByAv } from "../controller/forecastByAvs.js";
const app = new OpenAPIHono({ strict: false });
const Body = z.object({
includeRunnningNumbers: z.string().openapi({ example: "x" }),
});
app.openapi(
createRoute({
tags: ["dataMart"],
summary: "Returns the psiarticleData.",
method: "get",
path: "/forecastbyav",
request: {
body: {
content: {
"application/json": { schema: Body },
},
},
},
responses: responses(),
}),
async (c) => {
const q: any = c.req.queries();
// make sure we have a vaid user being accessed thats really logged in
apiHit(c, { endpoint: "/forecastbyav" });
//console.log(articles["avs"][0]);
const { data, error } = await tryCatch(
getForecastByAv(q["avs"] ? q["avs"][0] : null)
);
if (error) {
console.log(error);
return c.json(
{
success: false,
message: "There was an error getting the forecast data.",
data: error,
},
400
);
}
//console.log(data);
return c.json(
{
success: data.success,
message: data.message,
data: data.data,
},
data.success ? 200 : 400
);
}
);
export default app;