All files / src/app/api/admin/users route.ts

100% Statements 83/83
100% Branches 10/10
100% Functions 1/1
100% Lines 83/83

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 841x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 3x 3x 20x 20x 3x 3x 3x 3x 3x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 17x 17x 17x 17x 17x 17x 17x 17x 17x 1x 1x  
export const dynamic = "force-dynamic";
 
import { NextRequest, NextResponse } from 'next/server';
import { } from "next-auth";
import { prisma } from "@/lib/prisma";
import { Prisma } from "@prisma/client";
import { } from "@/lib/core";
import {
  withAdmin,
  withErrorHandling,
  successResponse,
  ApiSuccessResponse,
  ApiErrorResponse } from "@/lib/api";
import { } from "@/lib/api/middleware";
 
/**
 * GET /api/admin/users
 * Get all users with filters for admin management
 * Query params: page, limit, role, search, sortBy, sortOrder
 */
async function handleGet(request: NextRequest): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
  const searchParams = request.nextUrl.searchParams;
  const page = searchParams.get("page") ? parseInt(searchParams.get("page")!) : 1;
  const limit = searchParams.get("limit") ? parseInt(searchParams.get("limit")!) : 20;
  const role = searchParams.get("role");
  const search = searchParams.get("search");
  const sortBy = searchParams.get("sortBy") || "createdAt";
  const sortOrder = searchParams.get("sortOrder") || "desc";
 
  const skip = (page - 1) * limit;
 
  const where: Prisma.UserWhereInput = {};
 
  if (role) {
    where.role = role as "CUSTOMER" | "ADMIN";
  }
 
  if (search) {
    where.OR = [
      { email: { contains: search } },
      { name: { contains: search } },
    ];
  }
 
  const orderBy: Prisma.UserOrderByWithRelationInput = {
    [sortBy]: sortOrder };
 
  const [users, total] = await Promise.all([
    prisma.user.findMany({
      where,
      select: {
        id: true,
        email: true,
        name: true,
        phone: true,
        role: true,
        createdAt: true,
        updatedAt: true,
        emailVerified: true,
        image: true,
        failedLoginAttempts: true,
        lockedUntil: true,
        _count: {
          select: {
            orders: true,
            reviews: true,
            addresses: true } } },
      skip,
      take: limit,
      orderBy }),
    prisma.user.count({ where }),
  ]);
 
  return successResponse({
    data: users,
    pagination: {
      page,
      limit,
      total,
      pages: Math.ceil(total / limit) } });
}
 
export const GET = withErrorHandling(withAdmin(handleGet));