From db419288afd612135b3fc20938f476d1fa7c1f63 Mon Sep 17 00:00:00 2001 From: eff-kay Date: Fri, 5 Sep 2025 13:29:05 -0400 Subject: [PATCH 1/3] groups --- app/api/groups/[id]/route.ts | 27 +++++++++++++++++++++++ app/api/groups/stats/routs.ts | 40 +++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 app/api/groups/[id]/route.ts create mode 100644 app/api/groups/stats/routs.ts diff --git a/app/api/groups/[id]/route.ts b/app/api/groups/[id]/route.ts new file mode 100644 index 0000000..ea23275 --- /dev/null +++ b/app/api/groups/[id]/route.ts @@ -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 }); + } +} \ No newline at end of file diff --git a/app/api/groups/stats/routs.ts b/app/api/groups/stats/routs.ts new file mode 100644 index 0000000..1d101cd --- /dev/null +++ b/app/api/groups/stats/routs.ts @@ -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 }) + } +} \ No newline at end of file From bbf00760da578e09bdbb981242ea5f37b9e82572 Mon Sep 17 00:00:00 2001 From: eff-kay Date: Fri, 5 Sep 2025 13:35:13 -0400 Subject: [PATCH 2/3] add teams --- app/api/teams/[id]/route.ts | 27 ++++++++++++++++++++++++ app/api/teams/stats/routs.ts | 40 ++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 app/api/teams/[id]/route.ts create mode 100644 app/api/teams/stats/routs.ts diff --git a/app/api/teams/[id]/route.ts b/app/api/teams/[id]/route.ts new file mode 100644 index 0000000..ea23275 --- /dev/null +++ b/app/api/teams/[id]/route.ts @@ -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 }); + } +} \ No newline at end of file diff --git a/app/api/teams/stats/routs.ts b/app/api/teams/stats/routs.ts new file mode 100644 index 0000000..1d101cd --- /dev/null +++ b/app/api/teams/stats/routs.ts @@ -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 }) + } +} \ No newline at end of file From bbe5719bf45d44c2df9532cc47dfb19f1a215c62 Mon Sep 17 00:00:00 2001 From: "docsalot-app[bot]" <207601912+docsalot-app[bot]@users.noreply.github.com> Date: Fri, 5 Sep 2025 17:35:51 +0000 Subject: [PATCH 3/3] docs: create api-users.mdx for changes #1757093722436 --- docs/api-users.mdx | 94 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 docs/api-users.mdx diff --git a/docs/api-users.mdx b/docs/api-users.mdx new file mode 100644 index 0000000..5135a44 --- /dev/null +++ b/docs/api-users.mdx @@ -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). +``` \ No newline at end of file