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,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();