( request: NextRequest )
| 31 | import { captureException } from '@sentry/nextjs'; |
| 32 | |
| 33 | export async function GET( |
| 34 | request: NextRequest |
| 35 | ): Promise<NextResponse<{ error: string } | UsersApiResponse>> { |
| 36 | // Parse query parameters outside try block for error reporting |
| 37 | const { searchParams } = new URL(request.url); |
| 38 | const page = Math.max(1, parseInt(searchParams.get('page') || '1')); |
| 39 | const limit = Math.min(100, Math.max(1, parseInt(searchParams.get('limit') || '25'))); |
| 40 | const sortBy = (searchParams.get('sortBy') || 'created_at') as SortableField; |
| 41 | const sortOrder = searchParams.get('sortOrder') === 'asc' ? 'asc' : 'desc'; |
| 42 | const searchTerm = searchParams.get('search')?.trim() || ''; |
| 43 | const notesSearchTerm = searchParams.get('notesSearch')?.trim() || ''; |
| 44 | const blockedStatus = searchParams.get('blockedStatus'); |
| 45 | const orgMembership = searchParams.get('orgMembership'); |
| 46 | const paymentStatus = searchParams.get('paymentStatus'); |
| 47 | const autoTopUp = searchParams.get('autoTopUp'); |
| 48 | |
| 49 | try { |
| 50 | // Check authentication and admin status |
| 51 | const { authFailedResponse } = await getUserFromAuth({ adminOnly: true }); |
| 52 | if (authFailedResponse) { |
| 53 | return authFailedResponse; |
| 54 | } |
| 55 | |
| 56 | const hasValidationStytch = searchParams.get('hasValidationStytch'); |
| 57 | const hasValidationNovelCard = searchParams.get('hasValidationNovelCard'); |
| 58 | |
| 59 | // Validate sortBy field |
| 60 | const sortField = sortableFields.includes(sortBy) ? sortBy : 'created_at'; |
| 61 | |
| 62 | // Build where conditions |
| 63 | const conditions = []; |
| 64 | |
| 65 | // User-specific search condition |
| 66 | if (searchTerm) { |
| 67 | const referralCodeOwnerId = ( |
| 68 | await db |
| 69 | .select({ kilo_user_id: referral_codes.kilo_user_id }) |
| 70 | .from(referral_codes) |
| 71 | .where(eq(referral_codes.code, searchTerm)) |
| 72 | .limit(1) |
| 73 | )[0]?.kilo_user_id; |
| 74 | |
| 75 | conditions.push( |
| 76 | or( |
| 77 | ilike(kilocode_users.google_user_email, `%${searchTerm}%`), |
| 78 | ilike(kilocode_users.google_user_name, `%${searchTerm}%`), |
| 79 | eq(kilocode_users.id, searchTerm), |
| 80 | eq(kilocode_users.stripe_customer_id, searchTerm), |
| 81 | eq(kilocode_users.openrouter_upstream_safety_identifier, searchTerm), |
| 82 | eq(kilocode_users.openrouter_downstream_safety_identifier, searchTerm), |
| 83 | eq(kilocode_users.vercel_downstream_safety_identifier, searchTerm), |
| 84 | ...(referralCodeOwnerId ? [eq(kilocode_users.id, referralCodeOwnerId)] : []) |
| 85 | ) |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | // Notes and blocked reason search condition |
| 90 | if (notesSearchTerm) { |
nothing calls this directly
no test coverage detected