diff --git a/frontend/src/components/admin/notificationMGT/NotificationMGT.tsx b/frontend/src/components/admin/notificationMGT/NotificationMGT.tsx index 77af83f..5f41177 100644 --- a/frontend/src/components/admin/notificationMGT/NotificationMGT.tsx +++ b/frontend/src/components/admin/notificationMGT/NotificationMGT.tsx @@ -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
Loading adjustmnet data...
; + if (isError) { + return ( +
+

There was an error getting the adjustments.

+
+ ); + } return (
- Manage all notifications from here instad of going to the db, - locking some items that are auto updated on server restarts +
); } diff --git a/frontend/src/routes/__root.tsx b/frontend/src/routes/__root.tsx index 6245bb3..21d6ff8 100644 --- a/frontend/src/routes/__root.tsx +++ b/frontend/src/routes/__root.tsx @@ -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 ( - <> +
-
); }, }); diff --git a/frontend/src/utils/querys/admin/notifications.tsx b/frontend/src/utils/querys/admin/notifications.tsx new file mode 100644 index 0000000..0e2fac6 --- /dev/null +++ b/frontend/src/utils/querys/admin/notifications.tsx @@ -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 ?? []; +}; diff --git a/server/services/notifications/controller/getNotifications.ts b/server/services/notifications/controller/getNotifications.ts new file mode 100644 index 0000000..73c9bdd --- /dev/null +++ b/server/services/notifications/controller/getNotifications.ts @@ -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, + }; +}; diff --git a/server/services/notifications/notifyService.ts b/server/services/notifications/notifyService.ts index 7fd1add..d6ba981 100644 --- a/server/services/notifications/notifyService.ts +++ b/server/services/notifications/notifyService.ts @@ -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); diff --git a/server/services/notifications/routes/getNotifications.ts b/server/services/notifications/routes/getNotifications.ts new file mode 100644 index 0000000..da53f90 --- /dev/null +++ b/server/services/notifications/routes/getNotifications.ts @@ -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;