refactor(backend): dock door scanning socket and perms

This commit is contained in:
2026-06-01 14:23:26 -05:00
parent 2a35381fe4
commit f8335f5217
11 changed files with 391 additions and 38 deletions

View File

@@ -1,18 +1,70 @@
import { eq, sql } from "drizzle-orm";
import { Router } from "express";
import z from "zod";
import { db } from "../db/db.controller.js";
import { dockDoorScanners } from "../db/schema/dockdoor.schema.js";
import { apiReturn } from "../utils/returnHelper.utils.js";
import { tryCatch } from "../utils/trycatch.utils.js";
const r = Router();
const endLoading = z.object({
loadingOrder: z.string(),
dockId: z.string(),
});
r.post("/", async (req, res) => {
return apiReturn(res, {
success: true,
level: "info",
module: "dockdoor",
subModule: "lane check",
message: `Release x is being closed now. the bol should come out at the default printer.`,
data: req.body ?? [],
status: 200,
});
// close the loading order
// clear the loading order off the dock
try {
const validated = endLoading.parse(req.body);
const { data, error } = await tryCatch(
db
.update(dockDoorScanners)
.set({
currentLoadingOrder: "",
upd_date: sql`NOW()`,
upd_user: req.user?.username,
})
.where(eq(dockDoorScanners.dockId, validated.dockId))
.returning(),
);
if (error) {
return apiReturn(res, {
success: false,
level: "error",
module: "dockdoor",
subModule: "loadingOrder",
message: `Failed to updating the dock.`,
data: (error as any) ?? [],
status: 400,
});
}
return apiReturn(res, {
success: true,
level: "info",
module: "dockdoor",
subModule: "loadingOrder",
message: `Loading order ${validated.loadingOrder} was just closed.`,
data: data ?? [],
status: 200,
});
} catch (error) {
return apiReturn(res, {
success: false,
level: "error",
module: "dockdoor",
subModule: "loadingOrder",
message: `Failed to start loading order.`,
data: (error as any) ?? [],
status: 400,
});
}
});
export default r;

View File

@@ -0,0 +1,25 @@
import { Router } from "express";
import { apiReturn } from "../utils/returnHelper.utils.js";
import loadUnit from "./dockdoor.loadUnits.js";
const r = Router();
r.post("/", async (req, res) => {
const unit = await loadUnit({
dockId: req.body.dockId,
runningNo: req.body.runningNo,
});
return apiReturn(res, {
success: unit.success,
level: "info",
module: "dockdoor",
subModule: "loadingUnit",
message: unit.message,
data: unit?.data ?? [],
status: unit.success ? 200 : 400,
});
});
export default r;

View File

@@ -3,6 +3,7 @@
import { eq } from "drizzle-orm";
import { db } from "../db/db.controller.js";
import { dockDoorScanners } from "../db/schema/dockdoor.schema.js";
import { emitToRoom } from "../socket.io/roomEmitter.socket.js";
import { runProdApi } from "../utils/prodEndpoint.utils.js";
import { returnFunc } from "../utils/returnHelper.utils.js";
@@ -11,10 +12,10 @@ import { returnFunc } from "../utils/returnHelper.utils.js";
type Data = {
dockId?: string;
sscc?: string;
runningNr?: string;
runningNo?: string;
};
export const loadUnit = async (data: Data) => {
const loadUnit = async (data: Data) => {
// are we even active at this time?
const dockDoorActive = await db.query.settings.findFirst({
where: (u, { eq }) => eq(u.name, "dockDoorScanning"),
@@ -44,7 +45,7 @@ export const loadUnit = async (data: Data) => {
"Failed to load the unit to the truck, there was no pallet read.",
data: [],
notify: false,
room: `dockDoorLoading${data.dockId}`,
room: `dockDoorLoading:${data.dockId}`,
});
}
@@ -64,7 +65,7 @@ export const loadUnit = async (data: Data) => {
"There are know current active loading orders please start one and try again.",
data: [],
notify: false,
room: `dockDoorLoading${data.dockId}`,
room: `dockDoorLoading:${data.dockId}`,
});
}
@@ -76,14 +77,67 @@ export const loadUnit = async (data: Data) => {
// add the loading units
try {
const prod = await runProdApi({
const unitToScan = data.sscc
? { sscc: data.sscc?.slice(2) }
: { runningNo: Number(data.runningNo) };
const prod = (await runProdApi({
method: "post",
endpoint: `/public/v1.0/OutboundDeliveries/LoadingOrders/${dock[0]?.currentLoadingOrder}/LoadUnit`,
data: [{ sscc: data.sscc?.slice(2) }],
});
data: [unitToScan],
})) as any;
console.log(prod?.data);
//emitToRoom(`dockDoorLoading:${data.dockId}`, prod?.data ?? []);
if (!prod?.success) {
emitToRoom(`dockDoorLoading:${data.dockId}`, prod?.data.errors[0]);
return returnFunc({
success: false,
level: "error",
module: "dockdoor",
subModule: "loadUnit",
message: `Unit encountered an error while loading`,
data: prod?.data.errors[0] as any,
notify: false,
//room: `dockDoorLoading:${data.dockId}`,
});
} else {
const emitData = {
message: `The unit ${prod.data.message.messageParams.runningNo} was loaded.`,
data: prod.data,
code: 0,
};
emitToRoom(`dockDoorLoading:${data.dockId}`, emitData as any);
return returnFunc({
success: true,
level: "info",
module: "dockdoor",
subModule: "loadUnit",
message: `Unit added to loading order`,
data: [
{
message: `The unit ${prod.data.message.messageParams.runningNo} was loaded.`,
data: prod.data,
code: 0,
},
] as any,
notify: false,
//room: `dockDoorLoading:${data.dockId}`,
});
}
} catch (error) {
console.log(error);
return returnFunc({
success: true,
level: "error",
module: "dockdoor",
subModule: "loadUnit",
message: `Failed to load unit`,
data: error as any,
notify: false,
room: `dockDoorLoading:${data.dockId}`,
});
}
};
export default loadUnit;

View File

@@ -2,6 +2,7 @@ import type { Express } from "express";
import { featureCheck } from "../middleware/featureActive.middleware.js";
import activeLoadingOrders from "./dockdoor.activeLoadingOrders.route.js";
import closeLoadingOrder from "./dockdoor.closeLoadingOrder.route.js";
import load from "./dockdoor.loadUnits.route.js";
import startLoad from "./dockdoor.startLoad.route.js";
import prodDocks from "./dockdoors.docks.route.js";
import docks from "./dockdoors.route.js";
@@ -17,7 +18,7 @@ export const setupDockDoorRoutes = (baseUrl: string, app: Express) => {
);
app.use(
`${baseUrl}/api/dockDoor/closeLoadingOrder`,
`${baseUrl}/api/dockDoor/finishOrder`,
featureCheck("dockDoorScanning"),
closeLoadingOrder,
);
@@ -37,7 +38,11 @@ export const setupDockDoorRoutes = (baseUrl: string, app: Express) => {
prodDocks,
);
// TODO : add manual way to add pallets
app.use(
`${baseUrl}/api/dockDoor/loadUnit`,
featureCheck("dockDoorScanning"),
load,
);
// all other system should be under /api/system/*
};

View File

@@ -1,4 +1,4 @@
import { sql } from "drizzle-orm";
import { eq, sql } from "drizzle-orm";
import { Router } from "express";
import z from "zod";
import { db } from "../db/db.controller.js";
@@ -25,6 +25,7 @@ r.post("/", async (req, res) => {
upd_date: sql`NOW()`,
upd_user: req.user?.username,
})
.where(eq(dockDoorScanners.dockId, validated.dockId))
.returning(),
);