Compare commits

..

3 Commits

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 axios from 'axios';
import { Database, RefreshCw, RefreshCcw, Menu, Sparkles } from 'lucide-react'; import { Database, RefreshCw, RefreshCcw, Menu, Sparkles } from 'lucide-react';
import { API_BASE } from './services/api'; import { API_BASE } from './services/api';
import type { AdminTabType } from './types'; import type { AdminTabType } from './types';
import { normalizeCategory } from './utils/category'; import { normalizeCategory } from './utils/category';
import { isAdminAccount, partitionUserLists } from './utils/roles';
// Modular Components // Modular Components
import { AdminSidebar } from './components/layout/AdminSidebar'; import { AdminSidebar } from './components/layout/AdminSidebar';
@ -56,9 +57,7 @@ export function extractErrorMessage(err: any, fallback: string = 'An error occur
export default function App() { export default function App() {
const [userToken, setUserToken] = useState<string | null>(localStorage.getItem('access_token')); const [userToken, setUserToken] = useState<string | null>(localStorage.getItem('access_token'));
const [loggedInUser, setLoggedInUser] = useState<any>(null); const [loggedInUser, setLoggedInUser] = useState<any>(null);
const isAdmin = Boolean( const isAdmin = isAdminAccount(loggedInUser);
loggedInUser?.roles?.some((role: string) => role === 'super_admin' || role === 'finance_admin' || role === 'admin')
);
// Admin Login State // Admin Login State
const [adminAuthLoading, setAdminAuthLoading] = useState(false); const [adminAuthLoading, setAdminAuthLoading] = useState(false);
@ -187,6 +186,10 @@ export default function App() {
// Admins & Practitioners // Admins & Practitioners
const [adminsList, setAdminsList] = useState<any[]>([]); const [adminsList, setAdminsList] = useState<any[]>([]);
const [practitionersList, setPractitionersList] = useState<any[]>([]); const [practitionersList, setPractitionersList] = useState<any[]>([]);
const { customers: customersList, adminAccounts: resolvedAdminsList } = useMemo(
() => partitionUserLists(practitionersList, adminsList),
[practitionersList, adminsList]
);
const [createAdminEmail, setCreateAdminEmail] = useState(''); const [createAdminEmail, setCreateAdminEmail] = useState('');
const [createAdminPassword, setCreateAdminPassword] = useState(''); const [createAdminPassword, setCreateAdminPassword] = useState('');
const [createAdminName, setCreateAdminName] = useState(''); const [createAdminName, setCreateAdminName] = useState('');
@ -1091,10 +1094,10 @@ export default function App() {
modules: adminModulesList.length, modules: adminModulesList.length,
plans: adminPlansList.length, plans: adminPlansList.length,
coupons: adminCouponsList.length, coupons: adminCouponsList.length,
customers: practitionersList.length, customers: customersList.length,
subscriptions: usersSubscriptions.length, subscriptions: usersSubscriptions.length,
payments: rzpPayments.length, payments: rzpPayments.length,
admins: adminsList.length, admins: resolvedAdminsList.length,
}} }}
isCollapsed={isSidebarCollapsed} isCollapsed={isSidebarCollapsed}
onToggleCollapse={() => { onToggleCollapse={() => {
@ -1177,9 +1180,9 @@ export default function App() {
{adminSubTab === 'overview' && ( {adminSubTab === 'overview' && (
<SyncOverview <SyncOverview
stats={{ stats={{
totalUsers: practitionersList.length + adminsList.length, totalUsers: customersList.length,
subscriptions: usersSubscriptions.length, subscriptions: usersSubscriptions.length,
superAdmins: adminsList.length, superAdmins: resolvedAdminsList.length,
payments: rzpPayments.length, payments: rzpPayments.length,
}} }}
adminLogs={adminLogs} adminLogs={adminLogs}
@ -1246,7 +1249,7 @@ export default function App() {
{adminSubTab === 'customers' && ( {adminSubTab === 'customers' && (
<CustomersTab <CustomersTab
customers={practitionersList} customers={customersList}
currentPage={customersPage} currentPage={customersPage}
perPage={customersPerPage} perPage={customersPerPage}
onPageChange={setCustomersPage} onPageChange={setCustomersPage}
@ -1285,7 +1288,7 @@ export default function App() {
{adminSubTab === 'admins' && ( {adminSubTab === 'admins' && (
<AdminsTab <AdminsTab
admins={adminsList} admins={resolvedAdminsList}
name={createAdminName} name={createAdminName}
email={createAdminEmail} email={createAdminEmail}
password={createAdminPassword} password={createAdminPassword}
@ -1317,7 +1320,7 @@ export default function App() {
downloadingReport={reportDownloading} downloadingReport={reportDownloading}
successMessage={reportExportSuccess} successMessage={reportExportSuccess}
errorMessage={reportExportError} errorMessage={reportExportError}
totalUsers={practitionersList.length + adminsList.length} totalUsers={customersList.length}
totalPayments={rzpPayments.length} totalPayments={rzpPayments.length}
totalSubscriptions={usersSubscriptions.length} totalSubscriptions={usersSubscriptions.length}
totalCoupons={adminCouponsList.length} totalCoupons={adminCouponsList.length}

View File

@ -124,7 +124,7 @@ export const ReportsExportTab: React.FC<ReportsExportTabProps> = ({
<Users className="h-5 w-5" /> <Users className="h-5 w-5" />
</div> </div>
<span className="text-[10px] font-bold px-2.5 py-1 rounded-full bg-indigo-50 text-indigo-700 border border-indigo-100"> <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> </span>
</div> </div>
<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="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"> <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-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>
<div className="bg-gradient-to-br from-purple-50 to-pink-50 border border-purple-200/60 rounded-2xl p-4 text-center"> <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> <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 };
}