Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
23 changes: 23 additions & 0 deletions app/admin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,29 @@ type AdminUser = {
};
}

async function getGroups(): Promise<AdminGroup[]> {
const groups = await prisma.group.findMany({
select: {
id: true,
name: true,
email: true,
image: true,
createdAt: true,
lastLoginAt: true,
loginCount: true,
_count: {
select: {
sessions: true
}
}
},
orderBy: {
createdAt: 'desc'
}
})
return groups
}

async function getUsers(): Promise<AdminUser[]> {
const users = await prisma.user.findMany({
select: {
Expand Down
27 changes: 27 additions & 0 deletions app/api/groups/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { NextResponse } from 'next/server';
import { prisma } from '../../../../lib/prisma';

export async function GET(
request: Request,
{ params }: { params: { id: string } }
) {
try {
const { id } = params;
const group = await prisma.group.findUnique({
where: { id },
include: {
posts: true, // Include posts by the group
accounts: true, // Include group accounts
},
});

if (!group) {
return NextResponse.json({ message: 'group not found' }, { status: 404 });
}

return NextResponse.json(group);
} catch (error) {
console.error('Error fetching group:', error);
return NextResponse.json({ message: 'Error fetching group' }, { status: 500 });
}
}
40 changes: 40 additions & 0 deletions app/api/groups/stats/routs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
import { getServerSession } from 'next-auth'

export async function GET() {
const session = await getServerSession()

if (!session) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}

try {
const totalgroups = await prisma.group.count()
const activegroups = await prisma.session.count({
where: {
expires: {
gt: new Date()
}
}
})

const recentgroups = await prisma.group.count({
where: {
createdAt: {
gte: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000) // Last 7 days
}
}
})

return NextResponse.json({
totalgroups,
activegroups,
recentgroups,
timestamp: new Date().toISOString()
})
} catch (error) {
console.error('Stats error:', error)
return NextResponse.json({ error: 'Failed to fetch stats' }, { status: 500 })
}
}
27 changes: 27 additions & 0 deletions app/api/organizations/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { NextResponse } from 'next/server';
import { prisma } from '../../../../lib/prisma';

export async function GET(
request: Request,
{ params }: { params: { id: string } }
) {
try {
const { id } = params;
const organizations = await prisma.organizations.findUnique({
where: { id },
include: {
posts: true, // Include posts by the organizations
accounts: true, // Include organizations accounts
},
});

if (!organizations) {
return NextResponse.json({ message: 'organizations not found' }, { status: 404 });
}

return NextResponse.json(organizations);
} catch (error) {
console.error('Error fetching organizations:', error);
return NextResponse.json({ message: 'Error fetching organizations' }, { status: 500 });
}
}
40 changes: 40 additions & 0 deletions app/api/organizations/stats/routs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
import { getServerSession } from 'next-auth'

export async function GET() {
const session = await getServerSession()

if (!session) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}

try {
const totalgroups = await prisma.group.count()
const activegroups = await prisma.session.count({
where: {
expires: {
gt: new Date()
}
}
})

const recentgroups = await prisma.group.count({
where: {
createdAt: {
gte: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000) // Last 7 days
}
}
})

return NextResponse.json({
totalgroups,
activegroups,
recentgroups,
timestamp: new Date().toISOString()
})
} catch (error) {
console.error('Stats error:', error)
return NextResponse.json({ error: 'Failed to fetch stats' }, { status: 500 })
}
}
4 changes: 3 additions & 1 deletion docs/api-introduction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: 'Overview of the Chatbot API endpoints'

# API Documentation

The Chatbot API provides a set of endpoints for managing chat functionality, user authentication, and administrative tasks.
The Chatbot API provides a set of endpoints for managing chat functionality, user authentication, organizations, and administrative tasks.

## Base URL

Expand All @@ -29,5 +29,7 @@ The API is organized into the following categories:
- `/api/chat/*` - Chat functionality
- `/api/users/*` - User management
- `/api/users/stats/*` - User statistics
- `/api/organizations/*` - Organization management endpoints
- `/api/organizations/stats/*` - Organization statistics

For detailed information about specific endpoints, see their respective documentation pages.
4 changes: 3 additions & 1 deletion docs/introduction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This is a Next.js-based chatbot application that provides real-time chat functio
- User authentication
- Admin dashboard
- User statistics
- Organization endpoints & statistics
- Responsive design
- Next.js 13+ App Router
- TypeScript support
Expand All @@ -27,7 +28,8 @@ app/
├── api/ # API routes
│ ├── auth/ # Authentication endpoints
│ ├── chat/ # Chat endpoints
│ └── users/ # User management endpoints
│ ├── users/ # User management endpoints
│ └── organizations/ # Organization endpoints (details & stats)
├── components/ # Reusable components
└── settings/ # User settings
```
Expand Down