test(printers): started printer migration

This commit is contained in:
2025-03-16 15:33:44 -05:00
parent d178e04362
commit 04a607d3bc
3 changed files with 118 additions and 0 deletions

View File

@@ -16,6 +16,7 @@ import ocme from "./services/ocme/ocmeService.js";
import sqlService from "./services/sqlServer/sqlService.js"; import sqlService from "./services/sqlServer/sqlService.js";
import logistics from "./services/logistics/logisticsService.js"; import logistics from "./services/logistics/logisticsService.js";
import rfid from "./services/rfid/rfidService.js"; import rfid from "./services/rfid/rfidService.js";
import printers from "./services/printers/printerService.js";
import {db} from "../database/dbclient.js"; import {db} from "../database/dbclient.js";
import {settings} from "../database/schema/settings.js"; import {settings} from "../database/schema/settings.js";
@@ -83,6 +84,7 @@ const routes = [
sqlService, sqlService,
logistics, logistics,
rfid, rfid,
printers,
] as const; ] as const;
const appRoutes = routes.forEach((route) => { const appRoutes = routes.forEach((route) => {

View File

@@ -0,0 +1,14 @@
import {OpenAPIHono} from "@hono/zod-openapi";
import alerts from "./route/printerAlert.js";
const app = new OpenAPIHono();
const port = 4000;
const routes = [alerts] as const;
// app.route("/server", modules);
const appRoutes = routes.forEach((route) => {
app.route("/printers", route);
});
export default app;

View File

@@ -0,0 +1,102 @@
//http://usday1vms006:4000/api/v1/zebra/wrapper1
import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi";
// Define the response schema
const responseSchema = z.object({
success: z.boolean().openapi({example: true}),
message: z.string().optional(),
});
const app = new OpenAPIHono();
const ParamsSchema = z.object({
printer: z
.string()
.min(3)
.openapi({
param: {
name: "printer",
in: "path",
},
example: "Line1",
}),
});
app.openapi(
createRoute({
tags: ["printer"],
summary: "Printer Alert",
method: "post",
path: "/{printer}",
request: {
params: ParamsSchema,
},
responses: {
200: {
content: {
"application/json": {schema: responseSchema},
},
description: "Response message",
},
400: {
content: {
"application/json": {
schema: z.object({message: z.string().optional().openapi({example: "Internal Server error"})}),
},
},
description: "Internal Server Error",
},
401: {
content: {
"application/json": {
schema: z.object({message: z.string().optional().openapi({example: "Unauthenticated"})}),
},
},
description: "Unauthorized",
},
500: {
content: {
"application/json": {
schema: z.object({message: z.string().optional().openapi({example: "Internal Server error"})}),
},
},
description: "Internal Server Error",
},
},
}),
async (c) => {
const {printer} = c.req.valid("param");
const contentType = c.req.header("Content-Type") || "";
const boundaryMatch = contentType.match(/boundary=(.*)$/);
if (!boundaryMatch) {
return c.json({message: "No boundary found in Content-Type"}, 400);
}
const boundary = boundaryMatch[1];
const body = await c.req.text(); // Get the body of the request
// Split the body by the boundary (adding extra dashes before the boundary)
const parts = body.split(`--${boundary}`);
console.log(parts);
// Remove the first and last empty parts (they are just before and after the boundaries)
const formDataParts = parts.slice(1, parts.length - 1);
// console.log(formDataParts);
// formDataParts.forEach((part, index) => {
// // Split the part into headers and body
// const [headers, data] = part.split("\r\n\r\n");
// // Log the part index and data for debugging
// console.log(`Part ${index + 1}:`);
// console.log("Headers:", headers);
// console.log("Data:", decodeURIComponent(data));
// });
return c.json({success: true, message: `New info from ${printer}`}, 200);
}
);
export default app;