feat(lstv2 move): moved lstv2 into this app to keep them combined and easier to maintain
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
import { db } from "../../../../../database/dbclient.js";
|
||||
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
||||
import { query } from "../../../sqlServer/prodSqlServer.js";
|
||||
import { siloQuery } from "../../../sqlServer/querys/silo/siloQuery.js";
|
||||
import { postAdjustment } from "./postAdjustment.js";
|
||||
import { siloAdjustments } from "../../../../../database/schema/siloAdjustments.js";
|
||||
import { greetingStuff } from "../../../../globalUtils/greetingEmail.js";
|
||||
import { sendEmail } from "../../../notifications/controller/sendMail.js";
|
||||
import { settings } from "../../../../../database/schema/settings.js";
|
||||
import { generateOneTimeKey } from "../../../../globalUtils/singleUseKey.js";
|
||||
import { eq } from "drizzle-orm";
|
||||
import {
|
||||
getSettings,
|
||||
serverSettings,
|
||||
} from "../../../server/controller/settings/getSettings.js";
|
||||
|
||||
export const createSiloAdjustment = async (
|
||||
data: any | null,
|
||||
user: any | null
|
||||
) => {
|
||||
/**
|
||||
* Creates a silo adjustment based off warehouse, location, and qty.
|
||||
* qty will come from the hmi, prolink, or silo patrol
|
||||
*/
|
||||
// const { data: set, error: setError } = await tryCatch(
|
||||
// db.select().from(settings)
|
||||
// );
|
||||
|
||||
// const { data: set, error: setError } = await tryCatch(getSettings());
|
||||
|
||||
// if (setError) {
|
||||
// return {
|
||||
// success: false,
|
||||
// message: `There was an error getting setting data to post to the server.`,
|
||||
// data: setError,
|
||||
// };
|
||||
// }
|
||||
|
||||
const set = serverSettings.length === 0 ? [] : serverSettings;
|
||||
// getting stock data first so we have it prior to the adjustment
|
||||
const { data: s, error: stockError } = await tryCatch(
|
||||
query(siloQuery, "Silo data Query")
|
||||
);
|
||||
|
||||
if (stockError) {
|
||||
return {
|
||||
success: false,
|
||||
message: `There was an error getting stock data to post to the server.`,
|
||||
data: stockError,
|
||||
};
|
||||
}
|
||||
const stock: any = s?.data as any;
|
||||
const { data: a, error: errorAdj } = await tryCatch(
|
||||
postAdjustment(data, user.prod)
|
||||
);
|
||||
|
||||
if (errorAdj) {
|
||||
return {
|
||||
success: false,
|
||||
message: `There was an error doing the silo adjustment.`,
|
||||
data: errorAdj,
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Checking to see the difference, and send email if +/- 5% will change later if needed
|
||||
*/
|
||||
|
||||
const sa: any = a;
|
||||
|
||||
if (!sa.success) {
|
||||
console.log(`insde error`);
|
||||
return {
|
||||
success: sa.success,
|
||||
message: sa.message,
|
||||
data: sa.data,
|
||||
};
|
||||
}
|
||||
|
||||
const stockNummy = stock.filter((s: any) => s.LocationID === data.laneId);
|
||||
const theDiff =
|
||||
((data.quantity - stockNummy[0].Stock_Total) /
|
||||
((data.quantity + stockNummy[0].Stock_Total) / 2)) *
|
||||
100;
|
||||
|
||||
/**
|
||||
* Post the data to our db.
|
||||
*/
|
||||
|
||||
//console.log(stockNummy);
|
||||
const { data: postAdj, error: postAdjError } = await tryCatch(
|
||||
db
|
||||
.insert(siloAdjustments)
|
||||
.values({
|
||||
warehouseID: data.warehouseId,
|
||||
locationID: data.laneId,
|
||||
currentStockLevel: stockNummy[0].Stock_Total,
|
||||
newLevel: data.quantity,
|
||||
lastDateAdjusted: new Date(stockNummy[0].LastAdjustment),
|
||||
add_user: user.username,
|
||||
})
|
||||
.returning({ id: siloAdjustments.siloAdjust_id })
|
||||
);
|
||||
|
||||
if (postAdjError) {
|
||||
//console.log(postAdjError);
|
||||
return {
|
||||
success: false,
|
||||
message: `There was an error posting the new adjustment.`,
|
||||
data: postAdjError,
|
||||
};
|
||||
}
|
||||
let adj: any = a;
|
||||
if (Math.abs(theDiff) > 5) {
|
||||
// console.log(`Send for comment due to being: ${theDiff.toFixed(2)}%`);
|
||||
const server = set.filter((n: any) => n.name === "server");
|
||||
|
||||
const port = set.filter((n: any) => n.name === "serverPort");
|
||||
const key = await generateOneTimeKey();
|
||||
const updateKey = await db
|
||||
.update(siloAdjustments)
|
||||
.set({ commentKey: key })
|
||||
.where(eq(siloAdjustments.siloAdjust_id, postAdj[0].id));
|
||||
|
||||
const emailSetup = {
|
||||
email: user.email,
|
||||
subject: `Alert - Siloadjustment was done with a descrepancy of 5% or greater`,
|
||||
template: "siloAdjustmentComment",
|
||||
context: {
|
||||
greeting: await greetingStuff(),
|
||||
siloName: stockNummy[0].Description,
|
||||
variance: `${theDiff.toFixed(2)}%`,
|
||||
currentLevel: stockNummy[0].Stock_Total,
|
||||
newLevel: data.quantity,
|
||||
variancePer: 5,
|
||||
adjustID: `${postAdj[0].id}&${key}`,
|
||||
server: server[0].value,
|
||||
port: port[0].value,
|
||||
},
|
||||
};
|
||||
|
||||
//console.log(emailSetup);
|
||||
|
||||
await sendEmail(emailSetup);
|
||||
return {
|
||||
success: adj.success,
|
||||
message: `Silo adjustmnet was completed you will also receive and email due to the adjustment having a variation of ${Math.abs(
|
||||
theDiff
|
||||
).toFixed(2)}%`,
|
||||
data: adj.data,
|
||||
};
|
||||
} else {
|
||||
return { success: adj.success, message: adj.message, data: adj.data };
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
||||
import { query } from "../../../sqlServer/prodSqlServer.js";
|
||||
import { siloQuery } from "../../../sqlServer/querys/silo/siloQuery.js";
|
||||
|
||||
export const getStockSiloData = async () => {
|
||||
/**
|
||||
* will return the current stock info where the silo is checked
|
||||
*/
|
||||
|
||||
const { data, error } = await tryCatch(query(siloQuery, "Get silo data"));
|
||||
|
||||
if (error) {
|
||||
return {
|
||||
success: false,
|
||||
message: "There was a n error getting the silo data.",
|
||||
};
|
||||
}
|
||||
const stockData: any = data?.data;
|
||||
return {
|
||||
success: true,
|
||||
message: "Current silo data from alplastock.",
|
||||
data: stockData,
|
||||
};
|
||||
};
|
||||
@@ -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,96 @@
|
||||
/**
|
||||
* 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";
|
||||
import {
|
||||
getSettings,
|
||||
serverSettings,
|
||||
} from "../../../server/controller/settings/getSettings.js";
|
||||
|
||||
export const migrateAdjustments = async () => {
|
||||
/**
|
||||
* Migrates the silo adjustments from v1 to v2
|
||||
*/
|
||||
|
||||
//const { data, error } = await tryCatch(db.select().from(settings));
|
||||
// const { data, error } = await tryCatch(getSettings());
|
||||
|
||||
// if (error) {
|
||||
// createLog("error", "silo", "logistics", "Getting settings.");
|
||||
// return;
|
||||
// }
|
||||
|
||||
const data = serverSettings.length === 0 ? [] : serverSettings;
|
||||
|
||||
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.data;
|
||||
createLog("info", "silo", "logistics", "Starting migration.");
|
||||
for (let i = 0; i < silo.length; i++) {
|
||||
const migrate = await db.insert(siloAdjustments).values({
|
||||
warehouseID: silo[i].warehouseID,
|
||||
locationID: silo[i].locationID,
|
||||
currentStockLevel: silo[i].currentStockLevel,
|
||||
newLevel: silo[i].newLevel,
|
||||
dateAdjusted: new Date(silo[i].dateAdjusted),
|
||||
lastDateAdjusted: new Date(silo[i].lastDateAdjusted),
|
||||
add_user: silo[i].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.");
|
||||
};
|
||||
@@ -0,0 +1,100 @@
|
||||
import axios from "axios";
|
||||
import { prodEndpointCreation } from "../../../../globalUtils/createUrl.js";
|
||||
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
||||
|
||||
export const postAdjustment = async (data: any, prod: any) => {
|
||||
if (data.warehouseId === undefined) {
|
||||
return {
|
||||
sucess: false,
|
||||
message: `Missing mandatory field: warehouseID`,
|
||||
data: { error: `Missing mandatory field: warehouseID` },
|
||||
};
|
||||
}
|
||||
|
||||
if (data.laneId === undefined) {
|
||||
return {
|
||||
sucess: false,
|
||||
message: `Missing mandatory field: locationID`,
|
||||
data: { error: `Missing mandatory field: locationID` },
|
||||
};
|
||||
}
|
||||
|
||||
if (data.quantity == "0") {
|
||||
return {
|
||||
sucess: false,
|
||||
message: `You entered 0 for the quantity to post, quantity needs to be at leave 1`,
|
||||
data: {
|
||||
error: `You entered 0 for the quantity to post, quantity needs to be at leave 1`,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const siloAdjustment = {
|
||||
warehouseId: data.warehouseId,
|
||||
laneId: data.laneId,
|
||||
quantity: data.quantity,
|
||||
};
|
||||
|
||||
let url = await prodEndpointCreation(
|
||||
"/public/v1.0/Warehousing/AdjustSiloStockLevel"
|
||||
);
|
||||
|
||||
const { data: silo, error } = await tryCatch(
|
||||
axios.post(url, siloAdjustment, {
|
||||
headers: {
|
||||
"X-API-Key": process.env.TEC_API_KEY || "",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
})
|
||||
);
|
||||
let e = error as any;
|
||||
if (e) {
|
||||
console.log(e.response);
|
||||
if (e.status === 401) {
|
||||
const data = {
|
||||
success: false,
|
||||
message: `There was error posting the data: ${JSON.stringify(
|
||||
e.response?.data
|
||||
)}`,
|
||||
data: {
|
||||
status: e.response?.status,
|
||||
statusText: e.response?.statusText,
|
||||
data: e.response?.data,
|
||||
},
|
||||
};
|
||||
return data;
|
||||
} else {
|
||||
return {
|
||||
success: false,
|
||||
message: "Error in posting the silo adjustment.",
|
||||
data: {
|
||||
status: e.response?.status,
|
||||
statusText: e.response?.statusText,
|
||||
data: e.response?.data,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (silo?.status !== 200) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Error in posting the silo adjustment",
|
||||
data: {
|
||||
status: silo?.status,
|
||||
statusText: silo?.statusText,
|
||||
data: silo?.data,
|
||||
},
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
success: true,
|
||||
message: "Adjustment was completed",
|
||||
data: {
|
||||
status: silo.status,
|
||||
statusText: silo.statusText,
|
||||
data: silo.data,
|
||||
},
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,64 @@
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import { db } from "../../../../../database/dbclient.js";
|
||||
import { siloAdjustments } from "../../../../../database/schema/siloAdjustments.js";
|
||||
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
||||
|
||||
export const postSiloComment = async (
|
||||
id: string,
|
||||
comment: string,
|
||||
commentk: string,
|
||||
user: any
|
||||
) => {
|
||||
/**
|
||||
* We will add the comment to the silo adjustment so we know the why we had this.
|
||||
*/
|
||||
|
||||
// make sure we havea valid key
|
||||
const { data: key, error: keyErro } = await tryCatch(
|
||||
db
|
||||
.select()
|
||||
.from(siloAdjustments)
|
||||
.where(eq(siloAdjustments.siloAdjust_id, id))
|
||||
);
|
||||
|
||||
if (keyErro) {
|
||||
return {
|
||||
success: false,
|
||||
message: "There was an error getting the adjustment.",
|
||||
data: keyErro,
|
||||
};
|
||||
}
|
||||
|
||||
if (key[0].commentKey != commentk) {
|
||||
return {
|
||||
success: false,
|
||||
message: "The key you provided is invalid.",
|
||||
data: keyErro,
|
||||
};
|
||||
}
|
||||
|
||||
const { data, error } = await tryCatch(
|
||||
db
|
||||
.update(siloAdjustments)
|
||||
.set({
|
||||
comment: comment,
|
||||
commentAddedBy: user.username,
|
||||
commentDate: sql`NOW()`,
|
||||
commentKey: null,
|
||||
})
|
||||
.where(eq(siloAdjustments.siloAdjust_id, id))
|
||||
);
|
||||
|
||||
if (error) {
|
||||
return {
|
||||
success: false,
|
||||
message: "There was an error adding the comment.",
|
||||
data: error,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Comment was successfully added.",
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user