feat(frontend): added in checks for links inside the module

This commit is contained in:
2025-03-06 19:34:10 -06:00
parent 4195b9e8bc
commit c30a48c4b8
4 changed files with 126 additions and 23 deletions

View File

@@ -20,7 +20,12 @@ export function AppSidebar() {
<SidebarContent>
<Header />
{moduleActive("production") && <ProductionSideBar />}
{moduleActive("logistics") && hasAccess(user, "logistics", modules) && <LogisticsSideBar />}
{moduleActive("logistics") && (
<LogisticsSideBar
user={user}
moduleID={modules.filter((n) => n.name === "logistics")[0].module_id as string}
/>
)}
{moduleActive("forklift") && hasAccess(user, "forklift", modules) && <ForkliftSideBar />}
{moduleActive("eom") && hasAccess(user, "eom", modules) && <EomSideBar />}
{moduleActive("quality") && hasAccess(user, "quality", modules) && <QualitySideBar />}

View File

@@ -7,35 +7,45 @@ import {
SidebarMenuButton,
SidebarMenuItem,
} from "../../ui/sidebar";
import {hasPageAccess} from "@/utils/userAccess";
import {User} from "@/types/users";
// this will need to be moved to a links section the db to make it more easy to remove and add
const items = [
{
title: "Silo Adjustments",
url: "#",
icon: Cylinder,
role: ["technician", "supervisor", "manager", "admin", "systemAdmin"],
module: "logistics",
active: true,
},
{
title: "Bulk orders",
url: "#",
icon: Truck,
role: ["technician", "supervisor", "manager", "admin", "systemAdmin"],
module: "logistics",
active: true,
},
{
title: "Forecast",
url: "#",
icon: Truck,
role: ["technician", "supervisor", "manager", "admin", "systemAdmin"],
module: "logistics",
active: true,
},
{
title: "Ocme cycle counts",
url: "#",
icon: Package,
role: ["technician", "supervisor", "manager", "admin", "systemAdmin"],
module: "logistics",
active: false,
},
];
export function LogisticsSideBar() {
export function LogisticsSideBar({user, moduleID}: {user: User | null; moduleID: string}) {
return (
<SidebarGroup>
<SidebarGroupLabel>Logistics</SidebarGroupLabel>
@@ -43,12 +53,16 @@ export function LogisticsSideBar() {
<SidebarMenu>
{items.map((item) => (
<SidebarMenuItem key={item.title}>
<SidebarMenuButton asChild>
<a href={item.url}>
<item.icon />
<span>{item.title}</span>
</a>
</SidebarMenuButton>
<>
{hasPageAccess(user, item.role, moduleID) && item.active && (
<SidebarMenuButton asChild>
<a href={item.url}>
<item.icon />
<span>{item.title}</span>
</a>
</SidebarMenuButton>
)}
</>
</SidebarMenuItem>
))}
</SidebarMenu>

View File

@@ -23,7 +23,10 @@ import { Route as EomEomImport } from './routes/_eom/eom'
import { Route as AuthProfileImport } from './routes/_auth/profile'
import { Route as AdminSettingsImport } from './routes/_admin/settings'
import { Route as AdminModulesImport } from './routes/_admin/modules'
import { Route as logisticsMaterialHelperIndexImport } from './routes/(logistics)/materialHelper/index'
import { Route as EomArticleAvImport } from './routes/_eom/article/$av'
import { Route as logisticsMaterialHelperSiloLinkIndexImport } from './routes/(logistics)/materialHelper/siloLink/index'
import { Route as logisticsMaterialHelperConsumptionIndexImport } from './routes/(logistics)/materialHelper/consumption/index'
// Create/Update Routes
@@ -96,12 +99,33 @@ const AdminModulesRoute = AdminModulesImport.update({
getParentRoute: () => AdminRoute,
} as any)
const logisticsMaterialHelperIndexRoute =
logisticsMaterialHelperIndexImport.update({
id: '/(logistics)/materialHelper/',
path: '/materialHelper/',
getParentRoute: () => rootRoute,
} as any)
const EomArticleAvRoute = EomArticleAvImport.update({
id: '/article/$av',
path: '/article/$av',
getParentRoute: () => EomRoute,
} as any)
const logisticsMaterialHelperSiloLinkIndexRoute =
logisticsMaterialHelperSiloLinkIndexImport.update({
id: '/(logistics)/materialHelper/siloLink/',
path: '/materialHelper/siloLink/',
getParentRoute: () => rootRoute,
} as any)
const logisticsMaterialHelperConsumptionIndexRoute =
logisticsMaterialHelperConsumptionIndexImport.update({
id: '/(logistics)/materialHelper/consumption/',
path: '/materialHelper/consumption/',
getParentRoute: () => rootRoute,
} as any)
// Populate the FileRoutesByPath interface
declare module '@tanstack/react-router' {
@@ -197,6 +221,27 @@ declare module '@tanstack/react-router' {
preLoaderRoute: typeof EomArticleAvImport
parentRoute: typeof EomImport
}
'/(logistics)/materialHelper/': {
id: '/(logistics)/materialHelper/'
path: '/materialHelper'
fullPath: '/materialHelper'
preLoaderRoute: typeof logisticsMaterialHelperIndexImport
parentRoute: typeof rootRoute
}
'/(logistics)/materialHelper/consumption/': {
id: '/(logistics)/materialHelper/consumption/'
path: '/materialHelper/consumption'
fullPath: '/materialHelper/consumption'
preLoaderRoute: typeof logisticsMaterialHelperConsumptionIndexImport
parentRoute: typeof rootRoute
}
'/(logistics)/materialHelper/siloLink/': {
id: '/(logistics)/materialHelper/siloLink/'
path: '/materialHelper/siloLink'
fullPath: '/materialHelper/siloLink'
preLoaderRoute: typeof logisticsMaterialHelperSiloLinkIndexImport
parentRoute: typeof rootRoute
}
}
}
@@ -248,6 +293,9 @@ export interface FileRoutesByFullPath {
'/ocp/lots': typeof OcpLotsRoute
'/ocp': typeof OcpIndexRoute
'/article/$av': typeof EomArticleAvRoute
'/materialHelper': typeof logisticsMaterialHelperIndexRoute
'/materialHelper/consumption': typeof logisticsMaterialHelperConsumptionIndexRoute
'/materialHelper/siloLink': typeof logisticsMaterialHelperSiloLinkIndexRoute
}
export interface FileRoutesByTo {
@@ -262,6 +310,9 @@ export interface FileRoutesByTo {
'/ocp/lots': typeof OcpLotsRoute
'/ocp': typeof OcpIndexRoute
'/article/$av': typeof EomArticleAvRoute
'/materialHelper': typeof logisticsMaterialHelperIndexRoute
'/materialHelper/consumption': typeof logisticsMaterialHelperConsumptionIndexRoute
'/materialHelper/siloLink': typeof logisticsMaterialHelperSiloLinkIndexRoute
}
export interface FileRoutesById {
@@ -279,6 +330,9 @@ export interface FileRoutesById {
'/ocp/lots': typeof OcpLotsRoute
'/ocp/': typeof OcpIndexRoute
'/_eom/article/$av': typeof EomArticleAvRoute
'/(logistics)/materialHelper/': typeof logisticsMaterialHelperIndexRoute
'/(logistics)/materialHelper/consumption/': typeof logisticsMaterialHelperConsumptionIndexRoute
'/(logistics)/materialHelper/siloLink/': typeof logisticsMaterialHelperSiloLinkIndexRoute
}
export interface FileRouteTypes {
@@ -295,6 +349,9 @@ export interface FileRouteTypes {
| '/ocp/lots'
| '/ocp'
| '/article/$av'
| '/materialHelper'
| '/materialHelper/consumption'
| '/materialHelper/siloLink'
fileRoutesByTo: FileRoutesByTo
to:
| '/'
@@ -308,6 +365,9 @@ export interface FileRouteTypes {
| '/ocp/lots'
| '/ocp'
| '/article/$av'
| '/materialHelper'
| '/materialHelper/consumption'
| '/materialHelper/siloLink'
id:
| '__root__'
| '/'
@@ -323,6 +383,9 @@ export interface FileRouteTypes {
| '/ocp/lots'
| '/ocp/'
| '/_eom/article/$av'
| '/(logistics)/materialHelper/'
| '/(logistics)/materialHelper/consumption/'
| '/(logistics)/materialHelper/siloLink/'
fileRoutesById: FileRoutesById
}
@@ -335,6 +398,9 @@ export interface RootRouteChildren {
LoginRoute: typeof LoginRoute
OcpLotsRoute: typeof OcpLotsRoute
OcpIndexRoute: typeof OcpIndexRoute
logisticsMaterialHelperIndexRoute: typeof logisticsMaterialHelperIndexRoute
logisticsMaterialHelperConsumptionIndexRoute: typeof logisticsMaterialHelperConsumptionIndexRoute
logisticsMaterialHelperSiloLinkIndexRoute: typeof logisticsMaterialHelperSiloLinkIndexRoute
}
const rootRouteChildren: RootRouteChildren = {
@@ -346,6 +412,11 @@ const rootRouteChildren: RootRouteChildren = {
LoginRoute: LoginRoute,
OcpLotsRoute: OcpLotsRoute,
OcpIndexRoute: OcpIndexRoute,
logisticsMaterialHelperIndexRoute: logisticsMaterialHelperIndexRoute,
logisticsMaterialHelperConsumptionIndexRoute:
logisticsMaterialHelperConsumptionIndexRoute,
logisticsMaterialHelperSiloLinkIndexRoute:
logisticsMaterialHelperSiloLinkIndexRoute,
}
export const routeTree = rootRoute
@@ -365,7 +436,10 @@ export const routeTree = rootRoute
"/about",
"/login",
"/ocp/lots",
"/ocp/"
"/ocp/",
"/(logistics)/materialHelper/",
"/(logistics)/materialHelper/consumption/",
"/(logistics)/materialHelper/siloLink/"
]
},
"/": {
@@ -422,6 +496,15 @@ export const routeTree = rootRoute
"/_eom/article/$av": {
"filePath": "_eom/article/$av.tsx",
"parent": "/_eom"
},
"/(logistics)/materialHelper/": {
"filePath": "(logistics)/materialHelper/index.tsx"
},
"/(logistics)/materialHelper/consumption/": {
"filePath": "(logistics)/materialHelper/consumption/index.tsx"
},
"/(logistics)/materialHelper/siloLink/": {
"filePath": "(logistics)/materialHelper/siloLink/index.tsx"
}
}
}

View File

@@ -1,17 +1,5 @@
import {Modules} from "@/types/modules";
type User = {
user_id: string;
email: string;
username: string;
roles: Roles[];
role: string;
};
interface Roles {
role: string;
module_id: string;
}
import {User} from "@/types/users";
// user will need access to the module.
// users role will determine there visual access
@@ -23,3 +11,16 @@ export function hasAccess(user: User | null, moduleName: string | null, modules:
return user?.roles.find((role) => role.module_id === filteredModule[0].module_id) ? true : false;
}
export function hasPageAccess(user: User | null, role: any, module_id: string): boolean {
if (!user) return false;
// get only the module in the user profile
const userRole = user?.roles.filter((role) => role.module_id === module_id);
if (role.includes(userRole[0].role)) {
return true;
}
return false;
}