feat(rfid): front end view of the readers and there status

This commit is contained in:
2025-07-10 21:29:01 -05:00
parent 6584b37cb0
commit f7b4de8130
15 changed files with 533 additions and 36 deletions

View File

@@ -1,23 +1,43 @@
import type { Context } from "hono";
import { createLog } from "../../services/logger/logger.js";
export type ReturnRes<T> =
| { success: true; message: string; data: T }
| { success: false; message: string; error: any };
export function returnRes<T>(
success: true,
message: string,
service: string,
user: string,
level: "info" | "error",
data: T
): { success: true; message: string; data: T };
export const returnRes = <T>(
success: boolean,
message: string,
data: T | null = null
): ReturnRes<T> => {
/**
* just a simple return to reduce the typing and make sure we are always consitant with our returns.
*
* data can be an error as well.
*/
return success
? { success, message, data: data as T }
: { success, message, error: data ?? "An unknown error occurred" };
};
export function returnRes<T>(
success: false,
message: string,
service: string,
user: string,
level: "info" | "error",
data?: T
): { success: false; message: string; error: T | string };
export function returnRes<T>(
success: boolean,
message: string,
service: string,
user: string,
level: "info" | "error",
data?: T
) {
createLog(level, user, service, message);
if (success) {
return { success: true, message, data: data as T };
} else {
return {
success: false,
message,
error: data ?? "An unknown error occurred",
};
}
}
// export const returnApi = (c:Context,success: boolean, message: string, data?: any, code: number)=>{
// /**

View File

@@ -12,13 +12,15 @@ export const getAllUsersRoles = async () => {
const { data, error } = await tryCatch(db.select().from(userRoles));
if (error) {
returnRes(
return returnRes(
false,
"auth",
"auth",
"There was an error getting users",
"error",
new Error("No user exists.")
);
}
returnRes(true, "All users.", data);
return { success: true, message: "All users", data };
return returnRes(true, "auth", "auth", "All users.", "info", data);
};

View File

@@ -32,7 +32,10 @@ export const getAllUsers = async () => {
if (error) {
returnRes(
false,
"auth",
"auth",
"There was an error getting users",
"error",
new Error("No user exists.")
);
}

View File

@@ -0,0 +1,21 @@
import { db } from "../../../../database/dbclient.js";
import { rfidReaders } from "../../../../database/schema/rfidReaders.js";
import { returnRes } from "../../../globalUtils/routeDefs/returnRes.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
export const getReaders = async () => {
const { data, error } = await tryCatch(db.select().from(rfidReaders));
if (error) {
return returnRes(
false,
"There was an error getting the Readers",
"rfid",
"rfid",
"error",
error
);
}
return returnRes(true, "Current readers", "rfid", "rfid", "info", data);
};

View File

@@ -52,6 +52,7 @@ export const badRead = async (reader: string) => {
lastTrigger: sql`NOW()`,
lastTriggerGood: false,
lastTagScanned: null,
badReads: sql`${rfidReaders.badReads} + 1`,
})
.where(eq(rfidReaders.reader, reader));
createLog(
@@ -85,7 +86,11 @@ export const goodRead = async (reader: string) => {
try {
const goodRead = await db
.update(rfidReaders)
.set({ lastTrigger: sql`NOW()`, lastTriggerGood: true })
.set({
lastTrigger: sql`NOW()`,
lastTriggerGood: true,
goodReads: sql`${rfidReaders.goodReads} + 1`,
})
.where(eq(rfidReaders.reader, reader));
createLog(
"info",

View File

@@ -1,13 +1,21 @@
import {OpenAPIHono} from "@hono/zod-openapi";
import { OpenAPIHono } from "@hono/zod-openapi";
import mgtEvents from "./route/mgtEvents.js";
import tagInfo from "./route/tagInfo.js";
import addReader from "./route/addReader.js";
import updateReader from "./route/updateReader.js";
import manualTrigger from "./route/manualTagRead.js";
import getReaders from "./route/getReaders.js";
const app = new OpenAPIHono();
const routes = [mgtEvents, tagInfo, addReader, updateReader, manualTrigger] as const;
const routes = [
mgtEvents,
tagInfo,
addReader,
updateReader,
manualTrigger,
getReaders,
] as const;
// app.route("/server", modules);
const appRoutes = routes.forEach((route) => {

View File

@@ -0,0 +1,43 @@
import { z, createRoute, OpenAPIHono } from "@hono/zod-openapi";
import { addReader } from "../controller/addReader.js";
import { authMiddleware } from "../../auth/middleware/authMiddleware.js";
import { responses } from "../../../globalUtils/routeDefs/responses.js";
import type { User } from "../../../types/users.js";
import { apiHit } from "../../../globalUtils/apiHits.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
import { getReaders } from "../controller/getReaders.js";
const app = new OpenAPIHono();
export const ReaderBody = z.object({
reader: z.string().openapi({ example: "wrapper1" }),
readerIP: z.string().openapi({ example: "192.168.1.52" }),
});
app.openapi(
createRoute({
tags: ["rfid"],
summary: "Get readers",
method: "get",
path: "/getreaders",
responses: responses(),
}),
async (c: any) => {
apiHit(c, { endpoint: "/getreaders" });
const { data, error } = await tryCatch(getReaders());
if (error) {
return c.json({
error,
});
}
return c.json({
data,
});
}
);
export default app;

View File

@@ -8,6 +8,23 @@ import { subModules } from "../../../../database/schema/subModules.js";
import { createLog } from "../../logger/logger.js";
// "view", "technician", "supervisor","manager", "admin", "systemAdmin"
const newSubModules = [
{
name: "RFID",
moduleName: "prodcution",
description: "RFID stuff",
link: "/rfid",
icon: "Tags",
active: true,
roles: [
"viewer",
"technician",
"supervisor",
"manager",
"admin",
"systemAdmin",
],
subSubModule: [],
},
{
name: "Silo Adjustments",
moduleName: "logistics",