Overview
Implement a `POST` handler that cancels a pending invoice.
⚠️ Scope: Write your code only inside `app/api/routes-b/invoices/[id]/cancel/route.ts`. Do not modify `app/api/invoices/` or any other existing route.
Create the route file
File: `app/api/routes-b/invoices/[id]/cancel/route.ts`
Request body (optional)
```json
{ "reason": "Client requested cancellation" }
```
`reason` is optional, max 200 chars.
Handler logic
- Verify auth (standard pattern)
- Find invoice by `params.id` — verify `invoice.userId === user.id`
- If invoice is not `pending` → `422` with message `"Only pending invoices can be cancelled"`
- Update: `status: 'cancelled'`, `cancelledAt: new Date()`, and `cancellationReason` if provided
Expected response
```json
{
"id": "uuid",
"invoiceNumber": "INV-abc123",
"status": "cancelled",
"cancelledAt": "2025-01-01T00:00:00.000Z",
"cancellationReason": "Client requested cancellation"
}
```
Acceptance criteria
Overview
Implement a `POST` handler that cancels a pending invoice.
Create the route file
File: `app/api/routes-b/invoices/[id]/cancel/route.ts`
Request body (optional)
```json
{ "reason": "Client requested cancellation" }
```
`reason` is optional, max 200 chars.
Handler logic
Expected response
```json
{
"id": "uuid",
"invoiceNumber": "INV-abc123",
"status": "cancelled",
"cancelledAt": "2025-01-01T00:00:00.000Z",
"cancellationReason": "Client requested cancellation"
}
```
Acceptance criteria