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 })
}
}
27 changes: 27 additions & 0 deletions app/api/teams/[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/teams/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 @@
```markdown
# Users API

This page documents the endpoints for managing and retrieving user information in the Chatbot API.

---

## Get User by ID

Retrieves a user by their ID, including related posts and linked accounts.

### Endpoint

```
GET /api/teams/[id]
```

### Parameters

| Name | In | Type | Required | Description |
|-------|--------|--------|----------|-----------------------|
| id | Path | string | Yes | The user's unique ID. |

### Response

- **200 OK**: Returns the user object, including the user's posts and accounts.
- **404 Not Found**: User does not exist.
- **500 Internal Server Error**: An error occurred on the server.

#### Example Response

```json
{
"id": "clu12345",
"name": "Jane Doe",
"email": "jane@example.com",
"posts": [
{
"id": "post1",
"title": "Hello World"
}
// ...
],
"accounts": [
{
"provider": "github",
"providerAccountId": "1234567"
}
// ...
]
}
```

---

## User Statistics

Provides aggregate statistics about users.

### Endpoint

```
GET /api/teams/stats
```

> **Note**: Requires authentication. Unauthenticated requests will receive a `401 Unauthorized` response.

### Response

- **200 OK**: Returns statistics about users.
- **401 Unauthorized**: Authentication is required.
- **500 Internal Server Error**: An error occurred on the server.

#### Example Response

```json
{
"totalUsers": 1234,
"activeUsers": 78,
"recentUsers": 56,
"timestamp": "2024-06-11T12:34:56.789Z"
}
```

**Statistics:**
- `totalUsers`: Total number of users.
- `activeUsers`: Users with currently active sessions.
- `recentUsers`: Number of users created in the last 7 days.
- `timestamp`: When this data was generated.

---

If you have questions or need support, [contact us](mailto:support@example.com).
```