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

100% Statements 65/65
100% Branches 18/18
100% Functions 1/1
100% Lines 65/65

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 661x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 5x 5x 5x 5x 3x 7x 9x 3x 3x 7x 9x 3x 3x 7x 9x 3x 3x 7x 9x 3x 3x 7x 7x 7x 1x 1x  
export const dynamic = "force-dynamic";
 
import { NextRequest, NextResponse } from 'next/server';
import { } from "next-auth";
import {
  getDashboardOverview,
  getOrderMetrics,
  getSalesAnalytics,
  getProductAnalytics,
  getCustomerAnalytics } from "@/lib/analytics";
import { } from "@/lib/core";
import {
  withAdmin,
  withErrorHandling,
  successResponse,
  ApiSuccessResponse,
  ApiErrorResponse } from "@/lib/api";
import { } from "@/lib/api/middleware";
 
/**
 * GET /api/admin/dashboard
 * Get admin dashboard data
 * Query params: section (overview|orders|sales|products|customers|all)
 */
async function handleGet(request: NextRequest): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
  const searchParams = request.nextUrl.searchParams;
  const section = searchParams.get("section") || "all";
 
  interface DashboardData {
    overview?: Awaited<ReturnType<typeof getDashboardOverview>>;
    orders?: Awaited<ReturnType<typeof getOrderMetrics>>;
    sales?: Awaited<ReturnType<typeof getSalesAnalytics>>;
    products?: Awaited<ReturnType<typeof getProductAnalytics>>;
    customers?: Awaited<ReturnType<typeof getCustomerAnalytics>>;
  }
 
  const data: DashboardData = {};
 
  if (section === "all" || section === "overview") {
    const dateRange = {
      startDate: new Date(new Date().setDate(new Date().getDate() - 30)),
      endDate: new Date() };
    data.overview = await getDashboardOverview(dateRange);
  }
 
  if (section === "all" || section === "orders") {
    data.orders = await getOrderMetrics();
  }
 
  if (section === "all" || section === "sales") {
    data.sales = await getSalesAnalytics(30);
  }
 
  if (section === "all" || section === "products") {
    data.products = await getProductAnalytics();
  }
 
  if (section === "all" || section === "customers") {
    data.customers = await getCustomerAnalytics();
  }
 
  return successResponse(data);
}
 
export const GET = withErrorHandling(withAdmin(handleGet));