fitaiProto/apps/admin/src/app/api/users/route.ts
2025-11-07 23:46:46 +01:00

32 lines
915 B
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { getDatabase } from '../../../lib/database/index'
export async function GET(request: NextRequest) {
try {
const db = await getDatabase()
const { searchParams } = new URL(request.url)
const role = searchParams.get('role')
let users = await db.getAllUsers()
if (role) {
users = users.filter(user => user.role === role)
}
const usersWithClients = await Promise.all(
users.map(async (user) => {
const { password: _, ...userWithoutPassword } = user
const client = await db.getClientByUserId(user.id)
return { ...userWithoutPassword, client }
})
)
return NextResponse.json({ users: usersWithClients })
} catch (error) {
console.error('Get users error:', error)
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
)
}
}