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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | export const dynamic = "force-dynamic"; /** * Dev Labels API * GET /api/dev/labels - List all labels * POST /api/dev/labels - Create a new label * DELETE /api/dev/labels?id=xxx - Delete a label */ import { NextRequest, NextResponse } from 'next/server'; import { Session } from "next-auth"; import { withAdmin, withErrorHandling, successResponse, createdResponse, ApiError, ApiSuccessResponse, ApiErrorResponse } from "@/lib/api"; import { AuthenticatedUser } from "@/lib/api/middleware"; import { prisma } from '@/lib/prisma'; import { CreateDevLabelSchema } from '@/lib/validation/dev-ticket-schemas'; import { logger } from '@/lib/logging'; async function handleGet( request: NextRequest ): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const { searchParams } = new URL(request.url); const search = searchParams.get('search'); const where: Record<string, unknown> = {}; if (search) { where.OR = [ { name: { contains: search } }, { description: { contains: search } }, ]; } const labels = await prisma.devTicketLabel.findMany({ where, include: { _count: { select: { tickets: true } } }, orderBy: { name: 'asc' } }); return successResponse(labels); } async function handlePost( request: NextRequest, context: unknown, session: Session, user: AuthenticatedUser ): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const body = await request.json(); const validationResult = CreateDevLabelSchema.safeParse(body); if (!validationResult.success) { throw ApiError.validation("Validation failed", validationResult.error.flatten().fieldErrors); } const data = validationResult.data; // Check if label name is unique const existingLabel = await prisma.devTicketLabel.findUnique({ where: { name: data.name } }); if (existingLabel) { throw ApiError.badRequest(`Label "${data.name}" already exists`); } // Create label const label = await prisma.devTicketLabel.create({ data: { name: data.name, color: data.color, description: data.description }, include: { _count: { select: { tickets: true } } } }); logger.info(`Created label "${data.name}"`, { category: 'DEV_LABELS', labelId: label.id, userId: user.id }); return createdResponse(label); } async function handleDelete( request: NextRequest, context: unknown, session: Session, user: AuthenticatedUser ): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const { searchParams } = new URL(request.url); const id = searchParams.get('id'); if (!id) { throw ApiError.badRequest('Label ID is required'); } // Check if label exists const label = await prisma.devTicketLabel.findUnique({ where: { id }, include: { _count: { select: { tickets: true } } } }); if (!label) { throw ApiError.notFound('Label not found'); } // Delete label (Prisma will automatically disconnect from tickets) await prisma.devTicketLabel.delete({ where: { id } }); logger.info(`Deleted label "${label.name}"`, { category: 'DEV_LABELS', labelId: id, userId: user.id, ticketsAffected: label._count.tickets }); return successResponse({ message: 'Label deleted successfully', ticketsAffected: label._count.tickets }); } export const GET = withErrorHandling(withAdmin(handleGet)); export const POST = withErrorHandling(withAdmin(handlePost)); export const DELETE = withErrorHandling(withAdmin(handleDelete)); |