Files
biblical-guide.com/app/api/admin/analytics/users/route.ts
Andrei 2074ee3bda Complete admin dashboard implementation with comprehensive features
🚀 Major Update: v2.0.0 - Complete Administrative Dashboard

## Phase 1: Dashboard Overview & Authentication 
- Secure admin authentication with JWT tokens
- Beautiful overview dashboard with key metrics
- Role-based access control (admin, moderator permissions)
- Professional MUI design with responsive layout

## Phase 2: User Management & Content Moderation 
- Complete user management with advanced data grid
- Prayer request content moderation system
- User actions: view, suspend, activate, promote, delete
- Content approval/rejection workflows

## Phase 3: Analytics Dashboard 
- Comprehensive analytics with interactive charts (Recharts)
- User activity analytics with retention tracking
- Content engagement metrics and trends
- Real-time statistics and performance monitoring

## Phase 4: Chat Monitoring & System Administration 
- Advanced conversation monitoring with content analysis
- System health monitoring and backup management
- Security oversight and automated alerts
- Complete administrative control panel

## Key Features Added:
 **32 new API endpoints** for complete admin functionality
 **Material-UI DataGrid** with advanced filtering and pagination
 **Interactive Charts** using Recharts library
 **Real-time Monitoring** with auto-refresh capabilities
 **System Health Dashboard** with performance metrics
 **Database Backup System** with automated scheduling
 **Content Filtering** with automated moderation alerts
 **Role-based Permissions** with granular access control
 **Professional UI/UX** with consistent MUI design
 **Visit Website Button** in admin header for easy navigation

## Technical Implementation:
- **Frontend**: Material-UI components with responsive design
- **Backend**: 32 new API routes with proper authentication
- **Database**: Optimized queries with proper indexing
- **Security**: Admin-specific JWT authentication
- **Performance**: Efficient data loading with pagination
- **Charts**: Interactive visualizations with Recharts

The Biblical Guide application now provides world-class administrative capabilities for complete platform management!

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-23 12:01:34 +00:00

224 lines
5.7 KiB
TypeScript

import { NextResponse } from 'next/server';
import { prisma } from '@/lib/db';
import { getCurrentAdmin, AdminPermission, hasPermission } from '@/lib/admin-auth';
export const runtime = 'nodejs';
export async function GET(request: Request) {
try {
const admin = await getCurrentAdmin();
if (!admin || !hasPermission(admin, AdminPermission.VIEW_ANALYTICS)) {
return NextResponse.json(
{ error: 'Unauthorized' },
{ status: 401 }
);
}
const url = new URL(request.url);
const period = url.searchParams.get('period') || '30'; // days
const periodDays = parseInt(period);
const startDate = new Date();
startDate.setDate(startDate.getDate() - periodDays);
// User registration timeline (last 30 days)
const registrationTimeline = await Promise.all(
Array.from({ length: periodDays }, (_, i) => {
const date = new Date();
date.setDate(date.getDate() - i);
return date.toISOString().split('T')[0];
}).reverse().map(async (date) => {
const startOfDay = new Date(date + 'T00:00:00.000Z');
const endOfDay = new Date(date + 'T23:59:59.999Z');
const registrations = await prisma.user.count({
where: {
createdAt: {
gte: startOfDay,
lte: endOfDay
}
}
});
return {
date,
registrations
};
})
);
// User activity patterns (login frequency)
const userActivityPatterns = await prisma.user.findMany({
select: {
id: true,
email: true,
name: true,
role: true,
createdAt: true,
lastLoginAt: true,
_count: {
select: {
chatConversations: true,
prayerRequests: true,
bookmarks: true,
notes: true
}
}
},
orderBy: {
lastLoginAt: 'desc'
},
take: 100
});
// Most active users (by total activity)
const mostActiveUsers = userActivityPatterns
.map(user => ({
...user,
totalActivity:
user._count.chatConversations +
user._count.prayerRequests +
user._count.bookmarks +
user._count.notes
}))
.sort((a, b) => b.totalActivity - a.totalActivity)
.slice(0, 20);
// User retention analysis
const thirtyDaysAgo = new Date();
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
const sevenDaysAgo = new Date();
sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7);
const newUsersLast30Days = await prisma.user.count({
where: {
createdAt: {
gte: thirtyDaysAgo
}
}
});
const activeUsersLast30Days = await prisma.user.count({
where: {
createdAt: {
gte: thirtyDaysAgo
},
lastLoginAt: {
gte: sevenDaysAgo
}
}
});
const retentionRate = newUsersLast30Days > 0 ? (activeUsersLast30Days / newUsersLast30Days) * 100 : 0;
// User engagement by feature
const featureUsage = {
chat: await prisma.chatConversation.count({
where: {
createdAt: {
gte: startDate
}
}
}),
prayers: await prisma.prayerRequest.count({
where: {
createdAt: {
gte: startDate
}
}
}),
bookmarks: await prisma.bookmark.count({
where: {
createdAt: {
gte: startDate
}
}
}),
notes: await prisma.note.count({
where: {
createdAt: {
gte: startDate
}
}
})
};
// User demographics (by role and creation time)
const userDemographics = await prisma.user.groupBy({
by: ['role'],
_count: {
role: true
},
_min: {
createdAt: true
},
_max: {
createdAt: true
}
});
// Session length analysis (approximate based on conversation activity)
const sessionAnalysis = await prisma.chatConversation.findMany({
select: {
userId: true,
createdAt: true,
lastMessageAt: true,
_count: {
select: {
messages: true
}
}
},
where: {
createdAt: {
gte: startDate
},
userId: {
not: null
}
},
orderBy: {
lastMessageAt: 'desc'
},
take: 1000
});
const avgSessionLength = sessionAnalysis.reduce((acc, session) => {
const duration = new Date(session.lastMessageAt).getTime() - new Date(session.createdAt).getTime();
return acc + (duration / 1000 / 60); // minutes
}, 0) / sessionAnalysis.length || 0;
const avgMessagesPerSession = sessionAnalysis.reduce((acc, session) => {
return acc + session._count.messages;
}, 0) / sessionAnalysis.length || 0;
return NextResponse.json({
period: periodDays,
timeline: {
registrations: registrationTimeline
},
activity: {
patterns: userActivityPatterns.slice(0, 50), // Limit for performance
mostActive: mostActiveUsers
},
retention: {
rate: Math.round(retentionRate * 100) / 100,
newUsers: newUsersLast30Days,
activeUsers: activeUsersLast30Days
},
engagement: {
featureUsage,
avgSessionLength: Math.round(avgSessionLength * 100) / 100,
avgMessagesPerSession: Math.round(avgMessagesPerSession * 100) / 100
},
demographics: userDemographics
});
} catch (error) {
console.error('Admin user analytics error:', error);
return NextResponse.json(
{ error: 'Server error' },
{ status: 500 }
);
}
}