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>
This commit is contained in:
272
app/api/admin/analytics/content/route.ts
Normal file
272
app/api/admin/analytics/content/route.ts
Normal file
@@ -0,0 +1,272 @@
|
||||
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);
|
||||
|
||||
// Prayer request engagement
|
||||
const prayerRequestEngagement = await prisma.prayerRequest.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
category: true,
|
||||
author: true,
|
||||
prayerCount: true,
|
||||
createdAt: true,
|
||||
isActive: true,
|
||||
_count: {
|
||||
select: {
|
||||
prayers: true,
|
||||
userPrayers: true
|
||||
}
|
||||
}
|
||||
},
|
||||
where: {
|
||||
createdAt: {
|
||||
gte: startDate
|
||||
}
|
||||
},
|
||||
orderBy: {
|
||||
prayerCount: 'desc'
|
||||
},
|
||||
take: 50
|
||||
});
|
||||
|
||||
// Prayer request engagement timeline
|
||||
const prayerEngagementTimeline = 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 [newRequests, newPrayers] = await Promise.all([
|
||||
prisma.prayerRequest.count({
|
||||
where: {
|
||||
createdAt: {
|
||||
gte: startOfDay,
|
||||
lte: endOfDay
|
||||
}
|
||||
}
|
||||
}),
|
||||
prisma.prayer.count({
|
||||
where: {
|
||||
createdAt: {
|
||||
gte: startOfDay,
|
||||
lte: endOfDay
|
||||
}
|
||||
}
|
||||
})
|
||||
]);
|
||||
|
||||
return {
|
||||
date,
|
||||
newRequests,
|
||||
newPrayers
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
// Chat conversation engagement
|
||||
const chatEngagement = await prisma.chatConversation.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
language: true,
|
||||
createdAt: true,
|
||||
lastMessageAt: true,
|
||||
isActive: true,
|
||||
_count: {
|
||||
select: {
|
||||
messages: true
|
||||
}
|
||||
}
|
||||
},
|
||||
where: {
|
||||
createdAt: {
|
||||
gte: startDate
|
||||
}
|
||||
},
|
||||
orderBy: {
|
||||
lastMessageAt: 'desc'
|
||||
},
|
||||
take: 50
|
||||
});
|
||||
|
||||
// Most bookmarked verses
|
||||
const mostBookmarkedVerses = await prisma.bookmark.groupBy({
|
||||
by: ['verseId'],
|
||||
_count: {
|
||||
verseId: true
|
||||
},
|
||||
where: {
|
||||
createdAt: {
|
||||
gte: startDate
|
||||
}
|
||||
},
|
||||
orderBy: {
|
||||
_count: {
|
||||
verseId: 'desc'
|
||||
}
|
||||
},
|
||||
take: 20
|
||||
});
|
||||
|
||||
// Get verse details for bookmarked verses
|
||||
const verseDetails = await Promise.all(
|
||||
mostBookmarkedVerses.map(async (bookmark) => {
|
||||
const verse = await prisma.bibleVerse.findUnique({
|
||||
where: { id: bookmark.verseId },
|
||||
select: {
|
||||
id: true,
|
||||
verseNum: true,
|
||||
text: true,
|
||||
chapter: {
|
||||
select: {
|
||||
chapterNum: true,
|
||||
book: {
|
||||
select: {
|
||||
name: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
...bookmark,
|
||||
verse
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
// Content categories performance
|
||||
const categoryPerformance = await prisma.prayerRequest.groupBy({
|
||||
by: ['category'],
|
||||
_sum: {
|
||||
prayerCount: true
|
||||
},
|
||||
_count: {
|
||||
category: true
|
||||
},
|
||||
_avg: {
|
||||
prayerCount: true
|
||||
},
|
||||
where: {
|
||||
createdAt: {
|
||||
gte: startDate
|
||||
},
|
||||
isActive: true
|
||||
}
|
||||
});
|
||||
|
||||
// Language distribution for conversations
|
||||
const languageDistribution = await prisma.chatConversation.groupBy({
|
||||
by: ['language'],
|
||||
_count: {
|
||||
language: true
|
||||
},
|
||||
where: {
|
||||
createdAt: {
|
||||
gte: startDate
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Content creation vs engagement ratio
|
||||
const contentMetrics = {
|
||||
totalPrayerRequests: await prisma.prayerRequest.count({
|
||||
where: {
|
||||
createdAt: { gte: startDate }
|
||||
}
|
||||
}),
|
||||
totalPrayers: await prisma.prayer.count({
|
||||
where: {
|
||||
createdAt: { gte: startDate }
|
||||
}
|
||||
}),
|
||||
totalConversations: await prisma.chatConversation.count({
|
||||
where: {
|
||||
createdAt: { gte: startDate }
|
||||
}
|
||||
}),
|
||||
totalMessages: await prisma.chatMessage.count({
|
||||
where: {
|
||||
timestamp: { gte: startDate }
|
||||
}
|
||||
}),
|
||||
totalBookmarks: await prisma.bookmark.count({
|
||||
where: {
|
||||
createdAt: { gte: startDate }
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
// Average engagement rates
|
||||
const avgPrayersPerRequest = contentMetrics.totalPrayerRequests > 0
|
||||
? contentMetrics.totalPrayers / contentMetrics.totalPrayerRequests
|
||||
: 0;
|
||||
|
||||
const avgMessagesPerConversation = contentMetrics.totalConversations > 0
|
||||
? contentMetrics.totalMessages / contentMetrics.totalConversations
|
||||
: 0;
|
||||
|
||||
// Content quality metrics (based on engagement)
|
||||
const highEngagementRequests = prayerRequestEngagement.filter(req => req.prayerCount >= 5).length;
|
||||
const lowEngagementRequests = prayerRequestEngagement.filter(req => req.prayerCount <= 1).length;
|
||||
|
||||
const engagementDistribution = {
|
||||
high: highEngagementRequests,
|
||||
medium: prayerRequestEngagement.length - highEngagementRequests - lowEngagementRequests,
|
||||
low: lowEngagementRequests
|
||||
};
|
||||
|
||||
return NextResponse.json({
|
||||
period: periodDays,
|
||||
engagement: {
|
||||
prayerRequests: prayerRequestEngagement.slice(0, 20),
|
||||
conversations: chatEngagement.slice(0, 20),
|
||||
bookmarkedVerses: verseDetails.slice(0, 15)
|
||||
},
|
||||
timeline: {
|
||||
prayers: prayerEngagementTimeline
|
||||
},
|
||||
metrics: {
|
||||
...contentMetrics,
|
||||
avgPrayersPerRequest: Math.round(avgPrayersPerRequest * 100) / 100,
|
||||
avgMessagesPerConversation: Math.round(avgMessagesPerConversation * 100) / 100
|
||||
},
|
||||
distributions: {
|
||||
categories: categoryPerformance,
|
||||
languages: languageDistribution,
|
||||
engagement: engagementDistribution
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Admin content analytics error:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Server error' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user