All files / src/app/api/testimonials route.ts

100% Statements 50/50
85.71% Branches 6/7
100% Functions 1/1
100% Lines 50/50

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 511x 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 9x 9x 8x 8x 9x 9x 9x 8x 8x 8x 8x 8x 8x 9x 1x 1x 1x 1x 1x 1x 9x  
export const dynamic = "force-dynamic";
 
import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { logger } from "@/lib/logging";
import { cachedJsonResponse, CACHE_PRESETS } from "@/lib/core/http-cache";
import { getOrSet, CACHE_TTL } from "@/lib/core";
 
 
/**
 * GET /api/testimonials
 * Returns active testimonials for public display
 */
export async function GET(request: Request) {
  try {
    const { searchParams } = new URL(request.url);
    const limit = Math.min(parseInt(searchParams.get("limit") || "10"), 50);
 
    // Use cached data
    const formattedTestimonials = await getOrSet(
      `testimonials:limit:${limit}`,
      async () => {
        const testimonials = await prisma.testimonial.findMany({
          where: { isActive: true },
          take: limit,
          orderBy: { createdAt: "desc" },
          select: {
            id: true,
            review: true,
            authorName: true,
            authorRole: true}});
 
        return testimonials;
      },
      CACHE_TTL.products
    );
 
    return cachedJsonResponse(
      request,
      { success: true, data: formattedTestimonials },
      CACHE_PRESETS.productList
    );
  } catch (error) {
    logger.error("Error fetching testimonials", error instanceof Error ? error : new Error(String(error)), { category: "API" });
    return NextResponse.json(
      { success: false, error: "Failed to fetch testimonials" },
      { status: 500 }
    );
  }
}