feat(settings): added in settings
This commit is contained in:
@@ -1,100 +1,106 @@
|
||||
import { Router } from "express";
|
||||
import type { Request, Response } from "express";
|
||||
import {
|
||||
insertServerDataSchema,
|
||||
serverData,
|
||||
} from "../../../../pkg/db/schema/servers.js";
|
||||
import { db } from "../../../../pkg/db/db.js";
|
||||
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 { type DrizzleError, sql } from "drizzle-orm";
|
||||
import type { Request, Response } from "express";
|
||||
import { Router } from "express";
|
||||
import https from "https";
|
||||
import { db } from "../../../../pkg/db/db.js";
|
||||
import {
|
||||
insertServerDataSchema,
|
||||
serverData,
|
||||
} from "../../../../pkg/db/schema/servers.js";
|
||||
import { createLogger } from "../../../../pkg/logger/logger.js";
|
||||
import { tryCatch } from "../../../../pkg/utils/tryCatch.js";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post("/", async (req: Request, res: Response) => {
|
||||
// when a new server 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
|
||||
//res.status(200).json({ message: "Server added", ip: req.hostname });
|
||||
const log = createLogger({ module: "admin", subModule: "add server" });
|
||||
const parsed = insertServerDataSchema.safeParse(req.body);
|
||||
// when a new server 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
|
||||
//res.status(200).json({ message: "Server added", ip: req.hostname });
|
||||
const log = createLogger({ module: "admin", subModule: "add server" });
|
||||
const parsed = insertServerDataSchema.safeParse(req.body);
|
||||
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({ errors: parsed.error.flatten() });
|
||||
}
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({ errors: parsed.error.flatten() });
|
||||
}
|
||||
|
||||
const { data, error } = await tryCatch(
|
||||
db
|
||||
.insert(serverData)
|
||||
.values(parsed.data)
|
||||
//.onConflictDoNothing()
|
||||
.returning({
|
||||
name: serverData.name,
|
||||
plantToken: serverData.plantToken,
|
||||
})
|
||||
);
|
||||
const { data, error } = await tryCatch(
|
||||
db
|
||||
.insert(serverData)
|
||||
.values({
|
||||
...parsed.data,
|
||||
add_user: req.user?.username,
|
||||
add_date: sql`NOW()`,
|
||||
upd_user: req.user?.username,
|
||||
upd_date: sql`NOW()`,
|
||||
})
|
||||
//.onConflictDoNothing()
|
||||
.returning({
|
||||
name: serverData.name,
|
||||
plantToken: serverData.plantToken,
|
||||
}),
|
||||
);
|
||||
|
||||
if (error) {
|
||||
const err: DrizzleError = error;
|
||||
return res.status(400).json({
|
||||
message: `Error adding the server`,
|
||||
error: err.cause,
|
||||
});
|
||||
}
|
||||
if (error) {
|
||||
const err: DrizzleError = error;
|
||||
return res.status(400).json({
|
||||
message: `Error adding the server`,
|
||||
error: err.cause,
|
||||
});
|
||||
}
|
||||
|
||||
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,
|
||||
});
|
||||
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" },
|
||||
}
|
||||
)) as any;
|
||||
const setCookie = loginRes.headers["set-cookie"][0];
|
||||
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" },
|
||||
},
|
||||
)) as any;
|
||||
const setCookie = loginRes.headers["set-cookie"][0];
|
||||
|
||||
if (!setCookie) {
|
||||
throw new Error("Did not receive a Set-Cookie header from login");
|
||||
}
|
||||
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`,
|
||||
parsed.data,
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Cookie: setCookie.split(";")[0],
|
||||
},
|
||||
withCredentials: true,
|
||||
}
|
||||
)
|
||||
);
|
||||
const { data, error } = await tryCatch(
|
||||
axios.post(
|
||||
`${process.env.MAIN_SERVER}/lst/api/admin/server`,
|
||||
parsed.data,
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Cookie: setCookie.split(";")[0],
|
||||
},
|
||||
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."
|
||||
);
|
||||
}
|
||||
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.",
|
||||
);
|
||||
}
|
||||
|
||||
return res
|
||||
.status(201)
|
||||
.json({ message: `Server ${data[0]?.name} added`, data: data });
|
||||
return res
|
||||
.status(201)
|
||||
.json({ message: `Server ${data[0]?.name} added`, data: data });
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -1,139 +1,141 @@
|
||||
import { Router } from "express";
|
||||
import axios from "axios";
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import type { Request, Response } from "express";
|
||||
import { Router } from "express";
|
||||
import https from "https";
|
||||
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";
|
||||
import { tryCatch } from "../../../../pkg/utils/tryCatch.js";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.patch("/:token", async (req: Request, res: Response) => {
|
||||
const log = createLogger({ module: "admin", subModule: "update server" });
|
||||
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.
|
||||
const token = req.params.token;
|
||||
const updates: Record<string, any> = {};
|
||||
// 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.
|
||||
const token = req.params.token;
|
||||
const updates: Record<string, any> = {};
|
||||
|
||||
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?.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?.greatPlainsPlantCode !== undefined) {
|
||||
updates.greatPlainsPlantCode = req.body.greatPlainsPlantCode;
|
||||
}
|
||||
|
||||
if (req.body?.lstServerPort !== undefined) {
|
||||
updates.lstServerPort = req.body.lstServerPort;
|
||||
}
|
||||
if (req.body?.lstServerPort !== undefined) {
|
||||
updates.lstServerPort = req.body.lstServerPort;
|
||||
}
|
||||
|
||||
if (req.body?.serverLoc !== undefined) {
|
||||
updates.serverLoc = req.body.serverLoc;
|
||||
}
|
||||
if (req.body?.serverLoc !== undefined) {
|
||||
updates.serverLoc = req.body.serverLoc;
|
||||
}
|
||||
|
||||
if (req.body?.streetAddress !== undefined) {
|
||||
updates.streetAddress = req.body.streetAddress;
|
||||
}
|
||||
if (req.body?.streetAddress !== undefined) {
|
||||
updates.streetAddress = req.body.streetAddress;
|
||||
}
|
||||
|
||||
if (req.body?.cityState !== undefined) {
|
||||
updates.cityState = req.body.cityState;
|
||||
}
|
||||
if (req.body?.cityState !== undefined) {
|
||||
updates.cityState = req.body.cityState;
|
||||
}
|
||||
|
||||
if (req.body?.zipcode !== undefined) {
|
||||
updates.zipcode = req.body.zipcode;
|
||||
}
|
||||
if (req.body?.zipcode !== undefined) {
|
||||
updates.zipcode = req.body.zipcode;
|
||||
}
|
||||
|
||||
if (req.body?.contactEmail !== undefined) {
|
||||
updates.contactEmail = req.body.contactEmail;
|
||||
}
|
||||
if (req.body?.contactEmail !== undefined) {
|
||||
updates.contactEmail = req.body.contactEmail;
|
||||
}
|
||||
|
||||
if (req.body?.contactPhone !== undefined) {
|
||||
updates.contactPhone = req.body.contactPhone;
|
||||
}
|
||||
if (req.body?.contactPhone !== undefined) {
|
||||
updates.contactPhone = req.body.contactPhone;
|
||||
}
|
||||
|
||||
if (req.body?.customerTiAcc !== undefined) {
|
||||
updates.customerTiAcc = req.body.customerTiAcc;
|
||||
}
|
||||
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.body?.active !== undefined) {
|
||||
updates.active = req.body.active;
|
||||
}
|
||||
|
||||
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,
|
||||
});
|
||||
updates.upd_user = req.user!.username || "lst_user";
|
||||
updates.upd_date = sql`NOW()`;
|
||||
|
||||
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" },
|
||||
}
|
||||
)) as any;
|
||||
try {
|
||||
if (Object.keys(updates).length > 0) {
|
||||
await db
|
||||
.update(serverData)
|
||||
.set(updates)
|
||||
.where(eq(serverData.plantToken, token));
|
||||
}
|
||||
|
||||
const setCookie = loginRes?.headers["set-cookie"][0];
|
||||
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,
|
||||
});
|
||||
|
||||
//console.log(setCookie.split(";")[0].replace("__Secure-", ""));
|
||||
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" },
|
||||
},
|
||||
)) as any;
|
||||
|
||||
if (!setCookie) {
|
||||
throw new Error(
|
||||
"Did not receive a Set-Cookie header from login"
|
||||
);
|
||||
}
|
||||
const setCookie = loginRes?.headers["set-cookie"][0];
|
||||
|
||||
const { data, error } = await tryCatch(
|
||||
axios.patch(
|
||||
`${process.env.MAIN_SERVER}/lst/api/admin/server/${token}`,
|
||||
updates,
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Cookie: setCookie.split(";")[0],
|
||||
},
|
||||
withCredentials: true,
|
||||
}
|
||||
)
|
||||
);
|
||||
//console.log(setCookie.split(";")[0].replace("__Secure-", ""));
|
||||
|
||||
if (error) {
|
||||
console.log(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 });
|
||||
}
|
||||
if (!setCookie) {
|
||||
throw new Error("Did not receive a Set-Cookie header from login");
|
||||
}
|
||||
|
||||
const { data, error } = await tryCatch(
|
||||
axios.patch(
|
||||
`${process.env.MAIN_SERVER}/lst/api/admin/server/${token}`,
|
||||
updates,
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Cookie: setCookie.split(";")[0],
|
||||
},
|
||||
withCredentials: true,
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
if (error) {
|
||||
console.log(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;
|
||||
|
||||
Reference in New Issue
Block a user