From 0dcb947f725a0165ae78fd47bf1cfa7027bdf881 Mon Sep 17 00:00:00 2001 From: DarrellRoberts Date: Wed, 17 Jun 2026 10:57:55 +0200 Subject: [PATCH 1/2] adds getagent person id for matching personids --- src/app/[lang]/dashboard/agents/[id]/page.tsx | 15 ++++++++--- .../Agents/AgentReadOnlyTableRow.tsx | 11 +++++++- src/hooks/api/getAgent.ts | 27 +++++++++++++++++++ src/hooks/api/{getUserRole.ts => getUser.ts} | 8 +++--- 4 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 src/hooks/api/getAgent.ts rename src/hooks/api/{getUserRole.ts => getUser.ts} (68%) diff --git a/src/app/[lang]/dashboard/agents/[id]/page.tsx b/src/app/[lang]/dashboard/agents/[id]/page.tsx index ee1a7cf0..6e09830d 100644 --- a/src/app/[lang]/dashboard/agents/[id]/page.tsx +++ b/src/app/[lang]/dashboard/agents/[id]/page.tsx @@ -1,5 +1,6 @@ import ProfileLayout from "@/components/Dashboard/Profile/ProfileLayout"; -import { getServerUserRole } from "@/hooks/api/getUserRole"; +import { getServerAgent } from "@/hooks/api/getAgent"; +import { getServerUser } from "@/hooks/api/getUser"; import { RouteParams } from "@/types"; import { UserRole } from "need4deed-sdk"; import { cookies } from "next/headers"; @@ -10,9 +11,17 @@ export default async function DashboardAgentPage({ params }: RouteParams) { const cookieStore = await cookies(); const cookieHeader = cookieStore.toString(); - const userRole = await getServerUserRole(cookieHeader); + const user = await getServerUser(cookieHeader); + const agent = await getServerAgent(cookieHeader, id); + const userRole = user?.role; + const personId = user?.personId; + const matchedPersonId = personId === agent?.representative?.id; - if (!userRole || (userRole !== UserRole.COORDINATOR && userRole !== UserRole.ADMIN)) { + if ( + !userRole || + !personId || + (userRole !== UserRole.COORDINATOR && userRole !== UserRole.ADMIN && !matchedPersonId) + ) { redirect(`/dashboard/agents`); } return ; diff --git a/src/components/Dashboard/Agents/AgentReadOnlyTableRow.tsx b/src/components/Dashboard/Agents/AgentReadOnlyTableRow.tsx index 9f3b7113..b873f77f 100644 --- a/src/components/Dashboard/Agents/AgentReadOnlyTableRow.tsx +++ b/src/components/Dashboard/Agents/AgentReadOnlyTableRow.tsx @@ -1,5 +1,7 @@ import { ApiAgentGetList, OptionItem } from "need4deed-sdk"; import { ClickableRow, TableCell } from "@/components/core/common/Table"; +import { useTranslation } from "react-i18next"; +import { useRouter } from "next/navigation"; interface Props { agent: ApiAgentGetList; @@ -10,10 +12,17 @@ interface Props { } export function AgentReadOnlyTableRow({ agent, isLast, districtsList }: Props) { + const { i18n } = useTranslation(); + const router = useRouter(); const { id, title, type, district } = agent; const districtTitle = district?.id ? (districtsList?.find((d) => d.id === district.id)?.title ?? null) : null; + + const handleGoToProfile = () => { + if (!id) return; + router.push(`/${i18n.language}/dashboard/agents/${id}`); + }; return ( - + {title} {type} {districtTitle || "—"} diff --git a/src/hooks/api/getAgent.ts b/src/hooks/api/getAgent.ts new file mode 100644 index 00000000..15c98fa5 --- /dev/null +++ b/src/hooks/api/getAgent.ts @@ -0,0 +1,27 @@ +import { fetchFn } from "@/hooks/api/utils"; +import { ApiAgentGet, ApiUserGet, UserRole } from "need4deed-sdk"; +import { apiPathAgent, apiPathMe } from "@/config/constants"; + +export interface ApiResponse { + message: string; + data: T; + count: number; +} + +export const getServerAgent = async (cookieHeader: string, id: string): Promise => { + try { + const urlPath = apiPathAgent.replace("/api/", ""); + const response = await fetchFn>({ + url: `${process.env.URL_API}/${urlPath}/${id}`, + options: { + method: "GET", + headers: { Cookie: cookieHeader }, + cache: "no-store", + }, + }); + return response.data; + } catch (error) { + console.error("Failed to fetch server user role:", error); + return null; + } +}; diff --git a/src/hooks/api/getUserRole.ts b/src/hooks/api/getUser.ts similarity index 68% rename from src/hooks/api/getUserRole.ts rename to src/hooks/api/getUser.ts index 512d8b04..795489ac 100644 --- a/src/hooks/api/getUserRole.ts +++ b/src/hooks/api/getUser.ts @@ -1,5 +1,5 @@ import { fetchFn } from "@/hooks/api/utils"; -import { UserRole } from "need4deed-sdk"; +import { ApiUserGet, UserRole } from "need4deed-sdk"; import { apiPathMe } from "@/config/constants"; export interface ApiResponse { @@ -8,10 +8,10 @@ export interface ApiResponse { count: number; } -export const getServerUserRole = async (cookieHeader: string): Promise => { +export const getServerUser = async (cookieHeader: string): Promise => { try { const urlPath = apiPathMe.replace("/api/", ""); - const response = await fetchFn>({ + const response = await fetchFn>({ url: `${process.env.URL_API}/${urlPath}`, options: { method: "GET", @@ -19,7 +19,7 @@ export const getServerUserRole = async (cookieHeader: string): Promise Date: Wed, 17 Jun 2026 11:07:32 +0200 Subject: [PATCH 2/2] adds getagent person id for matching personids --- src/hooks/api/getAgent.ts | 4 ++-- src/hooks/api/getUser.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/hooks/api/getAgent.ts b/src/hooks/api/getAgent.ts index 15c98fa5..f32cfca5 100644 --- a/src/hooks/api/getAgent.ts +++ b/src/hooks/api/getAgent.ts @@ -1,6 +1,6 @@ import { fetchFn } from "@/hooks/api/utils"; -import { ApiAgentGet, ApiUserGet, UserRole } from "need4deed-sdk"; -import { apiPathAgent, apiPathMe } from "@/config/constants"; +import { ApiAgentGet } from "need4deed-sdk"; +import { apiPathAgent } from "@/config/constants"; export interface ApiResponse { message: string; diff --git a/src/hooks/api/getUser.ts b/src/hooks/api/getUser.ts index 795489ac..f45db6e4 100644 --- a/src/hooks/api/getUser.ts +++ b/src/hooks/api/getUser.ts @@ -1,5 +1,5 @@ import { fetchFn } from "@/hooks/api/utils"; -import { ApiUserGet, UserRole } from "need4deed-sdk"; +import { ApiUserGet } from "need4deed-sdk"; import { apiPathMe } from "@/config/constants"; export interface ApiResponse {