test(notification): added framework for managment tab

This commit is contained in:
2025-04-14 12:27:45 -05:00
parent 17773e9a23
commit ae7ea2bb90
6 changed files with 143 additions and 16 deletions

View File

@@ -1,8 +1,22 @@
import { getnotifications } from "@/utils/querys/admin/notifications";
import { notifyColumns } from "@/utils/tableData/notifications/notifyColumns";
import { NotifyTable } from "@/utils/tableData/notifications/notifyData";
import { useQuery } from "@tanstack/react-query";
export default function NotificationMGT() {
const { data, isError, isLoading } = useQuery(getnotifications());
if (isLoading) return <div>Loading adjustmnet data...</div>;
if (isError) {
return (
<div>
<p>There was an error getting the adjustments.</p>
</div>
);
}
return (
<div>
Manage all notifications from here instad of going to the db,
locking some items that are auto updated on server restarts
<NotifyTable columns={notifyColumns} data={data} />
</div>
);
}

View File

@@ -1,4 +1,9 @@
import { createRootRoute, Link, Outlet } from "@tanstack/react-router";
import {
createRootRoute,
Link,
Outlet,
useLocation,
} from "@tanstack/react-router";
//import {TanStackRouterDevtools} from "@tanstack/router-devtools";
import Cookies from "js-cookie";
import { SidebarProvider } from "../components/ui/sidebar";
@@ -17,11 +22,12 @@ import {
import { SessionProvider } from "../components/providers/Providers";
import { Toaster } from "sonner";
//import { Button } from "../components/ui/button";
import { useSessionStore } from "../lib/store/sessionStore";
import { useSession } from "@/hooks/useSession";
import { useLogout } from "@/hooks/useLogout";
import ExportInventoryData from "@/components/logistics/warehouse/ExportInventoryData";
import { AddCards } from "@/components/dashboard/AddCards";
//import { AddCards } from "@/components/dashboard/AddCards";
// same as the layout
export const Route = createRootRoute({
@@ -30,16 +36,20 @@ export const Route = createRootRoute({
const { session } = useSession();
const { user } = useSessionStore();
const logout = useLogout();
const location = useLocation();
return (
<>
<div className="overflow-hidden">
<SessionProvider>
<ThemeProvider>
<nav className="flex justify-end">
<nav className="flex justify-end w-full shadow ">
<div className="m-2 flex flex-row">
<div className="m-auto pr-2">
<AddCards />
</div>
{location.pathname === "/" && (
<div className="m-auto pr-2 flex flex-row gap-2">
<ExportInventoryData />
<AddCards />
</div>
)}
<div className="m-1">
<ModeToggle />
</div>
@@ -88,16 +98,18 @@ export const Route = createRootRoute({
)}
</div>
</nav>
<SidebarProvider defaultOpen={sidebarState}>
<AppSidebar />
<Toaster expand={true} richColors closeButton />
<Outlet />
</SidebarProvider>
<main>
<SidebarProvider defaultOpen={sidebarState}>
<AppSidebar />
<Toaster expand={true} richColors closeButton />
<Outlet />
</SidebarProvider>
</main>
</ThemeProvider>
</SessionProvider>
{/* <TanStackRouterDevtools position="bottom-right" /> */}
</>
</div>
);
},
});

View File

@@ -0,0 +1,26 @@
import { queryOptions } from "@tanstack/react-query";
import axios from "axios";
export function getnotifications() {
const token = localStorage.getItem("auth_token");
return queryOptions({
queryKey: ["getNotifications"],
queryFn: () => fetchUsers(token),
enabled: !!token, // Prevents query if token is null
staleTime: 1000,
refetchInterval: 2 * 2000,
refetchOnWindowFocus: true,
});
}
const fetchUsers = async (token: string | null) => {
const { data } = await axios.get(`/api/notify/notifications`, {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
});
// if we are not localhost ignore the devDir setting.
//const url: string = window.location.host.split(":")[0];
return data.data ?? [];
};

View File

@@ -0,0 +1,30 @@
import { and, desc, eq, gte, inArray, lte, sql } from "drizzle-orm";
import { db } from "../../../../database/dbclient.js";
import { logs } from "../../../../database/schema/logs.js";
import { createLog } from "../../logger/logger.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
import { notifications } from "../../../../database/schema/notifications.js";
export const getNotifications = async () => {
const { data, error } = await tryCatch(db.select().from(notifications));
if (error) {
createLog(
"error",
"notify",
"notify",
`Error getting notifications: ${error}`
);
return {
success: false,
message: "Error getting notifications.",
data: error,
};
}
return {
sucess: true,
message: "Current notifications.",
data: data,
};
};

View File

@@ -11,10 +11,11 @@ import { startNotificationMonitor } from "./utils/processNotifications.js";
import notifyStats from "./routes/getActiveNotifications.js";
import tiTrigger from "./routes/manualTiggerTi.js";
import blocking from "./routes/qualityBlocking.js";
import notify from "./routes/getNotifications.js";
const app = new OpenAPIHono();
const routes = [sendemail, notifyStats, tiTrigger, blocking] as const;
const routes = [sendemail, notifyStats, tiTrigger, blocking, notify] as const;
const appRoutes = routes.forEach((route) => {
app.route("/notify", route);

View File

@@ -0,0 +1,44 @@
// an external way to creating logs
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
//import { apiHit } from "../../../globalUtils/apiHits.js";
import { responses } from "../../../globalUtils/routeDefs/responses.js";
import { getNotifications } from "../controller/getNotifications.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
const app = new OpenAPIHono({ strict: false });
app.openapi(
createRoute({
tags: ["notify"],
summary: "Gets notifications.",
method: "get",
path: "/notifications",
// description:
// "This might be a temp soltuin during the transtion between versions",
responses: responses(),
}),
async (c: any) => {
//apiHit(c, { endpoint: `api/logger/logs` });
const { data, error } = await tryCatch(getNotifications());
if (error) {
return c.json(
{
success: false,
message: "There was an error clearing the log.",
data: error,
},
400
);
}
return c.json(
{
success: data?.success,
message: data?.message,
data: data?.data,
},
200
);
}
);
export default app;