fix(admin): exclude admin accounts from all customer count metrics #72

Merged
amit merged 1 commits from fix/issue-2-exclude-admins-from-customers into main 2026-12-03 05:03:42 +00:00
4 changed files with 61 additions and 13 deletions

View File

@ -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<string | null>(localStorage.getItem('access_token'));
const [loggedInUser, setLoggedInUser] = useState<any>(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<any[]>([]);
const [practitionersList, setPractitionersList] = useState<any[]>([]);
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' && (
<SyncOverview
stats={{
totalUsers: practitionersList.length + adminsList.length,
totalUsers: customersList.length,
subscriptions: usersSubscriptions.length,
superAdmins: adminsList.length,
superAdmins: resolvedAdminsList.length,
payments: rzpPayments.length,
}}
adminLogs={adminLogs}
@ -1211,7 +1214,7 @@ export default function App() {
{adminSubTab === 'customers' && (
<CustomersTab
customers={practitionersList}
customers={customersList}
currentPage={customersPage}
perPage={customersPerPage}
onPageChange={setCustomersPage}
@ -1250,7 +1253,7 @@ export default function App() {
{adminSubTab === 'admins' && (
<AdminsTab
admins={adminsList}
admins={resolvedAdminsList}
name={createAdminName}
email={createAdminEmail}
password={createAdminPassword}
@ -1282,7 +1285,7 @@ export default function App() {
downloadingReport={reportDownloading}
successMessage={reportExportSuccess}
errorMessage={reportExportError}
totalUsers={practitionersList.length + adminsList.length}
totalUsers={customersList.length}
totalPayments={rzpPayments.length}
totalSubscriptions={usersSubscriptions.length}
totalCoupons={adminCouponsList.length}

View File

@ -124,7 +124,7 @@ export const ReportsExportTab: React.FC<ReportsExportTabProps> = ({
<Users className="h-5 w-5" />
</div>
<span className="text-[10px] font-bold px-2.5 py-1 rounded-full bg-indigo-50 text-indigo-700 border border-indigo-100">
{totalUsers} Total Users
{totalUsers} Total Customers
</span>
</div>
<div>

View File

@ -24,7 +24,7 @@ export const SyncOverview: React.FC<SyncOverviewProps> = ({
<div className="grid grid-cols-2 md:grid-cols-4 gap-4">
<div className="bg-gradient-to-br from-blue-50 to-indigo-50 border border-blue-200/60 rounded-2xl p-4 text-center">
<p className="text-2xl font-black text-indigo-600">{stats.totalUsers}</p>
<p className="text-[10px] font-bold text-slate-500 uppercase tracking-wider mt-1">Total Users</p>
<p className="text-[10px] font-bold text-slate-500 uppercase tracking-wider mt-1">Total Customers</p>
</div>
<div className="bg-gradient-to-br from-purple-50 to-pink-50 border border-purple-200/60 rounded-2xl p-4 text-center">
<p className="text-2xl font-black text-purple-600">{stats.subscriptions}</p>

45
src/utils/roles.ts Normal file
View File

@ -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<T extends { id?: string; email?: string; roles?: string[] }>(
practitioners: T[],
admins: T[]
): { customers: T[]; adminAccounts: T[] } {
const customers: T[] = [];
const adminAccounts: T[] = [];
const seenAdmins = new Set<string>();
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 };
}