From 9f11f40e57df43d1b8bb5af95c9934d3cc9fa5f1 Mon Sep 17 00:00:00 2001 From: ShivaniKhaire Date: Wed, 2 Dec 2026 16:04:40 +0530 Subject: [PATCH] fix(admin): exclude admin accounts from all customer count metrics --- src/App.tsx | 25 ++++++----- src/components/database/ReportsExportTab.tsx | 2 +- src/components/sync/SyncOverview.tsx | 2 +- src/utils/roles.ts | 45 ++++++++++++++++++++ 4 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 src/utils/roles.ts diff --git a/src/App.tsx b/src/App.tsx index 3c6d42f..96a56b0 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,9 +1,10 @@ -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect, useMemo } from 'react'; import axios from 'axios'; import { Database, RefreshCw, RefreshCcw, Menu, Sparkles } from 'lucide-react'; import { API_BASE } from './services/api'; import type { AdminTabType } from './types'; import { normalizeCategory } from './utils/category'; +import { isAdminAccount, partitionUserLists } from './utils/roles'; // Modular Components import { AdminSidebar } from './components/layout/AdminSidebar'; @@ -56,9 +57,7 @@ export function extractErrorMessage(err: any, fallback: string = 'An error occur export default function App() { const [userToken, setUserToken] = useState(localStorage.getItem('access_token')); const [loggedInUser, setLoggedInUser] = useState(null); - const isAdmin = Boolean( - loggedInUser?.roles?.some((role: string) => role === 'super_admin' || role === 'finance_admin' || role === 'admin') - ); + const isAdmin = isAdminAccount(loggedInUser); // Admin Login State const [adminAuthLoading, setAdminAuthLoading] = useState(false); @@ -184,6 +183,10 @@ export default function App() { // Admins & Practitioners const [adminsList, setAdminsList] = useState([]); const [practitionersList, setPractitionersList] = useState([]); + const { customers: customersList, adminAccounts: resolvedAdminsList } = useMemo( + () => partitionUserLists(practitionersList, adminsList), + [practitionersList, adminsList] + ); const [createAdminEmail, setCreateAdminEmail] = useState(''); const [createAdminPassword, setCreateAdminPassword] = useState(''); const [createAdminName, setCreateAdminName] = useState(''); @@ -1056,10 +1059,10 @@ export default function App() { modules: adminModulesList.length, plans: adminPlansList.length, coupons: adminCouponsList.length, - customers: practitionersList.length, + customers: customersList.length, subscriptions: usersSubscriptions.length, payments: rzpPayments.length, - admins: adminsList.length, + admins: resolvedAdminsList.length, }} isCollapsed={isSidebarCollapsed} onToggleCollapse={() => { @@ -1142,9 +1145,9 @@ export default function App() { {adminSubTab === 'overview' && ( = ({ - {totalUsers} Total Users + {totalUsers} Total Customers
diff --git a/src/components/sync/SyncOverview.tsx b/src/components/sync/SyncOverview.tsx index 5436c89..c4ba376 100644 --- a/src/components/sync/SyncOverview.tsx +++ b/src/components/sync/SyncOverview.tsx @@ -24,7 +24,7 @@ export const SyncOverview: React.FC = ({

{stats.totalUsers}

-

Total Users

+

Total Customers

{stats.subscriptions}

diff --git a/src/utils/roles.ts b/src/utils/roles.ts new file mode 100644 index 0000000..d60d81a --- /dev/null +++ b/src/utils/roles.ts @@ -0,0 +1,45 @@ +export const ADMIN_ROLES = new Set(['super_admin', 'admin', 'finance_admin']); + +/** + * Returns true when a user object carries at least one administrative role. + */ +export function isAdminAccount(user?: { roles?: string[] } | null): boolean { + if (!Array.isArray(user?.roles)) return false; + return user.roles.some((role) => ADMIN_ROLES.has(String(role).trim().toLowerCase())); +} + +/** + * Separates customer accounts from admin accounts so admins never leak into customer counts. + */ +export function partitionUserLists( + practitioners: T[], + admins: T[] +): { customers: T[]; adminAccounts: T[] } { + const customers: T[] = []; + const adminAccounts: T[] = []; + const seenAdmins = new Set(); + + for (const admin of admins) { + const key = String(admin.id || admin.email || '').toLowerCase(); + if (key) { + if (seenAdmins.has(key)) continue; + seenAdmins.add(key); + } + adminAccounts.push(admin); + } + + for (const user of practitioners) { + if (isAdminAccount(user)) { + const key = String(user.id || user.email || '').toLowerCase(); + if (!key || !seenAdmins.has(key)) { + if (key) seenAdmins.add(key); + adminAccounts.push(user); + } + } else { + customers.push(user); + } + } + + return { customers, adminAccounts }; +} + -- 2.34.1