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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 8x 10x 6x 6x 6x 3x 3x 6x 6x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 6x 6x 3x 3x 3x 6x 6x 3x 3x 3x 6x 3x 3x 6x 6x 6x 6x 6x 6x 6x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x | export const dynamic = "force-dynamic";
import { NextRequest, NextResponse } from 'next/server';
import { } from "next-auth";
import { getAnalyticsSummary } from "@/lib/analytics";
import { } from "@/lib/core";
import {
withAdmin,
withErrorHandling,
successResponse,
ApiSuccessResponse,
ApiErrorResponse } from "@/lib/api";
import { } from "@/lib/api/middleware";
/**
* GET /api/admin/reports
* Generate analytics reports
* Query params: format (csv|json), type (summary|orders|customers|products|sales)
*/
async function handleGet(request: NextRequest): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
const searchParams = request.nextUrl.searchParams;
const format = searchParams.get("format") || "json";
const type = searchParams.get("type") || "summary";
const analytics = await getAnalyticsSummary();
if (format === "csv") {
let csv = "";
if (type === "summary" || type === "orders") {
csv += generateOrdersCSV(analytics.orders);
}
if (type === "summary" || type === "customers") {
csv += "\n\nCUSTOMERS\n";
csv += "Total Customers,New Customers,Returning Customers,Avg LTV,Retention Rate\n";
const c = analytics.customers;
csv += `${c.totalCustomers},${c.newCustomers},${c.returningCustomers},${c.averageLifetimeValue},${c.customerRetentionRate}%\n`;
csv += "\nTop Customers\n";
csv += "Name,Email,Orders,Total Spent\n";
c.topCustomers.forEach((customer) => {
csv += `"${customer.name}","${customer.email}",${customer.orderCount},$${customer.totalSpent}\n`;
});
csv += "\nCustomer Segments\n";
csv += "VIP,Loyal,Occasional,One-Time\n";
csv += `${c.customerSegments.vip},${c.customerSegments.loyal},${c.customerSegments.occasional},${c.customerSegments.oneTime}\n`;
}
if (type === "summary" || type === "products") {
csv += "\n\nPRODUCTS\n";
csv += generateProductsCSV(analytics.products);
}
if (type === "summary" || type === "sales") {
csv += "\n\nDAILY SALES\n";
csv += "Date,Orders,Revenue\n";
analytics.sales.dailySales.forEach((day) => {
csv += `${day.date},${day.orders},$${day.revenue}\n`;
});
}
return new NextResponse(csv, {
status: 200,
headers: {
"Content-Type": "text/csv",
"Content-Disposition": `attachment; filename="analytics-${type}-${new Date().toISOString().split("T")[0]}.csv"` } }) as NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>;
}
// Default JSON format
return successResponse({
type,
generatedAt: new Date().toISOString(),
data: analytics });
}
export const GET = withErrorHandling(withAdmin(handleGet));
interface OrdersReport {
total: number;
processing: number;
shipped: number;
delivered: number;
cancelled: number;
averageOrderValue: number;
totalRevenue: number;
}
interface BestSellerProduct {
title: string;
sold: number;
revenue: number;
rating: number;
}
interface TopRatedProduct {
title: string;
rating: number;
reviewCount: number;
}
interface LowStockProduct {
title: string;
stock: number;
reorderLevel: number;
}
interface CategoryPerformance {
category: string;
products: number;
totalSales: number;
revenue: number;
}
interface ProductsReport {
bestSellers: BestSellerProduct[];
topRated: TopRatedProduct[];
lowStock: LowStockProduct[];
categoryPerformance: CategoryPerformance[];
}
/**
* Generate orders CSV content
*/
function generateOrdersCSV(orders: OrdersReport): string {
let csv = "ORDERS\n";
csv += "Total,Processing,Shipped,Delivered,Cancelled,Avg Order Value,Total Revenue\n";
csv += `${orders.total},${orders.processing},${orders.shipped},${orders.delivered},${orders.cancelled},$${orders.averageOrderValue},$${orders.totalRevenue}\n`;
return csv;
}
/**
* Generate products CSV content
*/
function generateProductsCSV(products: ProductsReport): string {
let csv = "Best Sellers\n";
csv += "Product,Sold,Revenue,Rating\n";
products.bestSellers.forEach((product) => {
csv += `"${product.title}",${product.sold},$${product.revenue},${product.rating}\n`;
});
csv += "\nTop Rated\n";
csv += "Product,Rating,Reviews\n";
products.topRated.forEach((product) => {
csv += `"${product.title}",${product.rating},${product.reviewCount}\n`;
});
csv += "\nLow Stock\n";
csv += "Product,Current Stock,Reorder Level\n";
products.lowStock.forEach((product) => {
csv += `"${product.title}",${product.stock},${product.reorderLevel}\n`;
});
csv += "\nCategory Performance\n";
csv += "Category,Products,Total Sales,Revenue\n";
products.categoryPerformance.forEach((category) => {
csv += `"${category.category}",${category.products},${category.totalSales},$${category.revenue}\n`;
});
return csv;
}
|