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 })
}
}
118 changes: 118 additions & 0 deletions docs/api-users.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
```markdown
# Users API

This page documents the endpoints related to user management for the Chatbot API.

---

## Fetch a User

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

Fetch information about a specific user by their ID, including their posts and accounts.

### Request

- Method: `GET`
- URL parameter: `id` — The user's unique identifier.

**Example:**
```
GET /api/groups/123
```

### Response

- `200 OK` with JSON:
```json
{
"id": "123",
"name": "Jane Doe",
"email": "jane@example.com",
"posts": [
{
"id": "abc",
"title": "First Post",
"content": "..."
}
// ... more posts
],
"accounts": [
{
"provider": "google",
"providerAccountId": "xxx"
}
// ... more accounts
],
// ...other user fields
}
```
- `404 Not Found`:
```json
{ "message": "User not found" }
```
- `500 Internal Server Error`:
```json
{ "message": "Error fetching user" }
```

---

## Get User Statistics

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

Retrieves various user statistics. Requires authentication.

### Statistics Provided

- `totalUsers`: Total number of users.
- `activeUsers`: Users with an active session (not expired).
- `recentUsers`: Users registered in the last 7 days.
- `timestamp`: Time when the stats were generated (ISO 8601 string).

### Request

- Method: `GET`
- Authentication: Required (must have a valid session).

### Response

- `200 OK` with JSON:
```json
{
"totalUsers": 128,
"activeUsers": 45,
"recentUsers": 8,
"timestamp": "2024-06-12T13:42:11.615Z"
}
```
- `401 Unauthorized`:
```json
{ "error": "Unauthorized" }
```
- `500 Internal Server Error`:
```json
{ "error": "Failed to fetch stats" }
```

---

## Error Handling

Errors will be returned as JSON with an appropriate HTTP status code and a descriptive message.

---

## See Also

- [API Introduction](./api-introduction)
- [Authentication](./api-authentication)
- [Chat API](./api-chat)
```