test(ocme): lots of changes to get it working in production

This commit is contained in:
2025-03-24 15:31:31 -05:00
parent 0c5fc1dfb0
commit 6dd9ed848b
14 changed files with 957 additions and 779 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;