From 90610c4ce2c9d80382300377f9e2c1a80f7cf0b5 Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Tue, 7 Oct 2025 06:46:02 -0500 Subject: [PATCH] feat(servers): cru added in to the server and dynamically updates vms036 --- .../Notifcations/Active notifications.bru | 15 ++ .../LstV2/Notifcations/folder.bru | 8 ++ .../app/admin/server/Add Server.bru | 12 +- .../app/admin/server/Update Server.bru | 15 +- .../environments/lst.bru | 2 +- app/main.ts | 1 + app/src/internal/admin/routes.ts | 13 +- .../admin/routes/servers/addServer.ts | 30 +++- .../admin/routes/servers/getServers.ts | 12 +- .../admin/routes/servers/serverRoutes.ts | 26 +++- .../admin/routes/servers/updateServer.ts | 128 +++++++++++++++++- 11 files changed, 239 insertions(+), 23 deletions(-) create mode 100644 LogisticsSupportTool_API_DOCS/LstV2/Notifcations/Active notifications.bru create mode 100644 LogisticsSupportTool_API_DOCS/LstV2/Notifcations/folder.bru diff --git a/LogisticsSupportTool_API_DOCS/LstV2/Notifcations/Active notifications.bru b/LogisticsSupportTool_API_DOCS/LstV2/Notifcations/Active notifications.bru new file mode 100644 index 0000000..ecb102a --- /dev/null +++ b/LogisticsSupportTool_API_DOCS/LstV2/Notifcations/Active notifications.bru @@ -0,0 +1,15 @@ +meta { + name: Active notifications + type: http + seq: 1 +} + +get { + url: {{urlv2}}/api/notify/activenotifications + body: none + auth: inherit +} + +settings { + encodeUrl: true +} diff --git a/LogisticsSupportTool_API_DOCS/LstV2/Notifcations/folder.bru b/LogisticsSupportTool_API_DOCS/LstV2/Notifcations/folder.bru new file mode 100644 index 0000000..ff5caf1 --- /dev/null +++ b/LogisticsSupportTool_API_DOCS/LstV2/Notifcations/folder.bru @@ -0,0 +1,8 @@ +meta { + name: Notifcations + seq: 3 +} + +auth { + mode: inherit +} diff --git a/LogisticsSupportTool_API_DOCS/app/admin/server/Add Server.bru b/LogisticsSupportTool_API_DOCS/app/admin/server/Add Server.bru index ec40af3..b31ce96 100644 --- a/LogisticsSupportTool_API_DOCS/app/admin/server/Add Server.bru +++ b/LogisticsSupportTool_API_DOCS/app/admin/server/Add Server.bru @@ -10,12 +10,16 @@ post { auth: inherit } +headers { + Cookie: {{session_cookie}} +} + body:json { { - "name": "Test Server", - "serverDNS": "USMCD1VMS036", - "plantToken": "test3", - "ipAddress": "10.193.0.56", + "name": "Bethlehem", + "serverDNS": "USBET1VMS006", + "plantToken": "usbet1", + "ipAddress": "10.204.0.26", "greatPlainsPlantCode": 0, "lstServerPort": 4000, "serverLoc": "E$\\LST" diff --git a/LogisticsSupportTool_API_DOCS/app/admin/server/Update Server.bru b/LogisticsSupportTool_API_DOCS/app/admin/server/Update Server.bru index 3226b9b..a3c22b4 100644 --- a/LogisticsSupportTool_API_DOCS/app/admin/server/Update Server.bru +++ b/LogisticsSupportTool_API_DOCS/app/admin/server/Update Server.bru @@ -5,11 +5,22 @@ meta { } patch { - url: {{url}}/lst/api/admin/server - body: none + url: {{url}}/lst/api/admin/server/:token + body: json auth: inherit } +params:path { + token: usbet1 +} + +body:json { + { + "zipcode": 45247 + + } +} + settings { encodeUrl: true } diff --git a/LogisticsSupportTool_API_DOCS/environments/lst.bru b/LogisticsSupportTool_API_DOCS/environments/lst.bru index 97388f5..c60e784 100644 --- a/LogisticsSupportTool_API_DOCS/environments/lst.bru +++ b/LogisticsSupportTool_API_DOCS/environments/lst.bru @@ -1,7 +1,7 @@ vars { url: http://localhost:4200 session_cookie: - urlv2: http://localhost:3000 + urlv2: http://usksc1vms006:3000 jwtV2: } vars:secret [ diff --git a/app/main.ts b/app/main.ts index e3b4c2c..b87d832 100644 --- a/app/main.ts +++ b/app/main.ts @@ -1,3 +1,4 @@ +process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; import express from "express"; import morgan from "morgan"; import { createServer } from "http"; diff --git a/app/src/internal/admin/routes.ts b/app/src/internal/admin/routes.ts index 482babc..7e64748 100644 --- a/app/src/internal/admin/routes.ts +++ b/app/src/internal/admin/routes.ts @@ -5,9 +5,13 @@ import { requireAuth } from "../../pkg/middleware/authMiddleware.js"; import users from "./routes/getUserRoles.js"; import grantRoles from "./routes/grantRole.js"; import servers from "./routes/servers/serverRoutes.js"; -import { restrictToHosts } from "../../pkg/middleware/restrictToHosts.js"; export const setupAdminRoutes = (app: Express, basePath: string) => { + app.use( + basePath + "/api/admin/server", // will pass bc system admin but this is just telling us we need this + servers + ); + app.use( basePath + "/api/admin/users", requireAuth("user", ["systemAdmin"]), // will pass bc system admin but this is just telling us we need this @@ -18,11 +22,4 @@ export const setupAdminRoutes = (app: Express, basePath: string) => { requireAuth("user", ["systemAdmin", "admin"]), // will pass bc system admin but this is just telling us we need this grantRoles ); - - app.use( - basePath + "/api/admin/server", - requireAuth("user", ["systemAdmin", "admin"]), // will pass bc system admin but this is just telling us we need this - restrictToHosts(["usmcd1vms036", "USMCD1VMS036"]), // what servers are allowed to see the server section - servers - ); }; diff --git a/app/src/internal/admin/routes/servers/addServer.ts b/app/src/internal/admin/routes/servers/addServer.ts index bd6c3bc..d0e5411 100644 --- a/app/src/internal/admin/routes/servers/addServer.ts +++ b/app/src/internal/admin/routes/servers/addServer.ts @@ -9,6 +9,7 @@ import { tryCatch } from "../../../../pkg/utils/tryCatch.js"; import type { DrizzleError } from "drizzle-orm"; import axios from "axios"; import { createLogger } from "../../../../pkg/logger/logger.js"; +import https from "https"; const router = Router(); @@ -43,6 +44,28 @@ router.post("/", async (req: Request, res: Response) => { if (req.hostname === "localhost" && process.env.MAIN_SERVER) { log.info({}, "Running in dev server about to add in a new server"); + const axiosInstance = axios.create({ + httpsAgent: new https.Agent({ rejectUnauthorized: false }), + baseURL: process.env.MAIN_SERVER, // e.g. "https://example.com" + withCredentials: true, + }); + + const loginRes = await axiosInstance.post( + `${process.env.MAIN_SERVER}/lst/api/auth/sign-in/username`, + { + username: process.env.MAIN_SERVER_USERNAME, + password: process.env.MAIN_SERVER_PASSWORD, + }, + { + headers: { "Content-Type": "application/json" }, + } + ); + const setCookie = loginRes.headers["set-cookie"]; + + if (!setCookie) { + throw new Error("Did not receive a Set-Cookie header from login"); + } + const { data, error } = await tryCatch( axios.post( `${process.env.MAIN_SERVER}/lst/api/admin/server`, @@ -50,7 +73,7 @@ router.post("/", async (req: Request, res: Response) => { { headers: { "Content-Type": "application/json", - Cookie: req.headers.cookie ?? "", + Cookie: setCookie.join("; "), }, withCredentials: true, } @@ -63,7 +86,10 @@ router.post("/", async (req: Request, res: Response) => { "There was an error adding the server to Main Server" ); } - log.info({ stack: data }, "A new Server was just added to the server."); + log.info( + { stack: data?.data }, + "A new Server was just added to the server." + ); } return res diff --git a/app/src/internal/admin/routes/servers/getServers.ts b/app/src/internal/admin/routes/servers/getServers.ts index 85152c7..80f646b 100644 --- a/app/src/internal/admin/routes/servers/getServers.ts +++ b/app/src/internal/admin/routes/servers/getServers.ts @@ -8,11 +8,21 @@ import { and, asc, eq } from "drizzle-orm"; const router = Router(); router.get("/", async (req: Request, res: Response) => { + const token = req.query.token; + + const conditions = []; + + if (token !== undefined) { + conditions.push(eq(serverData.plantToken, `${token}`)); + } + + conditions.push(eq(serverData.active, true)); + const { data, error } = await tryCatch( db .select() .from(serverData) - .where(eq(serverData.active, true)) + .where(and(...conditions)) .orderBy(asc(serverData.name)) ); diff --git a/app/src/internal/admin/routes/servers/serverRoutes.ts b/app/src/internal/admin/routes/servers/serverRoutes.ts index 4dd0350..b6a0bfe 100644 --- a/app/src/internal/admin/routes/servers/serverRoutes.ts +++ b/app/src/internal/admin/routes/servers/serverRoutes.ts @@ -2,11 +2,31 @@ import { Router } from "express"; import addServer from "./addServer.js"; import getServers from "./getServers.js"; import updateServer from "./updateServer.js"; +import { restrictToHosts } from "../../../../pkg/middleware/restrictToHosts.js"; +import { requireAuth } from "../../../../pkg/middleware/authMiddleware.js"; const router = Router(); -router.get("/", getServers); -router.post("/", addServer); -router.patch("/", updateServer); +router.use("/", getServers); +router.use( + "/", + requireAuth("user", ["systemAdmin", "admin"]), + restrictToHosts([ + "usmcd1vms036", + "USMCD1VMS036", + "https://usmcd1vms036.alpla.net", + ]), + addServer +); +router.use( + "/", + requireAuth("user", ["systemAdmin", "admin"]), + restrictToHosts([ + "usmcd1vms036", + "USMCD1VMS036", + "https://usmcd1vms036.alpla.net", + ]), + updateServer +); export default router; diff --git a/app/src/internal/admin/routes/servers/updateServer.ts b/app/src/internal/admin/routes/servers/updateServer.ts index 59fd75b..a7dac57 100644 --- a/app/src/internal/admin/routes/servers/updateServer.ts +++ b/app/src/internal/admin/routes/servers/updateServer.ts @@ -1,11 +1,135 @@ import { Router } from "express"; import type { Request, Response } from "express"; +import { db } from "../../../../pkg/db/db.js"; +import { serverData } from "../../../../pkg/db/schema/servers.js"; +import { eq } from "drizzle-orm"; +import { tryCatch } from "../../../../pkg/utils/tryCatch.js"; +import axios from "axios"; +import { createLogger } from "../../../../pkg/logger/logger.js"; +import https from "https"; const router = Router(); -router.patch("/", async (req: Request, res: Response) => { +router.patch("/:token", async (req: Request, res: Response) => { + const log = createLogger({ module: "admin", subModule: "update server" }); + // when a server is updated and is posted from localhost or 127.0.0.1 we also want to post it to the test server so we can see it from there, we want to insert with update on conflict. - res.status(200).json({ message: "Server added" }); + const token = req.params.token; + const updates: Record = {}; + + if (req.body?.name !== undefined) { + updates.name = req.body.name; + } + if (req.body?.serverDNS !== undefined) { + updates.serverDNS = req.body.serverDNS; + } + if (req.body?.ipAddress !== undefined) { + updates.ipAddress = req.body.ipAddress; + } + + if (req.body?.greatPlainsPlantCode !== undefined) { + updates.greatPlainsPlantCode = req.body.greatPlainsPlantCode; + } + + if (req.body?.lstServerPort !== undefined) { + updates.lstServerPort = req.body.lstServerPort; + } + + if (req.body?.serverLoc !== undefined) { + updates.serverLoc = req.body.serverLoc; + } + + if (req.body?.streetAddress !== undefined) { + updates.streetAddress = req.body.streetAddress; + } + + if (req.body?.cityState !== undefined) { + updates.cityState = req.body.cityState; + } + + if (req.body?.zipcode !== undefined) { + updates.zipcode = req.body.zipcode; + } + + if (req.body?.contactEmail !== undefined) { + updates.contactEmail = req.body.contactEmail; + } + + if (req.body?.contactPhone !== undefined) { + updates.contactPhone = req.body.contactPhone; + } + + if (req.body?.customerTiAcc !== undefined) { + updates.customerTiAcc = req.body.customerTiAcc; + } + + if (req.body?.active !== undefined) { + updates.active = req.body.active; + } + try { + if (Object.keys(updates).length > 0) { + await db + .update(serverData) + .set(updates) + .where(eq(serverData.plantToken, token)); + } + + if (req.hostname === "localhost" && process.env.MAIN_SERVER) { + log.info({}, "Running in dev server about to add in a new server"); + const axiosInstance = axios.create({ + httpsAgent: new https.Agent({ rejectUnauthorized: false }), + baseURL: process.env.MAIN_SERVER, + withCredentials: true, + }); + + const loginRes = await axiosInstance.post( + `${process.env.MAIN_SERVER}/lst/api/auth/sign-in/username`, + { + username: process.env.MAIN_SERVER_USERNAME, + password: process.env.MAIN_SERVER_PASSWORD, + }, + { + headers: { "Content-Type": "application/json" }, + } + ); + const setCookie = loginRes.headers["set-cookie"]; + + if (!setCookie) { + throw new Error( + "Did not receive a Set-Cookie header from login" + ); + } + + const { data, error } = await tryCatch( + axios.post( + `${process.env.MAIN_SERVER}/lst/api/admin/server/${token}`, + updates, + { + headers: { + "Content-Type": "application/json", + Cookie: setCookie.join("; "), + }, + withCredentials: true, + } + ) + ); + + if (error) { + log.error( + { stack: error }, + "There was an error adding the server to Main Server" + ); + } + log.info( + { stack: data?.data }, + "A new Server was just added to the server." + ); + } + res.status(200).json({ message: `${token} Server was just updated` }); + } catch (error) { + console.log(error); + res.status(400).json({ message: "Error Server updated", error }); + } }); export default router;