All files / src/app/api/admin/monitoring/errors/resolve-bulk route.ts

96.96% Statements 64/66
78.57% Branches 11/14
100% Functions 1/1
96.96% Lines 64/66

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 671x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 3x 3x 5x 5x 8x 1x 1x 4x 4x 8x 8x     4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 8x 8x 8x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x  
import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import { addSecurityHeaders } from '@/lib/security';
import {
  withAdmin,
  withErrorHandling,
  validationErrorResponse,
  type ApiSuccessResponse,
  type ApiErrorResponse,
  type AuthenticatedUser,
} from '@/lib/api';
import type { Session } from 'next-auth';
 
/**
 * POST /api/admin/monitoring/errors/resolve-bulk
 *
 * Bulk resolve multiple errors by their fingerprints.
 * Requires admin authentication.
 */
async function handlePost(
  request: NextRequest,
  _context: unknown,
  session: Session,
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  _user: AuthenticatedUser
): Promise<NextResponse<ApiSuccessResponse<{ resolved: number }> | ApiErrorResponse>> {
  const body = await request.json();
  const { fingerprints } = body as { fingerprints: string[] };
 
  if (!fingerprints || !Array.isArray(fingerprints) || fingerprints.length === 0) {
    return validationErrorResponse('fingerprints array is required');
  }
 
  // Limit bulk operations to 100 at a time
  if (fingerprints.length > 100) {
    return validationErrorResponse('Maximum 100 errors can be resolved at once');
  }
 
  // Get user ID from session
  const userId = session.user?.id;
  if (!userId) {
    return validationErrorResponse('User ID not found in session');
  }
 
  // Resolve all matching errors
  const result = await prisma.errorStatistic.updateMany({
    where: {
      fingerprint: { in: fingerprints },
      status: 'open',
    },
    data: {
      status: 'resolved',
      resolvedAt: new Date(),
      resolvedBy: typeof userId === 'string' ? parseInt(userId, 10) : userId,
    },
  });
 
  return addSecurityHeaders(
    NextResponse.json({
      success: true,
      data: { resolved: result.count },
    })
  ) as NextResponse<ApiSuccessResponse<{ resolved: number }>>;
}
 
export const POST = withErrorHandling(withAdmin(handlePost));