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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | export const dynamic = "force-dynamic"; import { NextRequest, NextResponse } from 'next/server'; import { } from "next-auth"; import { prisma } from "@/lib/prisma"; import { logger } from "@/lib/logging"; import { withAdmin, withErrorHandling, successResponse, ApiSuccessResponse, ApiErrorResponse } from "@/lib/api"; import { } from "@/lib/api/middleware"; const LOG_CATEGORY = "ADMIN_PRICING_ANALYTICS_API"; /** * GET /api/admin/pricing/analytics * Get pricing rule analytics and statistics */ async function handleGet(request: NextRequest): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const { searchParams } = new URL(request.url); const period = searchParams.get("period") || "30d"; // Calculate date range let startDate: Date; const endDate = new Date(); switch (period) { case "7d": startDate = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000); break; case "30d": startDate = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000); break; case "90d": startDate = new Date(Date.now() - 90 * 24 * 60 * 60 * 1000); break; default: startDate = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000); } // Get overall stats const [ totalRules, activeRules, totalApplications, totalDiscounted, applicationsByType, topRules, recentApplications, ] = await Promise.all([ // Total rules prisma.pricingRule.count(), // Active rules prisma.pricingRule.count({ where: { isActive: true, OR: [ { startDate: null }, { startDate: { lte: new Date() } }, ], AND: [ { OR: [ { endDate: null }, { endDate: { gte: new Date() } }, ] }, ] } }), // Total applications in period prisma.priceApplication.count({ where: { appliedAt: { gte: startDate, lte: endDate } } }), // Total discounted amount in period prisma.priceApplication.aggregate({ _sum: { discountAmount: true }, where: { appliedAt: { gte: startDate, lte: endDate } } }), // Applications by rule type prisma.priceApplication.groupBy({ by: ["pricingRuleId"], _count: { id: true }, _sum: { discountAmount: true }, where: { appliedAt: { gte: startDate, lte: endDate } } }), // Top performing rules prisma.pricingRule.findMany({ where: { applications: { some: { appliedAt: { gte: startDate, lte: endDate } } } }, select: { id: true, name: true, type: true, applicationCount: true, totalDiscounted: true }, orderBy: { applicationCount: "desc" }, take: 10 }), // Recent applications prisma.priceApplication.findMany({ where: { appliedAt: { gte: startDate, lte: endDate } }, select: { id: true, productId: true, userId: true, originalPrice: true, discountedPrice: true, discountAmount: true, appliedAt: true, pricingRule: { select: { id: true, name: true, type: true } } }, orderBy: { appliedAt: "desc" }, take: 20 }), ]); // Get rule type distribution const rulesByType = await prisma.pricingRule.groupBy({ by: ["type"], _count: { id: true } }); // Calculate daily application trends const dailyApplications = await prisma.$queryRaw< { date: string; count: bigint; total_discount: number }[] >` SELECT DATE(applied_at) as date, COUNT(*) as count, SUM(discount_amount) as total_discount FROM price_applications WHERE applied_at >= ${startDate} AND applied_at <= ${endDate} GROUP BY DATE(applied_at) ORDER BY date ASC `; logger.info("Pricing analytics fetched", { category: LOG_CATEGORY, period, totalRules, activeRules }); return successResponse({ overview: { totalRules, activeRules, totalApplications, totalDiscounted: totalDiscounted._sum.discountAmount || 0, period }, rulesByType: rulesByType.map((r) => ({ type: r.type, count: r._count.id })), topRules: topRules.map((rule) => ({ id: rule.id, name: rule.name, type: rule.type, applicationCount: rule.applicationCount, totalDiscounted: rule.totalDiscounted })), applicationsByRule: applicationsByType.map((a) => ({ ruleId: a.pricingRuleId, applications: a._count.id, totalDiscounted: a._sum.discountAmount || 0 })), dailyTrend: dailyApplications.map((d) => ({ date: d.date, applications: Number(d.count), totalDiscounted: d.total_discount || 0 })), recentApplications: recentApplications.map((a) => ({ id: a.id, productId: a.productId, userId: a.userId, originalPrice: a.originalPrice, discountedPrice: a.discountedPrice, discountAmount: a.discountAmount, appliedAt: a.appliedAt, rule: a.pricingRule })) }); } export const GET = withErrorHandling(withAdmin(handleGet)); |