All files / src/app/api/admin/loyalty/transactions route.ts

0% Statements 0/96
100% Branches 0/0
0% Functions 0/1
0% Lines 0/96

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                                                                                                                                                                                                 
export const dynamic = "force-dynamic";

import { NextRequest, NextResponse } from 'next/server';
import { } from "next-auth";
import { prisma } from "@/lib/prisma";
import {
  withAdmin,
  withErrorHandling,
  successResponse,
  ApiSuccessResponse,
  ApiErrorResponse } from "@/lib/api";
import { } from "@/lib/api/middleware";

/**
 * GET /api/admin/loyalty/transactions
 * List all transactions with filters
 */
async function handleGet(
  request: NextRequest
): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
  const { searchParams } = request.nextUrl;
  const page = parseInt(searchParams.get("page") || "1");
  const limit = parseInt(searchParams.get("limit") || "20");
  const userId = searchParams.get("userId");
  const type = searchParams.get("type");
  const dateFrom = searchParams.get("dateFrom");
  const dateTo = searchParams.get("dateTo");

  const skip = (page - 1) * limit;

  // Build where clause
  const where: {
    type?: string;
    customerLoyalty?: { userId: number };
    createdAt?: { gte?: Date; lte?: Date };
  } = {};

  if (type) {
    where.type = type;
  }

  if (userId) {
    where.customerLoyalty = { userId: parseInt(userId) };
  }

  if (dateFrom || dateTo) {
    where.createdAt = {};
    if (dateFrom) {
      where.createdAt.gte = new Date(dateFrom);
    }
    if (dateTo) {
      where.createdAt.lte = new Date(dateTo);
    }
  }

  const [transactions, total] = await Promise.all([
    prisma.loyaltyTransaction.findMany({
      where,
      skip,
      take: limit,
      include: {
        customerLoyalty: {
          include: {
            user: {
              select: {
                id: true,
                email: true,
                name: true } } } } },
      orderBy: { createdAt: "desc" } }),
    prisma.loyaltyTransaction.count({ where }),
  ]);

  const formattedTransactions = transactions.map((tx) => ({
    id: tx.id,
    userId: tx.customerLoyalty.userId,
    user: {
      id: tx.customerLoyalty.user.id,
      email: tx.customerLoyalty.user.email,
      name: tx.customerLoyalty.user.name || "N/A" },
    type: tx.type,
    points: tx.points,
    description: tx.description,
    orderId: tx.orderId,
    expiresAt: tx.expiresAt,
    createdAt: tx.createdAt }));

  return successResponse({
    transactions: formattedTransactions,
    pagination: {
      page,
      limit,
      total,
      totalPages: Math.ceil(total / limit) } });
}

export const GET = withErrorHandling(withAdmin(handleGet));