| @@ -11,12 +11,13 @@ import { downloadFile } from "@/app/utils/commonUtil"; | |||||
| interface Props { | interface Props { | ||||
| team: TeamResult[]; | team: TeamResult[]; | ||||
| customer: Customer[]; | customer: Customer[]; | ||||
| needAll: boolean | undefined; | |||||
| } | } | ||||
| type SearchQuery = Partial<Omit<CostAndExpenseReportFilter, "id">>; | type SearchQuery = Partial<Omit<CostAndExpenseReportFilter, "id">>; | ||||
| type SearchParamNames = keyof SearchQuery; | type SearchParamNames = keyof SearchQuery; | ||||
| const CostAndExpenseReport: React.FC<Props> = ({ team, customer }) => { | |||||
| const CostAndExpenseReport: React.FC<Props> = ({ team, customer, needAll }) => { | |||||
| const { t } = useTranslation("report"); | const { t } = useTranslation("report"); | ||||
| const teamCombo = team.map((t) => `${t.name} - ${t.code}`); | const teamCombo = team.map((t) => `${t.name} - ${t.code}`); | ||||
| const custCombo = customer.map(c => ({label: `${c.name} - ${c.code}`, value: c.id})) | const custCombo = customer.map(c => ({label: `${c.name} - ${c.code}`, value: c.id})) | ||||
| @@ -28,7 +29,7 @@ const CostAndExpenseReport: React.FC<Props> = ({ team, customer }) => { | |||||
| paramName: "team", | paramName: "team", | ||||
| type: "select", | type: "select", | ||||
| options: teamCombo, | options: teamCombo, | ||||
| needAll: true, | |||||
| needAll: needAll, | |||||
| }, | }, | ||||
| { | { | ||||
| label: t("Client"), | label: t("Client"), | ||||
| @@ -1,18 +1,30 @@ | |||||
| import React from "react"; | import React from "react"; | ||||
| import { fetchAllCustomers } from "@/app/api/customer"; | import { fetchAllCustomers } from "@/app/api/customer"; | ||||
| import { fetchTeam } from "@/app/api/team"; | |||||
| import { fetchIndivTeam, fetchTeam } from "@/app/api/team"; | |||||
| import CostAndExpenseReport from "./CostAndExpenseReport"; | import CostAndExpenseReport from "./CostAndExpenseReport"; | ||||
| import CostAndExpenseReportLoading from "./CostAndExpenseReportLoading"; | import CostAndExpenseReportLoading from "./CostAndExpenseReportLoading"; | ||||
| import { headers, cookies } from 'next/headers'; | |||||
| import { getServerSession } from "next-auth"; | |||||
| import { authOptions } from "@/config/authConfig"; | |||||
| import { TEAM_LEAD } from "@/middleware"; | |||||
| interface SubComponents { | interface SubComponents { | ||||
| Loading: typeof CostAndExpenseReportLoading; | Loading: typeof CostAndExpenseReportLoading; | ||||
| } | } | ||||
| const CostAndExpenseReportWrapper: React.FC & SubComponents = async () => { | const CostAndExpenseReportWrapper: React.FC & SubComponents = async () => { | ||||
| const customers = await fetchAllCustomers() | |||||
| const teams = await fetchTeam () | |||||
| const session: any = await getServerSession(authOptions) | |||||
| const teamId = session.staff?.team.id | |||||
| const role = session!.role | |||||
| let customers = await fetchAllCustomers() | |||||
| let teams = await fetchTeam() | |||||
| let needAll = true | |||||
| if (role === TEAM_LEAD) { | |||||
| needAll = false | |||||
| teams = teams.filter((team) => team.id === teamId); | |||||
| } | |||||
| return <CostAndExpenseReport team={teams} customer={customers}/> | |||||
| return <CostAndExpenseReport team={teams} customer={customers} needAll={needAll} /> | |||||
| }; | }; | ||||
| CostAndExpenseReportWrapper.Loading = CostAndExpenseReportLoading; | CostAndExpenseReportWrapper.Loading = CostAndExpenseReportLoading; | ||||
| @@ -3,6 +3,8 @@ import CredentialsProvider from "next-auth/providers/credentials"; | |||||
| import { LOGIN_API_PATH } from "./api"; | import { LOGIN_API_PATH } from "./api"; | ||||
| export interface SessionWithTokens extends Session { | export interface SessionWithTokens extends Session { | ||||
| staff?: any; | |||||
| role?: String; | |||||
| abilities?: any[]; | abilities?: any[]; | ||||
| accessToken?: string; | accessToken?: string; | ||||
| refreshToken?: string; | refreshToken?: string; | ||||
| @@ -52,12 +54,14 @@ export const authOptions: AuthOptions = { | |||||
| session({ session, token }) { | session({ session, token }) { | ||||
| const sessionWithToken: SessionWithTokens = { | const sessionWithToken: SessionWithTokens = { | ||||
| ...session, | ...session, | ||||
| role: token.role as String, | |||||
| // Add the data from the token to the session | // Add the data from the token to the session | ||||
| abilities: (token.abilities as ability[]).map( | abilities: (token.abilities as ability[]).map( | ||||
| (item: ability) => item.actionSubjectCombo, | (item: ability) => item.actionSubjectCombo, | ||||
| ) as string[], | ) as string[], | ||||
| accessToken: token.accessToken as string | undefined, | accessToken: token.accessToken as string | undefined, | ||||
| refreshToken: token.refreshToken as string | undefined, | refreshToken: token.refreshToken as string | undefined, | ||||
| staff: token.staff as any | |||||
| }; | }; | ||||
| // console.log(sessionWithToken) | // console.log(sessionWithToken) | ||||
| return sessionWithToken; | return sessionWithToken; | ||||
| @@ -3,6 +3,21 @@ import { ability, authOptions } from "@/config/authConfig"; | |||||
| import { NextFetchEvent, NextResponse } from "next/server"; | import { NextFetchEvent, NextResponse } from "next/server"; | ||||
| import { getToken } from "next-auth/jwt"; | import { getToken } from "next-auth/jwt"; | ||||
| // user groups | |||||
| export const [ | |||||
| SUPER_ADMIN, | |||||
| TOP_MANAGEMENT, | |||||
| TEAM_LEAD, | |||||
| NORMAL_STAFF, | |||||
| SUPPORTING_STAFF | |||||
| ] = [ | |||||
| "Super Admin", | |||||
| "Top Management", | |||||
| "Team Leader", | |||||
| "Normal Staff", | |||||
| "Supporting Staff" | |||||
| ] | |||||
| // abilities | // abilities | ||||
| export const [ | export const [ | ||||
| VIEW_USER, | VIEW_USER, | ||||
| @@ -61,7 +76,7 @@ export default async function middleware( | |||||
| event: NextFetchEvent, | event: NextFetchEvent, | ||||
| ) { | ) { | ||||
| const langPref = req.nextUrl.searchParams.get(LANG_QUERY_PARAM); | const langPref = req.nextUrl.searchParams.get(LANG_QUERY_PARAM); | ||||
| const token = await getToken({ req: req, secret: process.env.SECRET }); | |||||
| // const token = await getToken({ req: req, secret: process.env.SECRET }); | |||||
| if (langPref) { | if (langPref) { | ||||
| // Redirect to same url without the lang query param + set cookies | // Redirect to same url without the lang query param + set cookies | ||||
| const newUrl = new URL(req.nextUrl); | const newUrl = new URL(req.nextUrl); | ||||