(req: NextRequest, span: Span)
| 71 | ) |
| 72 | |
| 73 | async function updateCostInner(req: NextRequest, span: Span): Promise<NextResponse> { |
| 74 | const requestId = generateRequestId() |
| 75 | const startTime = Date.now() |
| 76 | |
| 77 | try { |
| 78 | logger.info(`[${requestId}] Update cost request started`) |
| 79 | |
| 80 | if (!isBillingEnabled) { |
| 81 | span.setAttribute(TraceAttr.BillingOutcome, BillingRouteOutcome.BillingDisabled) |
| 82 | span.setAttribute(TraceAttr.HttpStatusCode, 200) |
| 83 | return NextResponse.json({ |
| 84 | success: true, |
| 85 | message: 'Billing disabled, cost update skipped', |
| 86 | data: { |
| 87 | billingEnabled: false, |
| 88 | processedAt: new Date().toISOString(), |
| 89 | requestId, |
| 90 | }, |
| 91 | }) |
| 92 | } |
| 93 | |
| 94 | // Check authentication (internal API key) |
| 95 | const authResult = checkInternalApiKey(req) |
| 96 | if (!authResult.success) { |
| 97 | logger.warn(`[${requestId}] Authentication failed: ${authResult.error}`) |
| 98 | span.setAttribute(TraceAttr.BillingOutcome, BillingRouteOutcome.AuthFailed) |
| 99 | span.setAttribute(TraceAttr.HttpStatusCode, 401) |
| 100 | return NextResponse.json( |
| 101 | { |
| 102 | success: false, |
| 103 | error: authResult.error || 'Authentication failed', |
| 104 | }, |
| 105 | { status: 401 } |
| 106 | ) |
| 107 | } |
| 108 | |
| 109 | const parsed = await parseRequest( |
| 110 | billingUpdateCostContract, |
| 111 | req, |
| 112 | {}, |
| 113 | { |
| 114 | validationErrorResponse: (error) => { |
| 115 | logger.warn(`[${requestId}] Invalid request body`, { |
| 116 | errors: error.issues, |
| 117 | }) |
| 118 | span.setAttribute(TraceAttr.BillingOutcome, BillingRouteOutcome.InvalidBody) |
| 119 | span.setAttribute(TraceAttr.HttpStatusCode, 400) |
| 120 | return NextResponse.json( |
| 121 | { |
| 122 | success: false, |
| 123 | error: 'Invalid request body', |
| 124 | details: error.issues, |
| 125 | }, |
| 126 | { status: 400 } |
| 127 | ) |
| 128 | }, |
| 129 | invalidJsonResponse: () => { |
| 130 | span.setAttribute(TraceAttr.BillingOutcome, BillingRouteOutcome.InvalidBody) |
no test coverage detected