All files / varjoliitokauppa/app/api/admin/orders route.ts

75% Statements 9/12
62.5% Branches 5/8
100% Functions 1/1
81.81% Lines 9/11

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              1x 1x   1x 1x 1x 1x   1x                           1x   1x                      
import { NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
import { checkAdminAuth } from '@/lib/auth-check'
 
// GET /api/admin/orders - Get orders with optional pagination (Admin only)
// Query params: ?limit=50&offset=0 (default: no limit, all orders)
export async function GET(request: Request) {
  const authError = await checkAdminAuth()
  Iif (authError) return authError
 
  try {
    const { searchParams } = new URL(request.url)
    const limit = searchParams.get('limit') ? parseInt(searchParams.get('limit')!) : undefined
    const offset = searchParams.get('offset') ? parseInt(searchParams.get('offset')!) : 0
 
    const orders = await prisma.order.findMany({
      include: {
        items: {
          include: {
            product: true,
          },
        },
      },
      orderBy: { createdAt: 'desc' },
      take: limit, // undefined = no limit (backwards compatible)
      skip: offset,
    })
 
    // Also return total count for pagination UI
    const total = await prisma.order.count()
 
    return NextResponse.json({
      orders,
      total,
      limit: limit || null,
      offset,
    })
  } catch (error) {
    console.error('Error fetching orders:', error)
    return NextResponse.json({ error: 'Failed to fetch orders' }, { status: 500 })
  }
}