test(silo): backend silo stuff

This commit is contained in:
2025-04-08 06:48:12 -05:00
parent b630bae50d
commit b4a4dfcb75
9 changed files with 227 additions and 13 deletions

View File

@@ -0,0 +1,26 @@
import { queryOptions } from "@tanstack/react-query";
import axios from "axios";
export function getStockSilo() {
const token = localStorage.getItem("auth_token");
return queryOptions({
queryKey: ["getUsers"],
queryFn: () => fetchStockSilo(token),
enabled: !!token, // Prevents query if token is null
staleTime: 1000,
//refetchInterval: 2 * 2000,
refetchOnWindowFocus: true,
});
}
const fetchStockSilo = async (token: string | null) => {
const { data } = await axios.get(`/api/logistics/getsilosdjustment`, {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
});
// if we are not localhost ignore the devDir setting.
//const url: string = window.location.host.split(":")[0];
return data.data ?? [];
};

View File

@@ -4,7 +4,6 @@ import getArticles from "./route/getActiveArticles.js";
import currentInv from "./route/getInventory.js";
import getCustomerInv from "./route/getCustomerInv.js";
import getOpenOrders from "./route/getOpenOrders.js";
import siloAdjustments from "./route/getSiloAdjustments.js";
const app = new OpenAPIHono();
@@ -14,7 +13,6 @@ const routes = [
currentInv,
getCustomerInv,
getOpenOrders,
siloAdjustments,
] as const;
const appRoutes = routes.forEach((route) => {

View File

@@ -72,7 +72,7 @@ const current: any = [
// },
{
name: "getSiloAdjustment",
endpoint: "/api/v1/warehouse/getSiloAdjustment",
endpoint: "/api/logistics/getsilosdjustment",
// description: "Returns all inventory, by default excludes running numebrs, also excludes inv locations.",
description:
"Returns all siloadjustments in selected date range IE: 1/1/2025 to 1/31/2025",

View File

@@ -0,0 +1,79 @@
import { between, desc, gte, lte } from "drizzle-orm";
import { db } from "../../../../../database/dbclient.js";
import { siloAdjustments } from "../../../../../database/schema/siloAdjustments.js";
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
export const getSiloAdjustments = async (startDate: any, endDate: any) => {
/**
* Returns silo adjustments by date or all
*/
if (startDate && endDate) {
const { data: adjRange, error: adjRangeError } = await tryCatch(
db
.select()
.from(siloAdjustments)
.where(
between(
siloAdjustments.dateAdjusted,
new Date(startDate),
new Date(endDate)
)
)
.orderBy(desc(siloAdjustments.dateAdjusted))
);
if (adjRangeError) {
return {
success: false,
message: "Error getting silo adjustments.",
adjRangeError,
};
}
return {
success: true,
message: "Silo adjustment data.",
data: adjRange,
};
}
if (startDate) {
const { data: adjRange, error: adjRangeError } = await tryCatch(
db
.select()
.from(siloAdjustments)
.where(gte(siloAdjustments.dateAdjusted, new Date(startDate)))
.orderBy(desc(siloAdjustments.dateAdjusted))
);
if (adjRangeError)
return {
success: false,
message: "Error getting silo adjustments.",
adjRangeError,
};
return {
success: true,
message: "Silo adjustment data.",
data: adjRange,
};
}
const { data: adjRange, error: adjRangeError } = await tryCatch(
db
.select()
.from(siloAdjustments)
.orderBy(desc(siloAdjustments.dateAdjusted))
);
if (adjRangeError)
return {
success: false,
message: "Error getting silo adjustments.",
adjRangeError,
};
return {
success: true,
message: "Silo adjustment data.",
data: adjRange,
};
};

View File

@@ -0,0 +1,91 @@
/**
* 1. Get the silo adjustments from lstv1
* 2. Build the new data set to match the new system
* 3. insert the new values
*/
import axios from "axios";
import { db } from "../../../../../database/dbclient.js";
import { siloAdjustments } from "../../../../../database/schema/siloAdjustments.js";
import { createLog } from "../../../logger/logger.js";
import { delay } from "../../../../globalUtils/delay.js";
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
import { settings } from "../../../../../database/schema/settings.js";
import { eq } from "drizzle-orm";
export const migrateAdjustments = async () => {
/**
* Migrates the silo adjustments from v1 to v2
*/
const { data, error } = await tryCatch(db.select().from(settings));
if (error) {
createLog("error", "silo", "logistics", "Getting settings.");
return;
}
const migrationCompleted = data?.filter(
(n) => n.name === "siloAdjMigrations"
);
const server = data?.filter((n) => n.name === "v1SysServer");
const port = data?.filter((n) => n.name === "v1SysPort");
createLog("info", "silo", "logistics", "Getting v1 silo data.");
if (migrationCompleted[0]?.value === "1") {
createLog(
"info",
"silo",
"logistics",
"Migrations have already been completed on this server."
);
return;
}
const { data: s, error: siloError } = await tryCatch(
axios.get(
`http://${server[0].value}:${port[0].value}/api/v1/warehouse/getSilosAdjustment?startDate=1/1/2020&endDate=4/1/2026`
)
);
if (siloError) {
createLog("error", "silo", "logistics", "Getting settings.");
return;
}
/**
* Migrate all the silo adjustments :D
*/
const silo: any = s?.data;
createLog("info", "silo", "logistics", "Starting migration.");
for (let i = 0; i < silo.length; i++) {
const migrate = await db.insert(siloAdjustments).values({
warehouseID: silo[0].warehouseID,
locationID: silo[0].locationID,
currentStockLevel: silo[0].currentStockLevel,
newLevel: silo[0].newLevel,
dateAdjusted: new Date(silo[0].dateAdjusted),
lastDateAdjusted: new Date(silo[0].lastDateAdjusted),
add_user: silo[0].add_user,
});
createLog(
"info",
"silo",
"logistics",
`Migrations for Date ${silo[0].dateAdjusted} on silo: ${silo[0].locationID}`
);
await delay(120);
}
/**
* change the migration setting to be completed
*/
await db
.update(settings)
.set({ value: "1" })
.where(eq(settings.name, "siloAdjMigrations"));
createLog("info", "silo", "logistics", "Migration completed.");
};
migrateAdjustments();

View File

@@ -5,6 +5,8 @@ import returnMat from "./route/returnMaterial.js";
import createSiloAdjustment from "./route/siloAdjustments/createSiloAdjustment.js";
import postComment from "./route/siloAdjustments/postComment.js";
import getStockSilo from "./route/siloAdjustments/getStockData.js";
import { migrateAdjustments } from "./controller/siloAdjustments/migrateAdjustments.js";
import getSiloAdjustments from "./route/siloAdjustments/getSiloAdjustments.js";
const app = new OpenAPIHono();
@@ -16,6 +18,7 @@ const routes = [
createSiloAdjustment,
postComment,
getStockSilo,
getSiloAdjustments,
] as const;
// app.route("/server", modules);
@@ -23,4 +26,8 @@ const appRoutes = routes.forEach((route) => {
app.route("/logistics", route);
});
setTimeout(() => {
migrateAdjustments();
}, 10 * 1000);
export default app;

View File

@@ -1,8 +1,9 @@
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
import { responses } from "../../../globalUtils/routeDefs/responses.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
import { getOpenOrders } from "../controller/getOpenOrders.js";
import { responses } from "../../../../globalUtils/routeDefs/responses.js";
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
import { getOpenOrders } from "../../../dataMart/controller/getOpenOrders.js";
import axios from "axios";
import { getSiloAdjustments } from "../../controller/siloAdjustments/getSiloAdjustments.js";
const app = new OpenAPIHono({ strict: false });
// const Body = z.object({
@@ -23,7 +24,7 @@ app.openapi(
// },
responses: responses(),
}),
async (c) => {
async (c: any) => {
const customer: any = c.req.queries();
// make sure we have a vaid user being accessed thats really logged in
@@ -45,13 +46,20 @@ app.openapi(
const dates: any = c.req.queries();
// const { data, error } = await tryCatch(
// axios.get(
// `/api/v1/warehouse/getSilosAdjustment?startDate=${dates.startDate[0]}&endDate=${dates.endDate[0]}`
// )
// );
const startDate = dates.startDate ? dates.startDate[0] : null;
const endDate = dates.endDate ? dates.endDate[0] : null;
const { data, error } = await tryCatch(
axios.get(
`http://localhost:4400/api/v1/warehouse/getSilosAdjustment?startDate=${dates.startDate[0]}&endDate=${dates.endDate[0]}`
)
getSiloAdjustments(startDate, endDate)
);
if (error) {
console.log(error);
return c.json({
success: false,
message: "Error running query",
@@ -60,9 +68,9 @@ app.openapi(
}
return c.json({
success: data?.data.success,
message: data?.data.message,
data: data?.data.data,
success: data?.success,
message: data?.message,
data: data?.data,
});
}
);

View File

@@ -59,6 +59,8 @@ app.openapi(
data.key,
payload.user
);
console.log(addComment);
return c.json(
{
success: addComment.success,

View File

@@ -6,6 +6,7 @@ import { tryCatch } from "../../../../globalUtils/tryCatch.js";
import { lstAuth } from "../../../../index.js";
import { db } from "../../../../../database/dbclient.js";
import { createLog } from "../../../logger/logger.js";
import { assignedPrinters } from "../../utils/checkAssignments.js";
export const updatePrinters = async () => {
const currentTime = new Date(Date.now());
@@ -80,5 +81,7 @@ export const updatePrinters = async () => {
);
}
await assignedPrinters();
return { success: true, message: "Printers were just added or updated." };
};