Compare commits

..

No commits in common. "fccb283057dbd23aa6ff4ddd1d76e00bf7ac4115" and "660b7493e9c9c5432cef8c284a85ef88e191e83e" have entirely different histories.

2 changed files with 21 additions and 67 deletions

View File

@ -23,34 +23,6 @@ import { DatabaseBackupTab } from './components/database/DatabaseBackupTab';
import { ReportsExportTab } from './components/database/ReportsExportTab'; import { ReportsExportTab } from './components/database/ReportsExportTab';
import { AdminLoginForm } from './components/auth/AdminLoginForm'; import { AdminLoginForm } from './components/auth/AdminLoginForm';
export function extractErrorMessage(err: any, fallback: string = 'An error occurred.'): string {
if (!err) return fallback;
const detail = err.response?.data?.detail ?? err.response?.data?.message ?? err.message;
if (typeof detail === 'string') {
return detail;
}
if (detail && typeof detail === 'object') {
if (typeof detail.message === 'string' && detail.message) {
return detail.message;
}
if (Array.isArray(detail.issues) && detail.issues.length > 0) {
return detail.issues.map((i: any) => i.msg || JSON.stringify(i)).join('; ');
}
if (Array.isArray(detail.context?.issues) && detail.context.issues.length > 0) {
return detail.context.issues.map((i: any) => i.msg || JSON.stringify(i)).join('; ');
}
if (Array.isArray(detail) && detail.length > 0) {
return detail.map((i: any) => i.msg || (typeof i === 'string' ? i : JSON.stringify(i))).join('; ');
}
try {
return JSON.stringify(detail);
} catch {
return fallback;
}
}
return fallback;
}
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);
@ -288,7 +260,7 @@ export default function App() {
setUserToken(token); setUserToken(token);
fetchUserProfile(token); fetchUserProfile(token);
} catch (err: any) { } catch (err: any) {
setAdminAuthError(extractErrorMessage(err, 'Invalid administrative credentials.')); setAdminAuthError(err.response?.data?.detail || 'Invalid administrative credentials.');
} finally { } finally {
setAdminAuthLoading(false); setAdminAuthLoading(false);
} }
@ -315,7 +287,7 @@ export default function App() {
await fetchUserProfile(userToken!); await fetchUserProfile(userToken!);
await fetchRazorpayAdminData(); await fetchRazorpayAdminData();
} catch (err: any) { } catch (err: any) {
const errorMsg = extractErrorMessage(err, 'Failed to sync with Razorpay Gateway.'); const errorMsg = err.response?.data?.detail || err.message || String(err);
setAdminLogs((prev) => [`[Admin Sync] FAILED: ${errorMsg}`, ...prev]); setAdminLogs((prev) => [`[Admin Sync] FAILED: ${errorMsg}`, ...prev]);
} finally { } finally {
setAdminSyncing(false); setAdminSyncing(false);
@ -357,7 +329,7 @@ export default function App() {
setTimeout(() => setDbBackupSuccess(null), 8000); setTimeout(() => setDbBackupSuccess(null), 8000);
} catch (err: any) { } catch (err: any) {
setDbBackupError( setDbBackupError(
extractErrorMessage(err, 'Failed to download database backup. Ensure database client is active.') err.response?.data?.detail || 'Failed to download database backup. Ensure database client is active.'
); );
} finally { } finally {
setDbBackupDownloading(null); setDbBackupDownloading(null);
@ -423,7 +395,7 @@ export default function App() {
setReportExportSuccess(`Successfully downloaded ${reportType} report (${filename})`); setReportExportSuccess(`Successfully downloaded ${reportType} report (${filename})`);
setTimeout(() => setReportExportSuccess(null), 8000); setTimeout(() => setReportExportSuccess(null), 8000);
} catch (err: any) { } catch (err: any) {
setReportExportError(extractErrorMessage(err, 'Failed to export report CSV.')); setReportExportError(err.response?.data?.detail || 'Failed to export report CSV.');
} finally { } finally {
setReportDownloading(null); setReportDownloading(null);
} }
@ -441,7 +413,7 @@ export default function App() {
}); });
setCustomerDrawerData(res.data); setCustomerDrawerData(res.data);
} catch (err: any) { } catch (err: any) {
alert(extractErrorMessage(err, 'Failed to load customer details.')); alert(err.response?.data?.detail || 'Failed to load customer details.');
} finally { } finally {
setCustomerDrawerLoading(false); setCustomerDrawerLoading(false);
} }
@ -466,7 +438,7 @@ export default function App() {
); );
setAdminLogs((prev) => [`[Customer Control] ${res.data.message}`, ...prev]); setAdminLogs((prev) => [`[Customer Control] ${res.data.message}`, ...prev]);
} catch (err: any) { } catch (err: any) {
alert(extractErrorMessage(err, 'Failed to update account status.')); alert(err.response?.data?.detail || 'Failed to update account status.');
} finally { } finally {
setCustomerStatusToggling(false); setCustomerStatusToggling(false);
} }
@ -517,7 +489,7 @@ export default function App() {
setAdminModulesList((prev) => setAdminModulesList((prev) =>
prev.map((m) => (m.id === mod.id ? { ...m, is_active: !newActive, status: !newActive ? 'active' : 'inactive' } : m)) prev.map((m) => (m.id === mod.id ? { ...m, is_active: !newActive, status: !newActive ? 'active' : 'inactive' } : m))
); );
alert(extractErrorMessage(err, 'Failed to update module status.')); alert(err.response?.data?.detail || 'Failed to update module status.');
} }
}; };
@ -553,7 +525,7 @@ export default function App() {
setSelectedEditModule(null); setSelectedEditModule(null);
setAdminLogs((prev) => [`[Catalog] Updated metadata for '${editModuleName}'`, ...prev]); setAdminLogs((prev) => [`[Catalog] Updated metadata for '${editModuleName}'`, ...prev]);
} catch (err: any) { } catch (err: any) {
alert(extractErrorMessage(err, 'Failed to update module details.')); alert(err.response?.data?.detail || 'Failed to update module details.');
} finally { } finally {
setEditModuleSaving(false); setEditModuleSaving(false);
} }
@ -580,7 +552,7 @@ export default function App() {
setAdminPlansList((prev) => setAdminPlansList((prev) =>
prev.map((p) => (p.base_code === plan.base_code ? { ...p, is_active: !newActive } : p)) prev.map((p) => (p.base_code === plan.base_code ? { ...p, is_active: !newActive } : p))
); );
alert(extractErrorMessage(err, 'Failed to update plan status.')); alert(err.response?.data?.detail || 'Failed to update plan status.');
} }
}; };
@ -641,7 +613,7 @@ export default function App() {
...prev, ...prev,
]); ]);
} catch (err: any) { } catch (err: any) {
alert(extractErrorMessage(err, 'Failed to update plan.')); alert(err.response?.data?.detail || 'Failed to update plan.');
} finally { } finally {
setEditPriceSaving(false); setEditPriceSaving(false);
} }
@ -711,7 +683,7 @@ export default function App() {
} }
setShowCreatePlanModal(false); setShowCreatePlanModal(false);
} catch (err: any) { } catch (err: any) {
setCreatePlanError(extractErrorMessage(err, 'Failed to create custom plan.')); setCreatePlanError(err.response?.data?.detail || 'Failed to create custom plan.');
} finally { } finally {
setCreatePlanSaving(false); setCreatePlanSaving(false);
} }
@ -771,12 +743,6 @@ export default function App() {
const handleSaveCoupon = async (e: React.FormEvent) => { const handleSaveCoupon = async (e: React.FormEvent) => {
e.preventDefault(); e.preventDefault();
if (!couponFormData.code.trim() || !userToken) return; if (!couponFormData.code.trim() || !userToken) return;
if (couponFormData.startDate && couponFormData.endDate && couponFormData.endDate <= couponFormData.startDate) {
alert('Invalid date range: Expiration date must be later than start date.');
return;
}
setCouponSaving(true); setCouponSaving(true);
try { try {
const headers = { Authorization: `Bearer ${userToken}` }; const headers = { Authorization: `Bearer ${userToken}` };
@ -819,7 +785,7 @@ export default function App() {
setSelectedEditCoupon(null); setSelectedEditCoupon(null);
await fetchRazorpayAdminData(); await fetchRazorpayAdminData();
} catch (err: any) { } catch (err: any) {
alert(extractErrorMessage(err, 'Failed to save coupon.')); alert(err.response?.data?.detail || 'Failed to save coupon.');
} finally { } finally {
setCouponSaving(false); setCouponSaving(false);
} }
@ -846,7 +812,7 @@ export default function App() {
setAdminCouponsList((prev) => setAdminCouponsList((prev) =>
prev.map((item) => (item.id === c.id ? { ...item, is_active: !newActive } : item)) prev.map((item) => (item.id === c.id ? { ...item, is_active: !newActive } : item))
); );
alert(extractErrorMessage(err, 'Failed to update coupon status.')); alert(err.response?.data?.detail || 'Failed to update coupon status.');
} }
}; };
@ -859,7 +825,7 @@ export default function App() {
setAdminCouponsList((prev) => prev.filter((item) => item.id !== c.id)); setAdminCouponsList((prev) => prev.filter((item) => item.id !== c.id));
setAdminLogs((prev) => [`[Coupons] Deleted promo code ${c.code}.`, ...prev]); setAdminLogs((prev) => [`[Coupons] Deleted promo code ${c.code}.`, ...prev]);
} catch (err: any) { } catch (err: any) {
alert(extractErrorMessage(err, 'Failed to delete coupon.')); alert(err.response?.data?.detail || 'Failed to delete coupon.');
} }
}; };
@ -884,7 +850,7 @@ export default function App() {
throw new Error('Invoice URL is missing in server response.'); throw new Error('Invoice URL is missing in server response.');
} }
} catch (err: any) { } catch (err: any) {
alert(extractErrorMessage(err, 'Could not download invoice.')); alert(err.response?.data?.detail || 'Could not download invoice.');
} finally { } finally {
setDownloadingInvoiceId(null); setDownloadingInvoiceId(null);
} }
@ -898,7 +864,7 @@ export default function App() {
const res = await axios.post(`${API_BASE}/api/admin/payments/${paymentId}/share-invoice`, {}, { headers }); const res = await axios.post(`${API_BASE}/api/admin/payments/${paymentId}/share-invoice`, {}, { headers });
alert(res.data?.message || 'Invoice shared successfully via email!'); alert(res.data?.message || 'Invoice shared successfully via email!');
} catch (err: any) { } catch (err: any) {
alert(extractErrorMessage(err, 'Could not share invoice.')); alert(err.response?.data?.detail || 'Could not share invoice.');
} finally { } finally {
setSharingInvoiceId(null); setSharingInvoiceId(null);
} }
@ -926,7 +892,7 @@ export default function App() {
setCreateAdminName(''); setCreateAdminName('');
await fetchRazorpayAdminData(); await fetchRazorpayAdminData();
} catch (err: any) { } catch (err: any) {
alert(`Admin creation failed: ${extractErrorMessage(err, 'Admin creation failed.')}`); alert(`Admin creation failed: ${err.response?.data?.detail || err.message}`);
} finally { } finally {
setAdminActionLoading(false); setAdminActionLoading(false);
} }

View File

@ -42,7 +42,6 @@ export const CouponModal: React.FC<CouponModalProps> = ({
if (!mode) return null; if (!mode) return null;
const isEdit = mode === 'edit'; const isEdit = mode === 'edit';
const isDateRangeInvalid = Boolean(data.startDate && data.endDate && data.endDate <= data.startDate);
return ( return (
<div className="fixed inset-0 z-50 bg-slate-900/60 backdrop-blur-xs flex items-center justify-center p-4"> <div className="fixed inset-0 z-50 bg-slate-900/60 backdrop-blur-xs flex items-center justify-center p-4">
@ -172,9 +171,7 @@ export const CouponModal: React.FC<CouponModalProps> = ({
type="date" type="date"
value={data.startDate} value={data.startDate}
onChange={(e) => onChange('startDate', e.target.value)} onChange={(e) => onChange('startDate', e.target.value)}
className={`w-full bg-white border rounded-xl px-3 py-2 text-xs text-slate-800 focus:outline-none cursor-pointer ${ 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 cursor-pointer"
isDateRangeInvalid ? 'border-rose-400 focus:border-rose-500' : 'border-slate-200 focus:border-indigo-500'
}`}
/> />
</div> </div>
<div> <div>
@ -183,19 +180,10 @@ export const CouponModal: React.FC<CouponModalProps> = ({
type="date" type="date"
value={data.endDate} value={data.endDate}
onChange={(e) => onChange('endDate', e.target.value)} onChange={(e) => onChange('endDate', e.target.value)}
className={`w-full bg-white border rounded-xl px-3 py-2 text-xs text-slate-800 focus:outline-none cursor-pointer ${ 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 cursor-pointer"
isDateRangeInvalid ? 'border-rose-400 focus:border-rose-500' : 'border-slate-200 focus:border-indigo-500'
}`}
/> />
</div> </div>
</div> </div>
{isDateRangeInvalid && (
<div className="text-xs font-semibold text-rose-600 bg-rose-50 border border-rose-200 rounded-xl px-3 py-2 flex items-center gap-2">
<span>⚠️</span>
<span>Invalid date range: Expiration date must be later than start date.</span>
</div>
)}
</div> </div>
{/* BLOCK 3: Scope & Item Restrictions */} {/* BLOCK 3: Scope & Item Restrictions */}
@ -341,8 +329,8 @@ export const CouponModal: React.FC<CouponModalProps> = ({
</button> </button>
<button <button
type="submit" type="submit"
disabled={saving || isDateRangeInvalid} disabled={saving}
className="flex-1 py-2.5 rounded-xl text-xs font-bold bg-indigo-600 hover:bg-indigo-500 text-white transition-colors flex items-center justify-center gap-1.5 shadow-md shadow-indigo-600/20 cursor-pointer disabled:opacity-50 disabled:cursor-not-allowed" className="flex-1 py-2.5 rounded-xl text-xs font-bold bg-indigo-600 hover:bg-indigo-500 text-white transition-colors flex items-center justify-center gap-1.5 shadow-md shadow-indigo-600/20 cursor-pointer disabled:opacity-50"
> >
{saving ? <RefreshCw className="h-3.5 w-3.5 animate-spin" /> : isEdit ? 'Save Changes' : 'Save & Publish Coupon'} {saving ? <RefreshCw className="h-3.5 w-3.5 animate-spin" /> : isEdit ? 'Save Changes' : 'Save & Publish Coupon'}
</button> </button>