Skip to content

Commit

Permalink
feature:Added HR role (#477)
Browse files Browse the repository at this point in the history
* feature:Added HR role

* feature:Added HR role
  • Loading branch information
Dhirajsingh212 authored Oct 7, 2024
1 parent 873e461 commit 9c41309
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ enum WorkMode {
enum Role {
USER
ADMIN
HR
}

enum EmployementType {
Expand Down
1 change: 1 addition & 0 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const prisma = new PrismaClient();
const users = [
{ id: '1', name: 'Jack', email: '[email protected]' },
{ id: '2', name: 'Admin', email: '[email protected]', role: Role.ADMIN },
{ id: '3', name: 'Hr', email: '[email protected]', role: Role.HR },
];

let jobs = [
Expand Down
9 changes: 7 additions & 2 deletions src/components/navitem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const NavItem = ({
}: {
path: string;
label: string;
roleRequired?: string;
roleRequired?: string[];
isPrivate?: boolean;
}) => {
const session = useSession();
Expand All @@ -22,7 +22,12 @@ export const NavItem = ({
if (!session.data?.user && isPrivate) {
return;
}
if (session && roleRequired && session.data?.user.role !== roleRequired) {
if (
session &&
roleRequired &&
session.data?.user.role &&
!roleRequired.includes(session.data?.user.role)
) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions src/config/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const ADMIN_ROLE = 'ADMIN';
export const HR_ROLE = 'HR';
export const USER_ROLE = 'USER';
export const JOBS_PER_PAGE = 10;
export const DEFAULT_PAGE = 1;
5 changes: 3 additions & 2 deletions src/layouts/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Image from 'next/image';
import { Skeleton } from '@/components/ui/skeleton';
import { Moon, Sun } from 'lucide-react';
import { useTheme } from 'next-themes';
import { ADMIN_ROLE } from '@/config/app.config';
import { ADMIN_ROLE, HR_ROLE } from '@/config/app.config';
import { useEffect, useState } from 'react';
import { Button } from '@/components/ui/button';
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
Expand Down Expand Up @@ -76,7 +76,8 @@ const Header = () => {
<Skeleton className="h-4 w-[60px]" key={index} />
))
: session.data?.user
? session.data?.user.role === ADMIN_ROLE
? session.data?.user.role === ADMIN_ROLE ||
session.data?.user.role === HR_ROLE
? adminNavbar.map((item) => (
<NavItem {...item} key={item.id} />
))
Expand Down
14 changes: 10 additions & 4 deletions src/layouts/mobile-nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { signOut, useSession } from 'next-auth/react';
import Link from 'next/link';
import { usePathname, useRouter } from 'next/navigation';
import { CompanyLogo } from './header';
import { ADMIN_ROLE, USER_ROLE } from '@/config/app.config';
import { ADMIN_ROLE, HR_ROLE, USER_ROLE } from '@/config/app.config';
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
import { getNameInitials } from '@/lib/utils';
export function MobileNav() {
Expand Down Expand Up @@ -78,7 +78,8 @@ export function MobileNav() {
<ul className="flex flex-col gap-2 text-sm justify-items-start px-4 py-2">
{session.status !== 'loading' && session.data?.user && (
<div className="w-full flex items-center">
{session.data?.user.role === ADMIN_ROLE ? (
{session.data?.user.role === ADMIN_ROLE ||
session.data?.user.role === HR_ROLE ? (
<div className="w-12 h-12 rounded-full flex items-center justify-center border-none ouline-none dark:bg-[#0F172A] dark:text-white bg-slate-200">
<p>HS</p>
</div>
Expand Down Expand Up @@ -165,7 +166,7 @@ const Item = ({
}: {
path: string;
label: string;
roleRequired?: string;
roleRequired?: string[];
isPrivate?: boolean;
}) => {
const session = useSession();
Expand All @@ -176,7 +177,12 @@ const Item = ({
if (!session.data?.user && isPrivate) {
return;
}
if (session && roleRequired && session.data?.user.role !== roleRequired)
if (
session &&
roleRequired &&
session.data?.user.role &&
!roleRequired.includes(session.data?.user.role)
)
return;
return (
<li className="my-1 dark:hover:bg-slate-800 hover:bg-slate-50 p-2 rounded-lg">
Expand Down
4 changes: 2 additions & 2 deletions src/lib/constant/app.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ export const adminNavbar = [
id: 2,
label: 'Manage Jobs',
path: APP_PATHS.MANAGE_JOBS,
roleRequired: 'ADMIN',
roleRequired: ['ADMIN', 'HR'],
icon: PackageSearch,
},
{
id: 3,
label: 'Post a job',
path: APP_PATHS.POST_JOB,
roleRequired: 'ADMIN',
roleRequired: ['ADMIN', 'HR'],
icon: PackageSearch,
},
];
Expand Down
6 changes: 5 additions & 1 deletion src/middleware.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ export async function middleware(req: NextRequest) {
if (!token && pathname === '/create') {
return NextResponse.redirect(new URL('/signin', req.url));
}
if (pathname === '/create' && token?.role !== 'ADMIN') {
if (
pathname === '/create' &&
token?.role !== 'ADMIN' &&
token?.role !== 'HR'
) {
return NextResponse.redirect(new URL('/', req.url));
}
return NextResponse.next();
Expand Down

0 comments on commit 9c41309

Please sign in to comment.