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 | import { NextRequest, NextResponse } from 'next/server'; import { Session } from "next-auth"; import { prisma } from "@/lib/prisma"; import { withAuth, withErrorHandling, paginatedResponse, ApiSuccessResponse, ApiErrorResponse, RouteContext } from "@/lib/api"; interface LoyaltyTransaction { id: number; type: string; points: number; description: string | null; orderId: number | null; expiresAt: Date | null; createdAt: Date; isPositive: boolean; } /** * GET /api/loyalty/history * Get user's loyalty points transaction history */ async function handleGet( request: NextRequest, context: RouteContext | undefined, session: Session ): Promise<NextResponse<ApiSuccessResponse<LoyaltyTransaction[]> | ApiErrorResponse>> { const userId = session.user.id; const searchParams = request.nextUrl.searchParams; const page = parseInt(searchParams.get("page") || "1"); const limit = parseInt(searchParams.get("limit") || "20"); const type = searchParams.get("type") || ""; // "earn", "redeem", "expire", "adjust", "bonus" const skip = (page - 1) * limit; // Get customer loyalty record const loyalty = await prisma.customerLoyalty.findUnique({ where: { userId } }); if (!loyalty) { return paginatedResponse([], { page, limit, total: 0 }); } const where: { customerLoyaltyId: number; type?: string; } = { customerLoyaltyId: loyalty.id }; if (type) { where.type = type; } const [transactions, total] = await Promise.all([ prisma.loyaltyTransaction.findMany({ where, orderBy: { createdAt: "desc" }, skip, take: limit }), prisma.loyaltyTransaction.count({ where }), ]); // Format transactions for display const formatted = transactions.map((tx) => ({ id: tx.id, type: tx.type, points: tx.points, description: tx.description, orderId: tx.orderId, expiresAt: tx.expiresAt, createdAt: tx.createdAt, isPositive: tx.points > 0 })); return paginatedResponse(formatted, { page, limit, total }); } export const GET = withErrorHandling(withAuth(handleGet)); |