diff --git a/app/panel/(layout)/dashboard/page.js b/app/panel/(layout)/dashboard/page.js index dafec69..48322bc 100644 --- a/app/panel/(layout)/dashboard/page.js +++ b/app/panel/(layout)/dashboard/page.js @@ -1,8 +1,40 @@ import DashboardPage from "@/components/panel/dashboard"; -import Information from "@/data/information.json"; +import { cookies } from "next/headers"; +import { fetchReq } from "@/lib/req"; +import { safeJsonParse } from "@/lib/sanitize"; +import { adaptRepresentationDashboard } from "@/helper"; -function Dashboard() { - return ; +export const dynamic = "force-dynamic"; + +async function Dashboard() { + const API_URL = process.env.NEXT_PUBLIC_API_URL; + let dashboard = null; + + try { + const cookieStore = await cookies(); + const userInfo = cookieStore.get("userInfo"); + const accessToken = cookieStore.get("access_token"); + + if (userInfo && accessToken) { + const parsedUserInfo = safeJsonParse(userInfo.value); + const representationUuid = parsedUserInfo?.representation_uuid; + + if (representationUuid) { + const now = new Date(); + const year = now.getFullYear(); + const month = now.getMonth() + 1; + + dashboard = await fetchReq( + `${API_URL}/api/v1/representation/${representationUuid}/dashboard/monthly?year=${year}&month=${month}`, + { headers: { Authorization: `Bearer ${accessToken.value}` } } + ); + } + } + } catch (error) { + console.error("Error fetching representation dashboard:", error); + } + + return ; } export default Dashboard; diff --git a/helper/index.js b/helper/index.js index c87a3c7..9a6ec32 100644 --- a/helper/index.js +++ b/helper/index.js @@ -696,3 +696,18 @@ export function adaptRepresentationInfo(representationInfo) { chart_amount_income: [], }; } + +export function adaptRepresentationDashboard(dashboard) { + const stats = dashboard?.data?.stats ?? dashboard?.stats ?? {}; + const daily = Array.isArray(stats.daily) ? stats.daily : []; + + return { + number_of_patients: stats.total_appointments ?? 0, + today_turns: daily.length ? daily[daily.length - 1].appointments ?? 0 : 0, + payments: stats.total_revenue_rials ?? 0, + commission_rials: stats.commission_rials ?? 0, + chart_amount_income: daily.map((d) => d.revenue ?? 0), + list_of_doctors_appointments: [], + todays_activities: [], + }; +}