diff --git a/frontend/src/components/layout/side-components/production.tsx b/frontend/src/components/layout/side-components/production.tsx index a6d00b3..05dc75a 100644 --- a/frontend/src/components/layout/side-components/production.tsx +++ b/frontend/src/components/layout/side-components/production.tsx @@ -1,4 +1,4 @@ -import { Printer } from "lucide-react"; +import { Printer, Tag } from "lucide-react"; import { SidebarGroup, SidebarGroupContent, @@ -10,34 +10,6 @@ import { import { hasPageAccess } from "@/utils/userAccess"; import { User } from "@/types/users"; -const items = [ - { - title: "One Click Print", - url: "/ocp", - icon: Printer, - role: ["viewer"], - module: "ocp", - active: true, - }, - { - title: "RFID", - moduleName: "prodcution", - description: "RFID stuff", - url: "/rfid", - icon: "Tags", - active: true, - roles: [ - "viewer", - "technician", - "supervisor", - "manager", - "admin", - "systemAdmin", - ], - subSubModule: [], - }, -]; - export function ProductionSideBar({ user, moduleID, @@ -45,6 +17,26 @@ export function ProductionSideBar({ user: User | null; moduleID: string; }) { + const url: string = window.location.host.split(":")[0]; + const items = [ + { + title: "One Click Print", + url: "/ocp", + icon: Printer, + role: ["viewer"], + module: "ocp", + active: true, + }, + { + title: "Rfid Readers", + url: "/rfid", + icon: Tag, + role: ["viewer"], + module: "production", + active: + url === "usday1vms006" || url === "localhost" ? true : false, + }, + ]; return ( Production diff --git a/frontend/src/utils/tableData/rfid/readers/readerColumns.tsx b/frontend/src/utils/tableData/rfid/readers/readerColumns.tsx index 67b2ad6..445f92b 100644 --- a/frontend/src/utils/tableData/rfid/readers/readerColumns.tsx +++ b/frontend/src/utils/tableData/rfid/readers/readerColumns.tsx @@ -1,6 +1,12 @@ //import { fixTime } from "@/utils/fixTime"; +import { Button } from "@/components/ui/button"; +import { getReaders } from "@/utils/querys/rfid/getReaders"; +import { useQuery } from "@tanstack/react-query"; import { ColumnDef } from "@tanstack/react-table"; +import axios from "axios"; import { format } from "date-fns-tz"; +import { useState } from "react"; +import { toast } from "sonner"; // This type is used to define the shape of our data. // You can use a Zod schema here if you want. @@ -94,4 +100,49 @@ export const readerColumns: ColumnDef[] = [ ); }, }, + { + accessorKey: "reset", + header: "Reset Reads", + cell: ({ row }) => { + const { refetch } = useQuery(getReaders()); + const [readerReset, setReaderReset] = useState(false); + // const goodRatio = + // (parseInt(row.getValue("goodReads")) / + // (parseInt(row.getValue("goodReads")) + + // parseInt(row.getValue("badReads")))) * + // 100; + const name = row.getValue("reader"); + const resetReads = async () => { + setReaderReset(true); + try { + const res = await axios.post("/api/rfid/resetRatio", { + reader: name, + }); + + if (res.status === 200) { + toast.success(res.data.message); + setReaderReset(false); + } else { + toast.error(res.data.message); + setReaderReset(false); + } + } catch (error: any) { + toast.error(error.data.message); + setReaderReset(false); + } + refetch(); + }; + return ( +
+ +
+ ); + }, + }, ]; diff --git a/server/services/rfid/controller/readerControl.ts b/server/services/rfid/controller/readerControl.ts index 41a4908..ba5ef24 100644 --- a/server/services/rfid/controller/readerControl.ts +++ b/server/services/rfid/controller/readerControl.ts @@ -6,6 +6,7 @@ import { eq, sql } from "drizzle-orm"; import { db } from "../../../../database/dbclient.js"; import { rfidReaders } from "../../../../database/schema/rfidReaders.js"; import { createLog } from "../../logger/logger.js"; +import { tryCatch } from "../../../globalUtils/tryCatch.js"; export const newHeartBeat = async (reader: string) => { /** @@ -79,6 +80,36 @@ export const badRead = async (reader: string) => { } }; +export const resetRatios = async (reader: string) => { + const { error } = await tryCatch( + db + .update(rfidReaders) + .set({ + goodReads: 0, + badReads: 0, + }) + .where(eq(rfidReaders.reader, reader)) + ); + + if (error) { + createLog( + "error", + "rfid", + "rfid", + `${reader} encountered an error resetting the data.` + ); + return { + success: false, + message: `${reader} encountered an error resetting the data.`, + }; + } + createLog("info", "rfid", "rfid", `${reader} just had the tag data reset.`); + return { + success: true, + message: `${reader} just had the tag data reset.`, + }; +}; + export const goodRead = async (reader: string) => { /** * When we have a bad read we want to make sure the reader shows this was well. diff --git a/server/services/rfid/rfidService.ts b/server/services/rfid/rfidService.ts index dcda4f3..f9c6089 100644 --- a/server/services/rfid/rfidService.ts +++ b/server/services/rfid/rfidService.ts @@ -6,6 +6,7 @@ import addReader from "./route/addReader.js"; import updateReader from "./route/updateReader.js"; import manualTrigger from "./route/manualTagRead.js"; import getReaders from "./route/getReaders.js"; +import resetRatio from "./route/resetRatio.js"; const app = new OpenAPIHono(); const routes = [ @@ -15,6 +16,7 @@ const routes = [ updateReader, manualTrigger, getReaders, + resetRatio, ] as const; // app.route("/server", modules); diff --git a/server/services/rfid/route/resetRatio.ts b/server/services/rfid/route/resetRatio.ts new file mode 100644 index 0000000..5c81193 --- /dev/null +++ b/server/services/rfid/route/resetRatio.ts @@ -0,0 +1,54 @@ +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 { verify } from "hono/jwt"; +import { apiHit } from "../../../globalUtils/apiHits.js"; +import { resetRatios } from "../controller/readerControl.js"; + +const app = new OpenAPIHono(); + +export const ReaderBody = z.object({ + reader: z.string().openapi({ example: "wrapper1" }), +}); + +app.openapi( + createRoute({ + tags: ["rfid"], + summary: "Resets the ratio on the reader", + method: "post", + path: "/resetRatio", + //middleware: authMiddleware, + //description: "Adding in a new reader to add to the network.", + request: { + body: { content: { "application/json": { schema: ReaderBody } } }, + }, + responses: responses(), + }), + async (c) => { + const body = await c.req.json(); + apiHit(c, { endpoint: "/resetRatio", lastBody: body }); + + try { + const reset = await resetRatios(body.reader); + return c.json( + { + success: reset.success, + message: reset.message, + }, + 200 + ); + } catch (error) { + return c.json( + { + success: false, + message: `${body.name} encountered an error while trying to reset the readers ratio`, + }, + 400 + ); + } + } +); + +export default app;