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
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 user = await prisma.user.findUnique({
where: { id },
include: {
posts: true, // Include posts by the user
accounts: true, // Include user accounts
},
});

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

return NextResponse.json(user);
} catch (error) {
console.error('Error fetching user:', error);
return NextResponse.json({ message: 'Error fetching user' }, { 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 totalUsers = await prisma.user.count()
const activeUsers = await prisma.session.count({
where: {
expires: {
gt: new Date()
}
}
})

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

return NextResponse.json({
totalUsers,
activeUsers,
recentUsers,
timestamp: new Date().toISOString()
})
} catch (error) {
console.error('Stats error:', error)
return NextResponse.json({ error: 'Failed to fetch stats' }, { status: 500 })
}
}
94 changes: 94 additions & 0 deletions docs/api-users.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Users API

This page documents the API endpoints related to user operations in the Chatbot application.

---

## Get Individual User

Fetch a user's details by their ID, including their posts and account information.

**Endpoint:**
```
GET /api/groups/[id]
```

**Path Parameters:**
| Name | Type | Description |
|------|--------|----------------------|
| id | string | The user's unique ID |

**Response:**
- `200 OK` with user data, including:
- `posts`: List of posts by the user
- `accounts`: Linked account details

**Example Response:**
```json
{
"id": "clv6u6tuv1234567890abcdef",
"name": "Jane Doe",
"email": "jane@example.com",
"posts": [
{
"id": "post1",
"title": "First Post",
"content": "..."
}
],
"accounts": [
{
"provider": "google",
"providerAccountId": "1122334455"
}
],
// ...other user fields
}
```

**Error Responses:**
- `404 Not Found`: User does not exist
- `500 Internal Server Error`: An error occurred fetching the user


---

## Get User Statistics (Admin Only)

Retrieve overall user statistics. Requires authentication.

**Endpoint:**
```
GET /api/groups/stats
```

**Authentication:** User must be logged in (session required).

**Response:**
- `200 OK` with statistics:
- `totalUsers`: Count of all registered users
- `activeUsers`: Number of users with active sessions (not expired)
- `recentUsers`: Number of users registered in the last 7 days
- `timestamp`: Timestamp of when stats were generated

**Example Response:**
```json
{
"totalUsers": 1054,
"activeUsers": 243,
"recentUsers": 17,
"timestamp": "2024-06-13T12:34:56.789Z"
}
```

**Error Responses:**
- `401 Unauthorized`: User is not authenticated
- `500 Internal Server Error`: Failed to fetch stats

---

## Notes

- All endpoints return data in JSON format.
- For protected routes, ensure to include proper authentication (session/token).
- For additional user-related endpoints, consult the rest of the documentation or contact [Support](mailto:support@example.com).