refactor(ocme): corrections to endpoints to work with ocnme as intneeded

This commit is contained in:
2025-03-25 07:54:58 -05:00
parent 73aa95a693
commit e6e1cecce3
8 changed files with 458 additions and 357 deletions

View File

@@ -1,60 +1,71 @@
import {createRoute, OpenAPIHono, z} from "@hono/zod-openapi";
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
import {apiHit} from "../../../globalUtils/apiHits.js";
import {responses} from "../../../globalUtils/routeDefs/responses.js";
import {authMiddleware} from "../../auth/middleware/authMiddleware.js";
import {cycleCount} from "../controller/cycleCount.js";
import type {User} from "../../../types/users.js";
import {verify} from "hono/jwt";
import { apiHit } from "../../../globalUtils/apiHits.js";
import { responses } from "../../../globalUtils/routeDefs/responses.js";
import { authMiddleware } from "../../auth/middleware/authMiddleware.js";
import { cycleCount } from "../controller/cycleCount.js";
import type { User } from "../../../types/users.js";
import { verify } from "hono/jwt";
const app = new OpenAPIHono({strict: false});
const app = new OpenAPIHono({ strict: false });
const AddSetting = z.object({
lane: z.string().openapi({example: "L064"}),
lane: z.string().openapi({ example: "L064" }),
});
app.openapi(
createRoute({
tags: ["ocme"],
summary: "Cycle counts a lane based on the lane Alias",
method: "post",
path: "/cyclecount",
middleware: authMiddleware,
request: {
body: {
content: {
"application/json": {schema: AddSetting},
},
},
createRoute({
tags: ["ocme"],
summary: "Cycle counts a lane based on the lane Alias",
method: "post",
path: "/cycleCount",
middleware: authMiddleware,
request: {
body: {
content: {
"application/json": { schema: AddSetting },
},
responses: responses(),
}),
async (c) => {
apiHit(c, {endpoint: "api/auth/register"});
// make sure we have a vaid user being accessed thats really logged in
const body = await c.req.json();
},
},
responses: responses(),
}),
async (c) => {
apiHit(c, { endpoint: "api/auth/register" });
// make sure we have a vaid user being accessed thats really logged in
const body = await c.req.json();
const authHeader = c.req.header("Authorization");
const authHeader = c.req.header("Authorization");
const token = authHeader?.split("Bearer ")[1] || "";
let user: User;
const token = authHeader?.split("Bearer ")[1] || "";
let user: User;
try {
const payload = await verify(token, process.env.JWT_SECRET!);
user = payload.user as User;
} catch (error) {
return c.json({message: "Unauthorized"}, 401);
}
try {
const cycleData = await cycleCount(body, user);
return c.json({success: true, message: `${body.lane} was just cycle counted.`, data: cycleData}, 200);
} catch (error) {
return c.json(
{success: false, message: `There was an error cycle counting ${body.lane}`, data: error},
400
);
}
try {
const payload = await verify(token, process.env.JWT_SECRET!);
user = payload.user as User;
} catch (error) {
return c.json({ message: "Unauthorized" }, 401);
}
try {
const cycleData = await cycleCount(body, user);
return c.json(
{
success: true,
message: `${body.lane} was just cycle counted.`,
data: cycleData,
},
200
);
} catch (error) {
return c.json(
{
success: false,
message: `There was an error cycle counting ${body.lane}`,
data: error,
},
400
);
}
}
);
export default app;

View File

@@ -1,94 +1,114 @@
import {createRoute, OpenAPIHono, z} from "@hono/zod-openapi";
import {apiHit} from "../../../globalUtils/apiHits.js";
import {getShipmentPallets} from "../controller/getShipmentPallets.js";
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
import { apiHit } from "../../../globalUtils/apiHits.js";
import { getShipmentPallets } from "../controller/getShipmentPallets.js";
const app = new OpenAPIHono();
const ShipmentID = z.object({
shipmentID: z.string().optional().openapi({example: "14558"}),
shipmentID: z.string().optional().openapi({ example: "14558" }),
});
app.openapi(
createRoute({
tags: ["ocme"],
summary: "Post New running number to be picked up.",
method: "post",
path: "/getshipmentpallets",
request: {
body: {
content: {
"application/json": {schema: ShipmentID},
},
},
createRoute({
tags: ["ocme"],
summary: "Post New running number to be picked up.",
method: "post",
path: "/GetShipmentPallets",
request: {
body: {
content: {
"application/json": { schema: ShipmentID },
},
responses: {
200: {
content: {
"application/json": {
schema: z.object({
success: z.boolean().openapi({example: true}),
message: z.string().openapi({example: "Starter"}),
// data: z
// .array(z.object({sscc: z.string().optional()}))
// .optional()
// .openapi({example: []}),
}),
},
},
description: "Response message",
},
400: {
content: {
"application/json": {
schema: z.object({
success: z.boolean().openapi({example: false}),
message: z.string().optional().openapi({example: "Internal Server error"}),
data: z.array(z.object({})).optional().openapi({example: []}),
}),
},
},
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",
// },
},
},
responses: {
200: {
content: {
"application/json": {
schema: z.object({
success: z.boolean().openapi({ example: true }),
message: z.string().openapi({ example: "Starter" }),
// data: z
// .array(z.object({sscc: z.string().optional()}))
// .optional()
// .openapi({example: []}),
}),
},
},
}),
async (c) => {
// make sure we have a vaid user being accessed thats really logged in
try {
const data = await c.req.json();
apiHit(c, {endpoint: "api/ocme/getshipmentpallets", lastBody: data});
description: "Response message",
},
400: {
content: {
"application/json": {
schema: z.object({
success: z.boolean().openapi({ example: false }),
message: z
.string()
.optional()
.openapi({ example: "Internal Server error" }),
data: z.array(z.object({})).optional().openapi({ example: [] }),
}),
},
},
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) => {
// make sure we have a vaid user being accessed thats really logged in
try {
const data = await c.req.json();
apiHit(c, { endpoint: "api/ocme/getshipmentpallets", lastBody: data });
if (!data.shipmentID) {
return c.json(
{success: false, message: "You are missing the shipment id please try again.", data: []},
400
);
}
console.log;
const shiptmentData = await getShipmentPallets(data.shipmentID);
if (!data.shipmentID) {
return c.json(
{
success: false,
message: "You are missing the shipment id please try again.",
data: [],
},
400
);
}
return c.json(
{success: shiptmentData.success, message: shiptmentData.message, data: shiptmentData.data ?? []},
200
);
} catch (error) {
return c.json({success: false, message: "There was an error getting the shipment data.", data: error}, 400);
}
const shiptmentData = await getShipmentPallets(data.shipmentID);
return c.json(
{
success: shiptmentData.success,
message: shiptmentData.message,
data: shiptmentData.data ?? [],
},
200
);
} catch (error) {
return c.json(
{
success: false,
message: "There was an error getting the shipment data.",
data: error,
},
400
);
}
}
);
export default app;