Compare commits
13 Commits
fix/lock-m
...
main
| Author | SHA1 | Date |
|---|---|---|
|
|
6c59b0ce3a | |
|
|
0673006d12 | |
|
|
f0bc8d4e66 | |
|
|
9f11f40e57 | |
|
|
167b22d3ec | |
|
|
1fa7b96195 | |
|
|
7709d6eba8 | |
|
|
1ca8551855 | |
|
|
02ecd42039 | |
|
|
5ff3215aa5 | |
|
|
e05e1e4f0f | |
|
|
98ecef7896 | |
|
|
8a1de005f2 |
94
src/App.tsx
94
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<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);
|
||||
|
|
@ -106,6 +105,8 @@ export default function App() {
|
|||
const [editModuleName, setEditModuleName] = useState<string>('');
|
||||
const [editModuleDesc, setEditModuleDesc] = useState<string>('');
|
||||
const [editModuleCategory, setEditModuleCategory] = useState<string>('point');
|
||||
const [editModuleIcon, setEditModuleIcon] = useState<string>('folder-outline');
|
||||
const [editModuleColor, setEditModuleColor] = useState<string>('#4F46E5');
|
||||
const [editModuleSaving, setEditModuleSaving] = useState<boolean>(false);
|
||||
|
||||
// Plans State
|
||||
|
|
@ -121,6 +122,7 @@ export default function App() {
|
|||
const [editPlanBadge, setEditPlanBadge] = useState<string>('');
|
||||
const [editPlanHeading, setEditPlanHeading] = useState<string>('');
|
||||
const [editPlanSubtitle, setEditPlanSubtitle] = useState<string>('');
|
||||
const [editPlanModuleIds, setEditPlanModuleIds] = useState<string[]>([]);
|
||||
const [editPriceSaving, setEditPriceSaving] = useState<boolean>(false);
|
||||
|
||||
// Create Custom Plan State
|
||||
|
|
@ -142,7 +144,7 @@ export default function App() {
|
|||
|
||||
// Coupons State
|
||||
const [adminCouponsList, setAdminCouponsList] = useState<any[]>([]);
|
||||
const [couponFilterStatus, setCouponFilterStatus] = useState<'all' | 'active' | 'scheduled' | 'expired'>('all');
|
||||
const [couponFilterStatus, setCouponFilterStatus] = useState<'all' | 'active' | 'scheduled' | 'fully_redeemed' | 'expired'>('all');
|
||||
const [couponSearchQuery, setCouponSearchQuery] = useState<string>('');
|
||||
|
||||
// Unified Coupon Modal State
|
||||
|
|
@ -168,6 +170,7 @@ export default function App() {
|
|||
minSpendUsd: '',
|
||||
minSpendEur: '',
|
||||
usageLimit: '0',
|
||||
perUserLimit: '',
|
||||
firstOnly: false,
|
||||
});
|
||||
const [couponSaving, setCouponSaving] = useState<boolean>(false);
|
||||
|
|
@ -183,6 +186,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('');
|
||||
|
|
@ -541,6 +548,8 @@ export default function App() {
|
|||
setEditModuleName(mod.name || '');
|
||||
setEditModuleDesc(mod.description || '');
|
||||
setEditModuleCategory(normalizeCategory(mod.category));
|
||||
setEditModuleIcon(mod.icon || 'folder-outline');
|
||||
setEditModuleColor(mod.color || '#4F46E5');
|
||||
};
|
||||
|
||||
const handleSaveModuleDetails = async (e: React.FormEvent) => {
|
||||
|
|
@ -548,6 +557,8 @@ export default function App() {
|
|||
if (!selectedEditModule || !userToken) return;
|
||||
setEditModuleSaving(true);
|
||||
const normalizedCat = normalizeCategory(editModuleCategory);
|
||||
const nextIcon = editModuleIcon.trim() || 'folder-outline';
|
||||
const nextColor = editModuleColor.trim() || '#4F46E5';
|
||||
try {
|
||||
await axios.put(
|
||||
`${API_BASE}/api/admin/modules/${selectedEditModule.id}`,
|
||||
|
|
@ -555,13 +566,22 @@ export default function App() {
|
|||
name: editModuleName,
|
||||
description: editModuleDesc,
|
||||
category: normalizedCat,
|
||||
icon: nextIcon,
|
||||
color: nextColor,
|
||||
},
|
||||
{ headers: { Authorization: `Bearer ${userToken}` } }
|
||||
);
|
||||
setAdminModulesList((prev) =>
|
||||
prev.map((m) =>
|
||||
m.id === selectedEditModule.id
|
||||
? { ...m, name: editModuleName, description: editModuleDesc, category: normalizedCat }
|
||||
? {
|
||||
...m,
|
||||
name: editModuleName,
|
||||
description: editModuleDesc,
|
||||
category: normalizedCat,
|
||||
icon: nextIcon,
|
||||
color: nextColor,
|
||||
}
|
||||
: m
|
||||
)
|
||||
);
|
||||
|
|
@ -609,6 +629,11 @@ export default function App() {
|
|||
setEditPlanBadge(plan.badge_text || '');
|
||||
setEditPlanHeading(plan.heading || '');
|
||||
setEditPlanSubtitle(plan.subtitle || '');
|
||||
setEditPlanModuleIds(
|
||||
Array.isArray(plan.module_ids) && plan.module_ids.length > 0
|
||||
? plan.module_ids
|
||||
: (plan.modules || []).map((m: any) => m.id).filter(Boolean)
|
||||
);
|
||||
};
|
||||
|
||||
const handleSavePlanPrice = async (e: React.FormEvent) => {
|
||||
|
|
@ -623,6 +648,10 @@ export default function App() {
|
|||
}
|
||||
setEditPriceSaving(true);
|
||||
try {
|
||||
const isEditableBundle =
|
||||
selectedEditPricePlan.plan_type === 'bundle' ||
|
||||
selectedEditPricePlan.base_code === 'PATTERN_CORE' ||
|
||||
selectedEditPricePlan.base_code === 'NUTRITION_CORE';
|
||||
await axios.put(
|
||||
`${API_BASE}/api/admin/plans/${selectedEditPricePlan.base_code}/pricing`,
|
||||
{
|
||||
|
|
@ -634,9 +663,14 @@ export default function App() {
|
|||
badge_text: editPlanBadge.trim() || undefined,
|
||||
heading: editPlanHeading.trim() || undefined,
|
||||
subtitle: editPlanSubtitle.trim() || undefined,
|
||||
module_ids: isEditableBundle ? editPlanModuleIds : undefined,
|
||||
},
|
||||
{ headers: { Authorization: `Bearer ${userToken}` } }
|
||||
);
|
||||
const updatedBundleModules =
|
||||
isEditableBundle
|
||||
? adminModulesList.filter((m) => editPlanModuleIds.includes(m.id))
|
||||
: undefined;
|
||||
setAdminPlansList((prev) =>
|
||||
prev.map((p) =>
|
||||
p.base_code === selectedEditPricePlan.base_code
|
||||
|
|
@ -650,6 +684,11 @@ export default function App() {
|
|||
price_inr: inrVal,
|
||||
price_usd: usdVal,
|
||||
price_eur: eurVal,
|
||||
pricing_status:
|
||||
inrVal > 0 || usdVal > 0 || eurVal > 0 ? 'configured' : p.pricing_status,
|
||||
...(isEditableBundle
|
||||
? { module_ids: editPlanModuleIds, modules: updatedBundleModules }
|
||||
: {}),
|
||||
}
|
||||
: p
|
||||
)
|
||||
|
|
@ -766,6 +805,7 @@ export default function App() {
|
|||
minSpendUsd: '',
|
||||
minSpendEur: '',
|
||||
usageLimit: '0',
|
||||
perUserLimit: '',
|
||||
firstOnly: false,
|
||||
});
|
||||
setCouponModalMode('create');
|
||||
|
|
@ -797,6 +837,7 @@ export default function App() {
|
|||
minSpendUsd: c.min_purchase_usd !== undefined && c.min_purchase_usd !== null ? String(c.min_purchase_usd) : '',
|
||||
minSpendEur: c.min_purchase_eur !== undefined && c.min_purchase_eur !== null ? String(c.min_purchase_eur) : '',
|
||||
usageLimit: String(c.usage_limit ?? 0),
|
||||
perUserLimit: c.per_user_limit !== undefined && c.per_user_limit !== null ? String(c.per_user_limit) : '',
|
||||
firstOnly: Boolean(c.is_first_purchase_only),
|
||||
});
|
||||
setCouponModalMode('edit');
|
||||
|
|
@ -826,6 +867,19 @@ export default function App() {
|
|||
const minUsd = couponFormData.minSpendUsd.trim() ? parseFloat(couponFormData.minSpendUsd) : undefined;
|
||||
const minEur = couponFormData.minSpendEur.trim() ? parseFloat(couponFormData.minSpendEur) : undefined;
|
||||
|
||||
const trimmedPerUser = couponFormData.perUserLimit.trim();
|
||||
if (trimmedPerUser === '') {
|
||||
alert('Per-user limit is required. Please enter 0 or greater.');
|
||||
setCouponSaving(false);
|
||||
return;
|
||||
}
|
||||
const parsedPerUser = Number(trimmedPerUser);
|
||||
if (!Number.isInteger(parsedPerUser) || parsedPerUser < 0) {
|
||||
alert('Per-user limit must be a whole number (0 or greater).');
|
||||
setCouponSaving(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const payload: any = {
|
||||
code: couponFormData.code.trim().toUpperCase(),
|
||||
discount_type: couponFormData.discountType,
|
||||
|
|
@ -839,6 +893,7 @@ export default function App() {
|
|||
max_discount_usd: capUsd,
|
||||
max_discount_eur: capEur,
|
||||
usage_limit: parseInt(couponFormData.usageLimit) || 0,
|
||||
per_user_limit: parsedPerUser,
|
||||
min_purchase_amount: minInr,
|
||||
min_purchase_inr: minInr,
|
||||
min_purchase_usd: minUsd,
|
||||
|
|
@ -1039,10 +1094,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={() => {
|
||||
|
|
@ -1125,9 +1180,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}
|
||||
|
|
@ -1194,7 +1249,7 @@ export default function App() {
|
|||
|
||||
{adminSubTab === 'customers' && (
|
||||
<CustomersTab
|
||||
customers={practitionersList}
|
||||
customers={customersList}
|
||||
currentPage={customersPage}
|
||||
perPage={customersPerPage}
|
||||
onPageChange={setCustomersPage}
|
||||
|
|
@ -1233,7 +1288,7 @@ export default function App() {
|
|||
|
||||
{adminSubTab === 'admins' && (
|
||||
<AdminsTab
|
||||
admins={adminsList}
|
||||
admins={resolvedAdminsList}
|
||||
name={createAdminName}
|
||||
email={createAdminEmail}
|
||||
password={createAdminPassword}
|
||||
|
|
@ -1265,7 +1320,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}
|
||||
|
|
@ -1285,9 +1340,13 @@ export default function App() {
|
|||
name={editModuleName}
|
||||
description={editModuleDesc}
|
||||
category={editModuleCategory}
|
||||
icon={editModuleIcon}
|
||||
color={editModuleColor}
|
||||
saving={editModuleSaving}
|
||||
onNameChange={setEditModuleName}
|
||||
onDescriptionChange={setEditModuleDesc}
|
||||
onIconChange={setEditModuleIcon}
|
||||
onColorChange={setEditModuleColor}
|
||||
onSave={handleSaveModuleDetails}
|
||||
onClose={() => setSelectedEditModule(null)}
|
||||
/>
|
||||
|
|
@ -1346,6 +1405,8 @@ export default function App() {
|
|||
priceInr={editPriceInr}
|
||||
priceUsd={editPriceUsd}
|
||||
priceEur={editPriceEur}
|
||||
allModules={adminModulesList}
|
||||
moduleIds={editPlanModuleIds}
|
||||
saving={editPriceSaving}
|
||||
onNameChange={setEditPlanName}
|
||||
onDescChange={setEditPlanDesc}
|
||||
|
|
@ -1355,6 +1416,13 @@ export default function App() {
|
|||
onPriceInrChange={setEditPriceInr}
|
||||
onPriceUsdChange={setEditPriceUsd}
|
||||
onPriceEurChange={setEditPriceEur}
|
||||
onToggleModule={(id: string) =>
|
||||
setEditPlanModuleIds((prev) =>
|
||||
prev.includes(id) ? prev.filter((x) => x !== id) : [...prev, id]
|
||||
)
|
||||
}
|
||||
onSelectAllModules={() => setEditPlanModuleIds(adminModulesList.map((m) => m.id))}
|
||||
onClearAllModules={() => setEditPlanModuleIds([])}
|
||||
onSave={handleSavePlanPrice}
|
||||
onClose={() => setSelectedEditPricePlan(null)}
|
||||
/>
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ export interface CouponFormData {
|
|||
minSpendUsd: string;
|
||||
minSpendEur: string;
|
||||
usageLimit: string;
|
||||
perUserLimit: string;
|
||||
firstOnly: boolean;
|
||||
}
|
||||
|
||||
|
|
@ -138,9 +139,9 @@ export const CouponModal: React.FC<CouponModalProps> = ({
|
|||
value={data.discountValue}
|
||||
onChange={(e) => onChange('discountValue', e.target.value)}
|
||||
placeholder="e.g. 20"
|
||||
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-bold text-slate-800 focus:outline-none focus:border-indigo-500"
|
||||
className="w-full bg-white border border-slate-200 rounded-xl pl-3 pr-7 py-2 text-xs font-bold text-slate-800 focus:outline-none focus:border-indigo-500 [appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none"
|
||||
/>
|
||||
<span className="absolute right-3 top-2 text-xs font-bold text-slate-400">%</span>
|
||||
<span className="absolute right-3 top-1/2 -translate-y-1/2 text-xs font-bold text-slate-400 pointer-events-none">%</span>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
|
|
@ -421,9 +422,11 @@ export const CouponModal: React.FC<CouponModalProps> = ({
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3 pt-1">
|
||||
<div className="grid grid-cols-1 sm:grid-cols-3 gap-3 pt-1 items-start">
|
||||
<div>
|
||||
<label className="block text-[11px] font-bold text-slate-600 mb-1">Global Usage Limit (0=None)</label>
|
||||
<label className="block text-[11px] font-bold text-slate-600 mb-1">
|
||||
Total Usage Limit
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
min="0"
|
||||
|
|
@ -432,8 +435,34 @@ export const CouponModal: React.FC<CouponModalProps> = ({
|
|||
placeholder="0"
|
||||
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs text-slate-800 focus:outline-none focus:border-indigo-500"
|
||||
/>
|
||||
<p className="text-[10px] text-slate-400 font-medium mt-1">
|
||||
0 = Unlimited total uses
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex items-center pt-5">
|
||||
<div>
|
||||
<label className="block text-[11px] font-bold text-slate-600 mb-1">
|
||||
Per-User Limit *
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
required
|
||||
min="0"
|
||||
value={data.perUserLimit}
|
||||
onChange={(e) => onChange('perUserLimit', e.target.value)}
|
||||
placeholder="e.g. 1"
|
||||
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs text-slate-800 focus:outline-none focus:border-indigo-500"
|
||||
/>
|
||||
<p
|
||||
className={`text-[10px] font-medium mt-1 ${
|
||||
data.perUserLimit.trim() === '0' ? 'text-rose-600 font-semibold' : 'text-slate-400'
|
||||
}`}
|
||||
>
|
||||
{data.perUserLimit.trim() === '0'
|
||||
? '0 = Blocked for all users'
|
||||
: '0 = Blocked · 1+ = Uses per user'}
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex items-center sm:pt-7">
|
||||
<label className="flex items-center gap-2 cursor-pointer">
|
||||
<input
|
||||
type="checkbox"
|
||||
|
|
|
|||
|
|
@ -2,13 +2,41 @@ import React, { useState } from 'react';
|
|||
import { Ticket, Plus, Check, Copy, Edit, Trash2 } from 'lucide-react';
|
||||
import { Pagination } from '../common/Pagination';
|
||||
|
||||
type CouponStatusFilter = 'all' | 'active' | 'scheduled' | 'fully_redeemed' | 'expired';
|
||||
|
||||
const STATUS_LABELS: Record<Exclude<CouponStatusFilter, 'all'>, string> = {
|
||||
active: 'Active',
|
||||
scheduled: 'Scheduled',
|
||||
fully_redeemed: 'Fully Redeemed',
|
||||
expired: 'Expired',
|
||||
};
|
||||
|
||||
interface CouponStatusFields {
|
||||
usage_limit?: number;
|
||||
usage_count?: number;
|
||||
schedule_status?: string;
|
||||
is_active?: boolean;
|
||||
}
|
||||
|
||||
const getEffectiveCouponStatus = (coupon: CouponStatusFields): string => {
|
||||
const usageLimit = coupon.usage_limit || 0;
|
||||
const usageCount = coupon.usage_count || 0;
|
||||
if (coupon.schedule_status === 'fully_redeemed') {
|
||||
return 'fully_redeemed';
|
||||
}
|
||||
if (coupon.is_active && usageLimit > 0 && usageCount >= usageLimit) {
|
||||
return 'fully_redeemed';
|
||||
}
|
||||
return coupon.schedule_status ?? '';
|
||||
};
|
||||
|
||||
interface CouponsTabProps {
|
||||
coupons: any[];
|
||||
filterStatus: 'all' | 'active' | 'scheduled' | 'expired';
|
||||
filterStatus: CouponStatusFilter;
|
||||
searchQuery: string;
|
||||
currentPage: number;
|
||||
perPage: number;
|
||||
onFilterStatusChange: (status: 'all' | 'active' | 'scheduled' | 'expired') => void;
|
||||
onFilterStatusChange: (status: CouponStatusFilter) => void;
|
||||
onSearchChange: (query: string) => void;
|
||||
onPageChange: (page: number) => void;
|
||||
onPerPageChange: (perPage: number) => void;
|
||||
|
|
@ -42,7 +70,7 @@ export const CouponsTab: React.FC<CouponsTabProps> = ({
|
|||
};
|
||||
|
||||
const filteredCoupons = coupons.filter((c) => {
|
||||
if (filterStatus !== 'all' && c.schedule_status !== filterStatus) return false;
|
||||
if (filterStatus !== 'all' && getEffectiveCouponStatus(c) !== filterStatus) return false;
|
||||
if (searchQuery.trim()) {
|
||||
const q = searchQuery.toLowerCase();
|
||||
return (
|
||||
|
|
@ -94,7 +122,7 @@ export const CouponsTab: React.FC<CouponsTabProps> = ({
|
|||
<div className="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4 bg-slate-50/80 border border-slate-200/60 rounded-2xl p-4">
|
||||
{/* Filter Pills */}
|
||||
<div className="flex flex-wrap items-center gap-1.5">
|
||||
{(['all', 'active', 'scheduled', 'expired'] as const).map((status) => (
|
||||
{(['all', 'active', 'scheduled', 'fully_redeemed', 'expired'] as const).map((status) => (
|
||||
<button
|
||||
key={status}
|
||||
type="button"
|
||||
|
|
@ -102,7 +130,7 @@ export const CouponsTab: React.FC<CouponsTabProps> = ({
|
|||
onFilterStatusChange(status);
|
||||
onPageChange(1);
|
||||
}}
|
||||
className={`px-3 py-1.5 rounded-xl text-xs font-bold capitalize transition-all cursor-pointer ${
|
||||
className={`px-3 py-1.5 rounded-xl text-xs font-bold transition-all cursor-pointer ${
|
||||
filterStatus === status
|
||||
? 'bg-indigo-600 text-white shadow-sm'
|
||||
: 'bg-white text-slate-600 border border-slate-200 hover:bg-slate-100 shadow-sm'
|
||||
|
|
@ -110,7 +138,7 @@ export const CouponsTab: React.FC<CouponsTabProps> = ({
|
|||
>
|
||||
{status === 'all'
|
||||
? `All (${coupons.length})`
|
||||
: `${status} (${coupons.filter((c) => c.schedule_status === status).length})`}
|
||||
: `${STATUS_LABELS[status]} (${coupons.filter((c) => getEffectiveCouponStatus(c) === status).length})`}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
|
@ -164,6 +192,13 @@ export const CouponsTab: React.FC<CouponsTabProps> = ({
|
|||
const hasCap = isPercent && c.max_discount_limit > 0;
|
||||
const usageLimit = c.usage_limit || 0;
|
||||
const usageCount = c.usage_count || 0;
|
||||
const effectiveStatus = getEffectiveCouponStatus(c);
|
||||
const statusLabel =
|
||||
effectiveStatus === 'fully_redeemed'
|
||||
? 'Fully Redeemed'
|
||||
: effectiveStatus;
|
||||
const hasPerUserLimit =
|
||||
c.per_user_limit !== undefined && c.per_user_limit !== null;
|
||||
const hasSpecific =
|
||||
(c.applicable_plan_ids && c.applicable_plan_ids.length > 0) ||
|
||||
(c.applicable_module_ids && c.applicable_module_ids.length > 0);
|
||||
|
|
@ -230,19 +265,21 @@ export const CouponsTab: React.FC<CouponsTabProps> = ({
|
|||
<td className="py-3.5 px-4 whitespace-nowrap">
|
||||
<span
|
||||
className={`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-[10px] font-extrabold tracking-wider uppercase border ${
|
||||
c.schedule_status === 'active'
|
||||
effectiveStatus === 'active'
|
||||
? 'bg-emerald-50 text-emerald-700 border-emerald-200'
|
||||
: c.schedule_status === 'scheduled'
|
||||
: effectiveStatus === 'scheduled'
|
||||
? 'bg-amber-50 text-amber-700 border-amber-200'
|
||||
: c.schedule_status === 'expired'
|
||||
: effectiveStatus === 'fully_redeemed'
|
||||
? 'bg-indigo-50 text-indigo-700 border-indigo-200'
|
||||
: effectiveStatus === 'expired'
|
||||
? 'bg-rose-50 text-rose-700 border-rose-200'
|
||||
: 'bg-slate-100 text-slate-500 border-slate-200'
|
||||
}`}
|
||||
>
|
||||
{c.schedule_status === 'active' && (
|
||||
{effectiveStatus === 'active' && (
|
||||
<span className="h-1.5 w-1.5 rounded-full bg-emerald-500 animate-pulse" />
|
||||
)}
|
||||
{c.schedule_status}
|
||||
{statusLabel}
|
||||
</span>
|
||||
<p className="text-[10px] text-slate-400 font-medium mt-1">
|
||||
{c.start_date?.split('T')[0] || '—'} → {c.end_date?.split('T')[0] || '—'}
|
||||
|
|
@ -264,6 +301,17 @@ export const CouponsTab: React.FC<CouponsTabProps> = ({
|
|||
) : (
|
||||
<span className="text-[9px] text-slate-400 font-semibold">Unlimited</span>
|
||||
)}
|
||||
{hasPerUserLimit && (
|
||||
<span
|
||||
className={`block text-[9px] font-bold mt-1 ${
|
||||
c.per_user_limit === 0 ? 'text-rose-600' : 'text-slate-500'
|
||||
}`}
|
||||
>
|
||||
{c.per_user_limit === 0
|
||||
? 'Per user: 0 (Blocked)'
|
||||
: `Max ${c.per_user_limit} / user`}
|
||||
</span>
|
||||
)}
|
||||
</td>
|
||||
|
||||
{/* Scope & Conditions */}
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -3,14 +3,29 @@ import { XCircle, RefreshCw } from 'lucide-react';
|
|||
import type { ModuleItem } from '../../types';
|
||||
import { normalizeCategory } from '../../utils/category';
|
||||
|
||||
const ICON_PRESETS = [
|
||||
'medkit-outline',
|
||||
'heart-outline',
|
||||
'woman-outline',
|
||||
'ear-outline',
|
||||
'body-outline',
|
||||
'leaf-outline',
|
||||
'pulse-outline',
|
||||
'folder-outline',
|
||||
];
|
||||
|
||||
interface EditModuleModalProps {
|
||||
module: ModuleItem | null;
|
||||
name: string;
|
||||
description: string;
|
||||
category: string;
|
||||
icon: string;
|
||||
color: string;
|
||||
saving: boolean;
|
||||
onNameChange: (val: string) => void;
|
||||
onDescriptionChange: (val: string) => void;
|
||||
onIconChange: (val: string) => void;
|
||||
onColorChange: (val: string) => void;
|
||||
onSave: (e: React.FormEvent) => void;
|
||||
onClose: () => void;
|
||||
}
|
||||
|
|
@ -20,9 +35,13 @@ export const EditModuleModal: React.FC<EditModuleModalProps> = ({
|
|||
name,
|
||||
description,
|
||||
category,
|
||||
icon,
|
||||
color,
|
||||
saving,
|
||||
onNameChange,
|
||||
onDescriptionChange,
|
||||
onIconChange,
|
||||
onColorChange,
|
||||
onSave,
|
||||
onClose,
|
||||
}) => {
|
||||
|
|
@ -35,7 +54,7 @@ export const EditModuleModal: React.FC<EditModuleModalProps> = ({
|
|||
<div className="flex items-center gap-2">
|
||||
<div
|
||||
className="h-8 w-8 rounded-lg flex items-center justify-center font-bold text-white text-xs"
|
||||
style={{ backgroundColor: (module as any).color || '#6366F1' }}
|
||||
style={{ backgroundColor: color || (module as any).color || '#4F46E5' }}
|
||||
>
|
||||
{(module.name || 'M')[0].toUpperCase()}
|
||||
</div>
|
||||
|
|
@ -89,6 +108,55 @@ export const EditModuleModal: React.FC<EditModuleModalProps> = ({
|
|||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
|
||||
<div>
|
||||
<label className="block text-[10px] uppercase font-bold text-slate-500 mb-1">Mobile Icon (Ionicons)</label>
|
||||
<input
|
||||
type="text"
|
||||
value={icon}
|
||||
onChange={(e) => onIconChange(e.target.value)}
|
||||
placeholder="e.g. medkit-outline"
|
||||
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-mono text-slate-800 focus:outline-none focus:border-indigo-500"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-[10px] uppercase font-bold text-slate-500 mb-1">Mobile Accent Color</label>
|
||||
<div className="flex items-center gap-2">
|
||||
<input
|
||||
type="color"
|
||||
value={/^#[0-9A-Fa-f]{6}$/.test(color) ? color : '#4F46E5'}
|
||||
onChange={(e) => onColorChange(e.target.value)}
|
||||
className="h-9 w-10 rounded-lg border border-slate-200 cursor-pointer p-0.5 bg-white"
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
value={color}
|
||||
onChange={(e) => onColorChange(e.target.value)}
|
||||
placeholder="#4F46E5"
|
||||
className="flex-1 bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-mono text-slate-800 focus:outline-none focus:border-indigo-500"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
{ICON_PRESETS.map((preset) => (
|
||||
<button
|
||||
key={preset}
|
||||
type="button"
|
||||
onClick={() => onIconChange(preset)}
|
||||
className={`px-2 py-1 rounded-lg text-[10px] font-mono border transition-colors cursor-pointer ${
|
||||
icon === preset
|
||||
? 'bg-indigo-50 text-indigo-700 border-indigo-300 font-bold'
|
||||
: 'bg-slate-50 text-slate-600 border-slate-200 hover:bg-slate-100'
|
||||
}`}
|
||||
>
|
||||
{preset}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className="flex gap-2 pt-2">
|
||||
<button
|
||||
type="button"
|
||||
|
|
|
|||
|
|
@ -1119,9 +1119,8 @@ export const ModuleBuilderTabContent: React.FC = () => {
|
|||
key={m.code}
|
||||
type="button"
|
||||
onClick={() => handleSwitchModule(m.code)}
|
||||
className={`w-full text-left px-3.5 py-2.5 flex items-center justify-between hover:bg-indigo-50/50 transition-colors ${
|
||||
m.code === moduleCode ? 'bg-indigo-50/70 text-indigo-900 font-bold' : 'text-slate-700'
|
||||
}`}
|
||||
className={`w-full text-left px-3.5 py-2.5 flex items-center justify-between hover:bg-indigo-50/50 transition-colors ${m.code === moduleCode ? 'bg-indigo-50/70 text-indigo-900 font-bold' : 'text-slate-700'
|
||||
}`}
|
||||
>
|
||||
<div>
|
||||
<div className="text-xs font-bold flex items-center gap-1.5">
|
||||
|
|
@ -1650,11 +1649,10 @@ export const ModuleBuilderTabContent: React.FC = () => {
|
|||
{patterns.map((pat, idx) => (
|
||||
<div
|
||||
key={idx}
|
||||
className={`flex items-center gap-1 px-3 py-1.5 rounded-xl text-xs font-bold transition-all cursor-pointer ${
|
||||
safePatternIndex === idx
|
||||
className={`flex items-center gap-1 px-3 py-1.5 rounded-xl text-xs font-bold transition-all cursor-pointer ${safePatternIndex === idx
|
||||
? 'bg-indigo-600 text-white shadow-sm shadow-indigo-600/30'
|
||||
: 'bg-slate-100 hover:bg-slate-200 text-slate-700'
|
||||
}`}
|
||||
}`}
|
||||
onClick={() => {
|
||||
setActivePatternIndex(idx);
|
||||
if (error) setError(null);
|
||||
|
|
@ -1668,9 +1666,8 @@ export const ModuleBuilderTabContent: React.FC = () => {
|
|||
e.stopPropagation();
|
||||
handleRemovePattern(idx);
|
||||
}}
|
||||
className={`h-4 w-4 rounded-full flex items-center justify-center hover:bg-black/10 transition-colors ml-1 ${
|
||||
safePatternIndex === idx ? 'text-white' : 'text-slate-400 hover:text-slate-700'
|
||||
}`}
|
||||
className={`h-4 w-4 rounded-full flex items-center justify-center hover:bg-black/10 transition-colors ml-1 ${safePatternIndex === idx ? 'text-white' : 'text-slate-400 hover:text-slate-700'
|
||||
}`}
|
||||
title="Delete pattern"
|
||||
>
|
||||
<X className="h-2.5 w-2.5" />
|
||||
|
|
@ -1920,11 +1917,10 @@ export const ModuleBuilderTabContent: React.FC = () => {
|
|||
type="button"
|
||||
onClick={() => handleAddPointChip(pt)}
|
||||
disabled={isAdded}
|
||||
className={`text-[10px] font-bold px-2 py-0.5 rounded-lg border transition-all ${
|
||||
isAdded
|
||||
className={`text-[10px] font-bold px-2 py-0.5 rounded-lg border transition-all ${isAdded
|
||||
? 'bg-slate-200 text-slate-400 border-slate-200 cursor-default'
|
||||
: 'bg-white hover:bg-indigo-50 border-slate-200 hover:border-indigo-300 text-slate-700 hover:text-indigo-600'
|
||||
}`}
|
||||
}`}
|
||||
>
|
||||
+{pt}
|
||||
</button>
|
||||
|
|
@ -2090,11 +2086,10 @@ export const ModuleBuilderTabContent: React.FC = () => {
|
|||
key={idx}
|
||||
type="button"
|
||||
onClick={() => setPreviewPatternIdx(idx)}
|
||||
className={`px-3 py-1.5 rounded-xl text-xs font-bold transition-all ${
|
||||
previewPatternIdx === idx
|
||||
className={`px-3 py-1.5 rounded-xl text-xs font-bold transition-all ${previewPatternIdx === idx
|
||||
? 'bg-indigo-600 text-white shadow-xs'
|
||||
: 'bg-slate-100 hover:bg-slate-200 text-slate-700'
|
||||
}`}
|
||||
}`}
|
||||
>
|
||||
{p.name || `Pattern ${idx + 1}`}
|
||||
</button>
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@ interface EditPriceModalProps {
|
|||
priceInr: string;
|
||||
priceUsd: string;
|
||||
priceEur: string;
|
||||
allModules?: any[];
|
||||
moduleIds?: string[];
|
||||
saving: boolean;
|
||||
onNameChange: (v: string) => void;
|
||||
onDescChange: (v: string) => void;
|
||||
|
|
@ -20,6 +22,9 @@ interface EditPriceModalProps {
|
|||
onPriceInrChange: (v: string) => void;
|
||||
onPriceUsdChange: (v: string) => void;
|
||||
onPriceEurChange: (v: string) => void;
|
||||
onToggleModule?: (id: string) => void;
|
||||
onSelectAllModules?: () => void;
|
||||
onClearAllModules?: () => void;
|
||||
onSave: (e: React.FormEvent) => void;
|
||||
onClose: () => void;
|
||||
}
|
||||
|
|
@ -34,6 +39,8 @@ export const EditPriceModal: React.FC<EditPriceModalProps> = ({
|
|||
priceInr,
|
||||
priceUsd,
|
||||
priceEur,
|
||||
allModules = [],
|
||||
moduleIds = [],
|
||||
saving,
|
||||
onNameChange,
|
||||
onDescChange,
|
||||
|
|
@ -43,6 +50,9 @@ export const EditPriceModal: React.FC<EditPriceModalProps> = ({
|
|||
onPriceInrChange,
|
||||
onPriceUsdChange,
|
||||
onPriceEurChange,
|
||||
onToggleModule,
|
||||
onSelectAllModules,
|
||||
onClearAllModules,
|
||||
onSave,
|
||||
onClose,
|
||||
}) => {
|
||||
|
|
@ -168,6 +178,59 @@ export const EditPriceModal: React.FC<EditPriceModalProps> = ({
|
|||
</div>
|
||||
</div>
|
||||
|
||||
{(plan.plan_type === 'bundle' || plan.base_code === 'PATTERN_CORE' || plan.base_code === 'NUTRITION_CORE') && allModules.length > 0 && onToggleModule && (
|
||||
<div className="space-y-2 border-t border-slate-100 pt-3">
|
||||
<div className="flex items-center justify-between">
|
||||
<label className="text-xs font-bold text-slate-600">
|
||||
Included Modules ({moduleIds.length} / {allModules.length})
|
||||
</label>
|
||||
<div className="flex items-center gap-2">
|
||||
{onSelectAllModules && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={onSelectAllModules}
|
||||
className="text-[11px] font-bold text-indigo-600 hover:text-indigo-800 cursor-pointer"
|
||||
>
|
||||
Select All
|
||||
</button>
|
||||
)}
|
||||
{onClearAllModules && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClearAllModules}
|
||||
className="text-[11px] font-bold text-slate-400 hover:text-slate-600 cursor-pointer"
|
||||
>
|
||||
Clear
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="max-h-40 overflow-y-auto rounded-xl border border-slate-200 bg-slate-50 p-2 grid grid-cols-1 sm:grid-cols-2 gap-1.5">
|
||||
{allModules.map((mod) => {
|
||||
const checked = moduleIds.includes(mod.id);
|
||||
return (
|
||||
<label
|
||||
key={mod.id}
|
||||
className={`flex items-center gap-2 px-2.5 py-1.5 rounded-lg text-xs cursor-pointer transition-colors ${
|
||||
checked
|
||||
? 'bg-indigo-50 text-indigo-800 border border-indigo-200 font-semibold'
|
||||
: 'bg-white text-slate-600 border border-slate-200 hover:bg-slate-100'
|
||||
}`}
|
||||
>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={checked}
|
||||
onChange={() => onToggleModule(mod.id)}
|
||||
className="rounded border-slate-300 text-indigo-600 focus:ring-indigo-500"
|
||||
/>
|
||||
<span className="truncate">{mod.name}</span>
|
||||
</label>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex gap-2 pt-2">
|
||||
<button
|
||||
type="button"
|
||||
|
|
|
|||
|
|
@ -158,6 +158,11 @@ export const PlansTab: React.FC<PlansTabProps> = ({
|
|||
<div className="space-y-0.5 max-w-xs">
|
||||
<div className="flex items-center gap-1.5">
|
||||
<span className="font-bold text-slate-800">{plan.name}</span>
|
||||
{plan.pricing_status === 'pending_pricing' && (
|
||||
<span className="inline-flex items-center px-2 py-0.5 rounded-full text-[10px] font-bold bg-amber-100 text-amber-800 border border-amber-300">
|
||||
Needs Pricing
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<code className="text-[10px] font-mono text-indigo-600 bg-indigo-50 px-1.5 py-0.5 rounded border border-indigo-100 font-bold">
|
||||
|
|
@ -286,11 +291,22 @@ export const PlansTab: React.FC<PlansTabProps> = ({
|
|||
<td className="py-3.5 px-3 text-center whitespace-nowrap">
|
||||
<button
|
||||
type="button"
|
||||
disabled={plan.pricing_status === 'pending_pricing'}
|
||||
onClick={() => onTogglePlanStatus(plan)}
|
||||
className={`relative inline-flex h-5 w-9 shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none ${
|
||||
plan.is_active ? 'bg-indigo-600' : 'bg-slate-300'
|
||||
className={`relative inline-flex h-5 w-9 shrink-0 rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none ${
|
||||
plan.pricing_status === 'pending_pricing'
|
||||
? 'bg-slate-200 cursor-not-allowed opacity-60'
|
||||
: plan.is_active
|
||||
? 'bg-indigo-600 cursor-pointer'
|
||||
: 'bg-slate-300 cursor-pointer'
|
||||
}`}
|
||||
title={plan.is_active ? 'Click to deactivate' : 'Click to activate'}
|
||||
title={
|
||||
plan.pricing_status === 'pending_pricing'
|
||||
? 'Set prices via Edit Info & Price before activating'
|
||||
: plan.is_active
|
||||
? 'Click to deactivate'
|
||||
: 'Click to activate'
|
||||
}
|
||||
>
|
||||
<span
|
||||
className={`pointer-events-none inline-block h-4 w-4 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out ${
|
||||
|
|
@ -299,7 +315,11 @@ export const PlansTab: React.FC<PlansTabProps> = ({
|
|||
/>
|
||||
</button>
|
||||
<span className="block text-[9px] font-bold text-slate-400 mt-0.5">
|
||||
{plan.is_active ? 'Active' : 'Inactive'}
|
||||
{plan.pricing_status === 'pending_pricing'
|
||||
? 'Needs Pricing'
|
||||
: plan.is_active
|
||||
? 'Active'
|
||||
: 'Inactive'}
|
||||
</span>
|
||||
</td>
|
||||
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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 };
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue