Spaces:
Sleeping
Sleeping
Nathan commited on
Commit ·
28f892f
1
Parent(s): 9bcbd91
Admin: Enabled User Management & Plan Control. Fixed Stats 0 issue.
Browse filesFiles changed (2)
- dashboard/admin.html+18-1
- server.js+49-21
152 unmodified lines153154155156156157158159160161162163164165166167168169170171172173174175176177178179180181152 unmodified lines <td><span class="badge ${u.plan === 'ultra' ? 'badge-ultra' : (u.plan === 'premium' ? 'badge-premium' : 'badge-free')}">${u.plan?.toUpperCase() || 'FREE'}</span></td> <td><div style="display: flex; align-items: center; gap: 8px;"><div style="width: 10px; height: 10px; border-radius: 50%; background: ${u.channels?.[0]?.is_active ? 'var(--success)' : '#475569'};"></div>${u.channels?.[0]?.is_active ? 'Active' : 'Offline'}</div></td> <td> <div style="display:flex; gap:8px;"> <button onclick="adminAction('${u.id}', 'ultra')" style="background: rgba(245, 158, 11, 0.1); color: #f59e0b; border: 1px solid #f59e0b; padding: 6px 12px; border-radius: 10px; cursor: pointer; font-weight: 800;">GOD MODE</button> <button onclick="adminAction('${u.id}', 'ultra')" style="background: rgba(245, 158, 11, 0.1); color: #f59e0b; border: 1px solid #f59e0b; padding: 6px 12px; border-radius: 10px; cursor: pointer; font-weight: 800; font-size: 0.7rem;">ULTRA</button> <button onclick="adminAction('${u.id}', 'pro')" style="background: rgba(99, 102, 241, 0.1); color: var(--accent); border: 1px solid var(--accent); padding: 6px 12px; border-radius: 10px; cursor: pointer; font-weight: 800; font-size: 0.7rem;">PRO</button> <button onclick="adminAction('${u.id}', 'free')" style="background: rgba(255, 255, 255, 0.05); color: #fff; border: 1px solid var(--glass-border); padding: 6px 12px; border-radius: 10px; cursor: pointer; font-weight: 800; font-size: 0.7rem;">FREE</button> </div> </td> </tr> `).join(''); }
async function adminAction(userId, plan) { if (!confirm(`Change user to ${plan.toUpperCase()}?`)) return; const res = await fetch('/api/admin/update-plan', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${authToken}` }, body: JSON.stringify({ userId, plan }) }); if (res.ok) { alert("User Plan Updated! 🚀"); loadAdminData(); } }
async function loadAdminPricing() { const res = await fetch('/api/platform/pricing'); const p = await res.json();325 unmodified lines326327328329330331332333334335336337338339340341342343344345346347348349350351352353332333334335336337338339340341342343344345346347348349350351352354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383325 unmodified lines }});
// --- Admin Control API ---app.get('/api/admin/users', authenticateAdmin, async (req, res) => { try { const { data: users, error } = await db.from('users').select('id, email, plan, role, created_at').order('created_at', { ascending: false }); if (error) throw error; res.json(users); } catch (e) { res.status(500).json({ error: e.message }); }});
app.post('/api/admin/update-plan', authenticateAdmin, async (req, res) => { const { userId, plan } = req.body; try { const { error } = await db.from('users').update({ plan }).eq('id', userId); if (error) throw error; res.json({ success: true, msg: `User updated to ${plan}` }); } catch (e) { res.status(500).json({ error: e.message }); }});
// --- Dashboard API ---
app.get('/api/stats', authenticateToken, async (req, res) => { // 1. Fetch channel data (niche, stats) - Prioritize active channels const { data: channel } = await db.from('channels') .select('niche, preferred_hours, total_views, subscribers') .eq('user_id', req.userId) .order('is_active', { ascending: false }) .limit(1).maybeSingle(); // 2. Fetch user data (custom keys) const { data: user } = await db.from('users').select('custom_client_id, custom_client_secret').eq('id', req.userId).single(); const { count: videosGenerated } = await db.from('activity').select('*', { count: 'exact', head: true }).eq('user_id', req.userId).eq('type', 'VIDEO_PUBLISHED'); res.json({ videosGenerated: videosGenerated || 0, totalViews: channel?.total_views || 0, subscribers: channel?.subscribers || 0, niche: channel ? channel.niche : 'Not set', preferred_hours: channel ? channel.preferred_hours : ['10:00'], custom_client_id: user?.custom_client_id || '', custom_client_secret: user?.custom_client_secret || '' }); try { // 1. Get the primary channel for stats const { data: channel } = await db.from('channels') .select('*') .eq('user_id', req.userId) .order('is_active', { ascending: false }) .limit(1) .maybeSingle();
// 2. Count total videos published across all channels for this user const { count: videosProduced } = await db.from('activity') .select('*', { count: 'exact', head: true }) .eq('user_id', req.userId) .eq('type', 'VIDEO_PUBLISHED'); res.json({ videosProduced: videosProduced || 0, totalViews: channel?.total_views || 0, subscribers: channel?.subscribers || 0, niche: channel?.niche || 'Facts', custom_client_id: channel?.custom_client_id || '', custom_client_secret: channel?.custom_client_secret || '', preferred_hours: channel?.preferred_hours || ['10:00'] }); } catch (e) { res.status(500).json({ error: e.message }); }});
app.get('/api/youtube/status', authenticateToken, async (req, res) => {dashboard/admin.html+18-1
server.js+49-21