All files / src/app/api/dev/tickets/epics route.ts

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

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

/**
 * Dev Ticket Epics API
 * GET /api/dev/tickets/epics - Get all epics (root-level tickets)
 */

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

async function handleGet(
  request: NextRequest
): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
  const { searchParams } = new URL(request.url);
  const projectId = searchParams.get('projectId');
  const includeProgress = searchParams.get('progress') === 'true';

  // Build where clause
  const where: { category: 'EPIC'; parentId: null; projectId?: string } = {
    category: 'EPIC',
    parentId: null };

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

  const epics = await prisma.devTicket.findMany({
    where,
    include: {
      reporter: {
        select: { id: true, name: true, email: true, image: true } },
      assignee: {
        select: { id: true, name: true, email: true, image: true } },
      project: {
        select: { id: true, name: true, key: true, color: true } },
      _count: {
        select: { children: true } } },
    orderBy: [{ status: 'asc' }, { createdAt: 'desc' }] });

  // Calculate progress if requested
  let epicsWithProgress = epics;
  if (includeProgress) {
    epicsWithProgress = await Promise.all(
      epics.map(async (epic) => {
        const progress =
          epic.childCount > 0
            ? Math.round((epic.completedChildCount / epic.childCount) * 100)
            : epic.status === 'COMPLETED'
              ? 100
              : 0;

        return {
          ...epic,
          progress };
      })
    );
  }

  return successResponse(epicsWithProgress);
}

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