Files
lstV2/server/services/ocme/route/cycleCount.ts

61 lines
1.9 KiB
TypeScript

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";
const app = new OpenAPIHono({strict: false});
const AddSetting = z.object({
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},
},
},
},
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 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
);
}
}
);
export default app;