All files / src/app/api/support/articles route.ts

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

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

/**
 * Support Articles API (Public)
 * GET /api/support/articles - List published articles
 */

import { NextRequest, NextResponse } from 'next/server';
import { prisma } from "@/lib/prisma";
import { ArticleFilterSchema } from "@/lib/validation/support-schemas";
import {
  withErrorHandling,
  paginatedResponse,
  ApiError,
  ApiSuccessResponse } from "@/lib/api";

/**
 * GET /api/support/articles
 * List published support articles (public)
 */
interface ArticleSummary {
  id: string;
  title: string;
  slug: string;
  summary: string | null;
  category: string;
  viewCount: number;
  helpfulCount: number;
  notHelpfulCount: number;
  publishedAt: Date | null;
  createdAt: Date;
}

async function handleGet(request: NextRequest): Promise<NextResponse<ApiSuccessResponse<ArticleSummary[]>>> {
  // Parse query parameters
  const { searchParams } = new URL(request.url);
  const queryParams = {
    category: searchParams.get("category") || undefined,
    search: searchParams.get("search") || undefined,
    page: searchParams.get("page") || "1",
    limit: searchParams.get("limit") || "20",
    isPublished: "true", // Always filter to published only for public API
  };

  const filterResult = ArticleFilterSchema.safeParse(queryParams);
  if (!filterResult.success) {
    throw ApiError.validation(
      "Invalid query parameters",
      filterResult.error.flatten().fieldErrors
    );
  }

  const filters = filterResult.data;

  // Build where clause
  const where: Record<string, unknown> = {
    isPublished: true};

  if (filters.category) {
    where.category = filters.category;
  }

  if (filters.search) {
    where.OR = [
      { title: { contains: filters.search } },
      { summary: { contains: filters.search } },
      { keywords: { contains: filters.search } },
    ];
  }

  // Get total count
  const total = await prisma.supportArticle.count({ where });

  // Get articles
  const articles = await prisma.supportArticle.findMany({
    where,
    select: {
      id: true,
      title: true,
      slug: true,
      summary: true,
      category: true,
      viewCount: true,
      helpfulCount: true,
      notHelpfulCount: true,
      publishedAt: true,
      createdAt: true},
    orderBy: [{ viewCount: "desc" }, { createdAt: "desc" }],
    skip: (filters.page - 1) * filters.limit,
    take: filters.limit});

  return paginatedResponse(articles, {
    page: filters.page,
    limit: filters.limit,
    total});
}

export const GET = withErrorHandling(handleGet);