feat(ocp): added in printers get and add
This commit is contained in:
21
server/services/ocp/controller/getPrinters.ts
Normal file
21
server/services/ocp/controller/getPrinters.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { db } from "../../../../database/dbclient.js";
|
||||
import { tryCatch } from "../../../globalUtils/tryCatch.js";
|
||||
import { printers } from "../../../../database/schema/printers.js";
|
||||
|
||||
export const getPrinters = async () => {
|
||||
const currentTime = new Date(Date.now());
|
||||
|
||||
const { data: printerData, error: printerError } = await tryCatch(
|
||||
db.select().from(printers)
|
||||
);
|
||||
|
||||
if (printerError) {
|
||||
return {
|
||||
success: false,
|
||||
message: "there was an error getting the printers",
|
||||
data: printerError,
|
||||
};
|
||||
}
|
||||
|
||||
return { success: true, message: "Printers", data: printerData };
|
||||
};
|
||||
84
server/services/ocp/controller/updatePrinters.ts
Normal file
84
server/services/ocp/controller/updatePrinters.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { db } from "../../../../database/dbclient.js";
|
||||
import { tryCatch } from "../../../globalUtils/tryCatch.js";
|
||||
import { printers } from "../../../../database/schema/printers.js";
|
||||
import { settings } from "../../../../database/schema/settings.js";
|
||||
import axios from "axios";
|
||||
import { lstAuth } from "../../../index.js";
|
||||
import { prodEndpointCreation } from "../../../globalUtils/createUrl.js";
|
||||
import { createLog } from "../../logger/logger.js";
|
||||
import { sql } from "drizzle-orm";
|
||||
|
||||
export const updatePrinters = async () => {
|
||||
const currentTime = new Date(Date.now());
|
||||
|
||||
// get the printers from prod
|
||||
let url = await prodEndpointCreation(
|
||||
"/public/v1.0/Administration/Printers"
|
||||
);
|
||||
|
||||
const { data: prodPrinters, error: prodError } = await tryCatch(
|
||||
axios.get(url, {
|
||||
headers: {
|
||||
Authorization: `Basic ${lstAuth}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
if (prodError) {
|
||||
console.log(prodError);
|
||||
return {
|
||||
success: false,
|
||||
message: "there was an error getting the printers.",
|
||||
data: prodError,
|
||||
};
|
||||
}
|
||||
|
||||
// do the printer update into our db
|
||||
const prodPrinterInfo = prodPrinters.data;
|
||||
|
||||
for (let i = 0; i < prodPrinterInfo.length; i++) {
|
||||
const printerStuff: any = {
|
||||
humanReadableId: prodPrinterInfo[i].humanReadableId,
|
||||
name: prodPrinterInfo[i].name,
|
||||
ipAddress: prodPrinterInfo[i].ipAddress,
|
||||
port: prodPrinterInfo[i].port,
|
||||
remark: prodPrinterInfo[i].remark,
|
||||
};
|
||||
const { data, error } = await tryCatch(
|
||||
db
|
||||
.insert(printers)
|
||||
.values(printerStuff)
|
||||
.onConflictDoUpdate({
|
||||
target: printers.humanReadableId,
|
||||
set: {
|
||||
//humanReadableId: prodPrinterInfo[i].humanReadableId,
|
||||
name: prodPrinterInfo[i].name,
|
||||
ipAddress: prodPrinterInfo[i].ipAddress,
|
||||
port: prodPrinterInfo[i].port,
|
||||
remark: prodPrinterInfo[i].remark,
|
||||
upd_date: sql`NOW()`,
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
if (error) {
|
||||
createLog(
|
||||
"error",
|
||||
"lst",
|
||||
"ocp",
|
||||
`${
|
||||
prodPrinterInfo[i].name
|
||||
} encoutered and error adding/updating ${JSON.stringify(error)}`
|
||||
);
|
||||
}
|
||||
createLog(
|
||||
"info",
|
||||
"lst",
|
||||
"ocp",
|
||||
`${prodPrinterInfo[i].name} were just added/updated.`
|
||||
);
|
||||
}
|
||||
|
||||
return { success: true, message: "Printers were just added or updated." };
|
||||
};
|
||||
@@ -1,14 +1,21 @@
|
||||
import {OpenAPIHono} from "@hono/zod-openapi";
|
||||
import { OpenAPIHono } from "@hono/zod-openapi";
|
||||
|
||||
// routes
|
||||
import manualLabelLog from "./routes/manualPrintLog.js";
|
||||
|
||||
import {db} from "../../../database/dbclient.js";
|
||||
import {settings} from "../../../database/schema/settings.js";
|
||||
import getPrinters from "./routes/printers/getPritners.js";
|
||||
import { db } from "../../../database/dbclient.js";
|
||||
import { settings } from "../../../database/schema/settings.js";
|
||||
import updateprinters from "./routes/printers/updatePrinters.js";
|
||||
import { updatePrinters } from "./controller/updatePrinters.js";
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
|
||||
const routes = [manualLabelLog] as const;
|
||||
const routes = [
|
||||
manualLabelLog,
|
||||
//printer
|
||||
getPrinters,
|
||||
updateprinters,
|
||||
] as const;
|
||||
const setting = await db.select().from(settings);
|
||||
|
||||
const appRoutes = routes.forEach((route) => {
|
||||
@@ -16,7 +23,13 @@ const appRoutes = routes.forEach((route) => {
|
||||
});
|
||||
|
||||
app.all("/ocp/*", (c) => {
|
||||
return c.json({success: false, message: "You have encounters a ocp route that dose not exist."});
|
||||
return c.json({
|
||||
success: false,
|
||||
message: "You have encounters a ocp route that dose not exist.",
|
||||
});
|
||||
});
|
||||
|
||||
// run the printer update on restart just to keep everything good
|
||||
updatePrinters();
|
||||
|
||||
export default app;
|
||||
|
||||
50
server/services/ocp/routes/printers/getPritners.ts
Normal file
50
server/services/ocp/routes/printers/getPritners.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
// an external way to creating logs
|
||||
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
||||
import { responses } from "../../../../globalUtils/routeDefs/responses.js";
|
||||
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
||||
import { getPrinters } from "../../controller/getPrinters.js";
|
||||
|
||||
const app = new OpenAPIHono({ strict: false });
|
||||
const CreateLog = z.object({
|
||||
line: z.string().openapi({ example: "info" }),
|
||||
initials: z.string().openapi({ example: "server" }),
|
||||
printReason: z.string().openapi({ example: "This is a new log posted" }),
|
||||
additionalComments: z
|
||||
.string()
|
||||
.optional()
|
||||
.openapi({ example: "Some reason why we did this." }),
|
||||
});
|
||||
|
||||
app.openapi(
|
||||
createRoute({
|
||||
tags: ["ocp"],
|
||||
summary: "Prints a label.",
|
||||
method: "get",
|
||||
path: "/getprinters",
|
||||
//middleware: authMiddleware,
|
||||
//description: "This might be a temp soltuin during the transtion between versions",
|
||||
request: {
|
||||
body: { content: { "application/json": { schema: CreateLog } } },
|
||||
},
|
||||
responses: responses(),
|
||||
}),
|
||||
async (c) => {
|
||||
const { data: printData, error: printError } = await tryCatch(
|
||||
getPrinters()
|
||||
);
|
||||
|
||||
if (printError) {
|
||||
return c.json({
|
||||
success: false,
|
||||
message: "There was an error getting the printers",
|
||||
});
|
||||
}
|
||||
|
||||
return c.json({
|
||||
success: printData.success,
|
||||
message: printData.message,
|
||||
data: printData.data,
|
||||
});
|
||||
}
|
||||
);
|
||||
export default app;
|
||||
51
server/services/ocp/routes/printers/updatePrinters.ts
Normal file
51
server/services/ocp/routes/printers/updatePrinters.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
// an external way to creating logs
|
||||
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
||||
import { responses } from "../../../../globalUtils/routeDefs/responses.js";
|
||||
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
||||
import { getPrinters } from "../../controller/getPrinters.js";
|
||||
import { updatePrinters } from "../../controller/updatePrinters.js";
|
||||
|
||||
const app = new OpenAPIHono({ strict: false });
|
||||
const CreateLog = z.object({
|
||||
line: z.string().openapi({ example: "info" }),
|
||||
initials: z.string().openapi({ example: "server" }),
|
||||
printReason: z.string().openapi({ example: "This is a new log posted" }),
|
||||
additionalComments: z
|
||||
.string()
|
||||
.optional()
|
||||
.openapi({ example: "Some reason why we did this." }),
|
||||
});
|
||||
|
||||
app.openapi(
|
||||
createRoute({
|
||||
tags: ["ocp"],
|
||||
summary: "Prints a label.",
|
||||
method: "get",
|
||||
path: "/updateprinters",
|
||||
//middleware: authMiddleware,
|
||||
//description: "This might be a temp soltuin during the transtion between versions",
|
||||
request: {
|
||||
body: { content: { "application/json": { schema: CreateLog } } },
|
||||
},
|
||||
responses: responses(),
|
||||
}),
|
||||
async (c) => {
|
||||
const { data: printData, error: printError } = await tryCatch(
|
||||
updatePrinters()
|
||||
);
|
||||
|
||||
if (printError) {
|
||||
return c.json({
|
||||
success: false,
|
||||
message: "There was an error getting the printers",
|
||||
});
|
||||
}
|
||||
|
||||
return c.json({
|
||||
success: printData.success,
|
||||
message: printData.message,
|
||||
data: printData.data,
|
||||
});
|
||||
}
|
||||
);
|
||||
export default app;
|
||||
Reference in New Issue
Block a user