feat(eom): lastSales, lastPurch added to be pulled with new template
This commit is contained in:
32
server/services/eom/controller/getHistoricalInvByDate.ts
Normal file
32
server/services/eom/controller/getHistoricalInvByDate.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../../../../database/dbclient.js";
|
||||
import { invHistoricalData } from "../../../../database/schema/historicalINV.js";
|
||||
import { tryCatch } from "../../../globalUtils/tryCatch.js";
|
||||
import { format } from "date-fns";
|
||||
|
||||
export const historicalInvByDate = async (date: string) => {
|
||||
const histDate = new Date(date);
|
||||
|
||||
const { data, error } = (await tryCatch(
|
||||
db
|
||||
.select()
|
||||
.from(invHistoricalData)
|
||||
.where(
|
||||
eq(invHistoricalData.histDate, format(histDate, "yyyy-MM-dd"))
|
||||
)
|
||||
)) as any;
|
||||
|
||||
if (error) {
|
||||
return {
|
||||
success: false,
|
||||
message: "There was an error with getting the inventory",
|
||||
data: error,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `Historical inventory for ${date}`,
|
||||
data: data,
|
||||
};
|
||||
};
|
||||
24
server/services/eom/controller/getLastPurchasesPrice.ts
Normal file
24
server/services/eom/controller/getLastPurchasesPrice.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { tryCatch } from "../../../globalUtils/tryCatch.js";
|
||||
import { format } from "date-fns";
|
||||
import { query } from "../../sqlServer/prodSqlServer.js";
|
||||
import { lastPurchasePrice } from "../../sqlServer/querys/eom/lstPurchasePrice.js";
|
||||
|
||||
export const lastPurchase = async () => {
|
||||
const { data, error } = (await tryCatch(
|
||||
query(lastPurchasePrice, "Last purchase price")
|
||||
)) as any;
|
||||
|
||||
if (error) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Error getting the last purchase price",
|
||||
data: error,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `Last purchase price for all av in the last 5 years`,
|
||||
data: data.data,
|
||||
};
|
||||
};
|
||||
23
server/services/eom/controller/getLastestSalesPrice.ts
Normal file
23
server/services/eom/controller/getLastestSalesPrice.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { tryCatch } from "../../../globalUtils/tryCatch.js";
|
||||
import { query } from "../../sqlServer/prodSqlServer.js";
|
||||
import { lastSalesPriceCheck } from "../../sqlServer/querys/eom/lastSalesprice.js";
|
||||
|
||||
export const lastSales = async (date: string) => {
|
||||
const { data, error } = (await tryCatch(
|
||||
query(lastSalesPriceCheck.replace("[date]", date), "Last sales price")
|
||||
)) as any;
|
||||
|
||||
if (error) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Error getting the last sales price",
|
||||
data: error,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `Last sales price for all av in the last 5 years`,
|
||||
data: data.data,
|
||||
};
|
||||
};
|
||||
@@ -10,7 +10,10 @@ import { tryCatch } from "../../globalUtils/tryCatch.js";
|
||||
import { query } from "../sqlServer/prodSqlServer.js";
|
||||
import { shiftChange } from "../sqlServer/querys/misc/shiftChange.js";
|
||||
import { createLog } from "../logger/logger.js";
|
||||
const routes = [stats, history] as const;
|
||||
import lastPurch from "./route/getLastPurchPrice.js";
|
||||
import lastSales from "./route/getLastSalesPrice.js";
|
||||
|
||||
const routes = [stats, history, lastPurch, lastSales] as const;
|
||||
|
||||
const appRoutes = routes.forEach((route) => {
|
||||
app.route("/eom", route);
|
||||
|
||||
41
server/services/eom/route/getLastPurchPrice.ts
Normal file
41
server/services/eom/route/getLastPurchPrice.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
||||
import { apiHit } from "../../../globalUtils/apiHits.js";
|
||||
import { responses } from "../../../globalUtils/routeDefs/responses.js";
|
||||
|
||||
import { lastPurchase } from "../controller/getLastPurchasesPrice.js";
|
||||
|
||||
const app = new OpenAPIHono({ strict: false });
|
||||
|
||||
app.openapi(
|
||||
createRoute({
|
||||
tags: ["eom"],
|
||||
summary: "Returns last sales price.",
|
||||
method: "get",
|
||||
path: "/lastpurchprice",
|
||||
responses: responses(),
|
||||
}),
|
||||
async (c) => {
|
||||
//const body = await c.req.json();
|
||||
// make sure we have a vaid user being accessed thats really logged in
|
||||
|
||||
apiHit(c, { endpoint: "/lastpurchprice" });
|
||||
try {
|
||||
const res = await lastPurchase();
|
||||
|
||||
return c.json(
|
||||
{ success: res.success, message: res.message, data: res.data },
|
||||
200
|
||||
);
|
||||
} catch (error) {
|
||||
return c.json(
|
||||
{
|
||||
success: false,
|
||||
message: "There was an error posting the eom stat.",
|
||||
data: error,
|
||||
},
|
||||
400
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
export default app;
|
||||
43
server/services/eom/route/getLastSalesPrice.ts
Normal file
43
server/services/eom/route/getLastSalesPrice.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
||||
import { apiHit } from "../../../globalUtils/apiHits.js";
|
||||
import { responses } from "../../../globalUtils/routeDefs/responses.js";
|
||||
|
||||
import { lastPurchase } from "../controller/getLastPurchasesPrice.js";
|
||||
import { lastSales } from "../controller/getLastestSalesPrice.js";
|
||||
|
||||
const app = new OpenAPIHono({ strict: false });
|
||||
|
||||
app.openapi(
|
||||
createRoute({
|
||||
tags: ["eom"],
|
||||
summary: "Returns last sales price.",
|
||||
method: "get",
|
||||
path: "/lastsalesprice",
|
||||
responses: responses(),
|
||||
}),
|
||||
async (c) => {
|
||||
//const body = await c.req.json();
|
||||
const month: string = c.req.query("month") ?? "";
|
||||
// make sure we have a vaid user being accessed thats really logged in
|
||||
|
||||
apiHit(c, { endpoint: "/lastsalesprice" });
|
||||
try {
|
||||
const res = await lastSales(month);
|
||||
|
||||
return c.json(
|
||||
{ success: res.success, message: res.message, data: res.data },
|
||||
200
|
||||
);
|
||||
} catch (error) {
|
||||
return c.json(
|
||||
{
|
||||
success: false,
|
||||
message: "There was an error posting the eom stat.",
|
||||
data: error,
|
||||
},
|
||||
400
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
export default app;
|
||||
@@ -1,6 +1,7 @@
|
||||
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
||||
import { apiHit } from "../../../globalUtils/apiHits.js";
|
||||
import { responses } from "../../../globalUtils/routeDefs/responses.js";
|
||||
import { historicalInvByDate } from "../controller/getHistoricalInvByDate.js";
|
||||
|
||||
const app = new OpenAPIHono({ strict: false });
|
||||
const EomStat = z.object({
|
||||
@@ -12,20 +13,24 @@ const EomStat = z.object({
|
||||
app.openapi(
|
||||
createRoute({
|
||||
tags: ["eom"],
|
||||
summary: "Gets the correct eom history.",
|
||||
method: "post",
|
||||
summary: "Gets History Data by date.",
|
||||
method: "get",
|
||||
path: "/histinv",
|
||||
request: {
|
||||
params: EomStat,
|
||||
},
|
||||
responses: responses(),
|
||||
}),
|
||||
async (c) => {
|
||||
//const body = await c.req.json();
|
||||
// make sure we have a vaid user being accessed thats really logged in
|
||||
const month: string = c.req.query("month") ?? "";
|
||||
|
||||
apiHit(c, { endpoint: "/histinv" });
|
||||
try {
|
||||
return c.json({ success: true, message: "", data: [] }, 200);
|
||||
const res = await historicalInvByDate(month);
|
||||
|
||||
return c.json(
|
||||
{ success: res.success, message: res.message, data: res.data },
|
||||
200
|
||||
);
|
||||
} catch (error) {
|
||||
return c.json(
|
||||
{
|
||||
|
||||
@@ -8,6 +8,7 @@ import { totalInvNoRn } from "../../sqlServer/querys/dataMart/totalINV.js";
|
||||
import { format } from "date-fns-tz";
|
||||
import { serverSettings } from "../../server/controller/settings/getSettings.js";
|
||||
import { deleteHistory } from "./removeHistorical.js";
|
||||
import { activeArticle } from "../../sqlServer/querys/dataMart/article.js";
|
||||
|
||||
export const historicalInvIMmport = async () => {
|
||||
const plantToken = serverSettings.filter((n) => n.name === "plantToken");
|
||||
@@ -52,6 +53,12 @@ export const historicalInvIMmport = async () => {
|
||||
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 eomImportData = importInv.map((i: any) => {
|
||||
return {
|
||||
@@ -59,7 +66,13 @@ export const historicalInvIMmport = async () => {
|
||||
plantToken: plantToken[0].value,
|
||||
article: i.av,
|
||||
articleDescription: i.Alias,
|
||||
materialType: "",
|
||||
materialType:
|
||||
av.filter((a: any) => a.IdArtikelvarianten === i.av)
|
||||
.length > 0
|
||||
? av.filter(
|
||||
(a: any) => a.IdArtikelvarianten === i.av
|
||||
)[0]?.TypeOfMaterial
|
||||
: "Item not defined",
|
||||
total_QTY: i.Total_PalletQTY,
|
||||
avaliable_QTY: i.Avaliable_PalletQTY,
|
||||
coa_QTY: i.COA_QTY,
|
||||
|
||||
Reference in New Issue
Block a user