test(silo): backend silo stuff
This commit is contained in:
@@ -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,
|
||||
};
|
||||
};
|
||||
@@ -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();
|
||||
@@ -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;
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
||||
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({
|
||||
// includeRunnningNumbers: z.string().openapi({ example: "x" }),
|
||||
// });
|
||||
app.openapi(
|
||||
createRoute({
|
||||
tags: ["dataMart"],
|
||||
summary: "Returns All open orders.",
|
||||
method: "get",
|
||||
path: "/getsilosdjustment",
|
||||
// request: {
|
||||
// body: {
|
||||
// content: {
|
||||
// "application/json": { schema: Body },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
responses: responses(),
|
||||
}),
|
||||
async (c: any) => {
|
||||
const customer: any = c.req.queries();
|
||||
|
||||
// make sure we have a vaid user being accessed thats really logged in
|
||||
//apiHit(c, { endpoint: `api/logger/logs/id` });
|
||||
// const { data, error } = await tryCatch(
|
||||
// getOpenOrders(customer ? customer : null)
|
||||
// );
|
||||
|
||||
// if (error) {
|
||||
// return c.json(
|
||||
// {
|
||||
// success: false,
|
||||
// message: "There was an error getting the inv.",
|
||||
// data: error,
|
||||
// },
|
||||
// 400
|
||||
// );
|
||||
// }
|
||||
|
||||
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(
|
||||
getSiloAdjustments(startDate, endDate)
|
||||
);
|
||||
|
||||
if (error) {
|
||||
console.log(error);
|
||||
return c.json({
|
||||
success: false,
|
||||
message: "Error running query",
|
||||
data: error,
|
||||
});
|
||||
}
|
||||
|
||||
return c.json({
|
||||
success: data?.success,
|
||||
message: data?.message,
|
||||
data: data?.data,
|
||||
});
|
||||
}
|
||||
);
|
||||
export default app;
|
||||
@@ -59,6 +59,8 @@ app.openapi(
|
||||
data.key,
|
||||
payload.user
|
||||
);
|
||||
|
||||
console.log(addComment);
|
||||
return c.json(
|
||||
{
|
||||
success: addComment.success,
|
||||
|
||||
Reference in New Issue
Block a user