Skip to content

[routes-b] POST /api/routes-b/invoices/[id]/cancel — cancel a pending invoice #423

@davedumto

Description

@davedumto

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

  1. Verify auth (standard pattern)
  2. Find invoice by `params.id` — verify `invoice.userId === user.id`
  3. If invoice is not `pending` → `422` with message `"Only pending invoices can be cancelled"`
  4. 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

  • Returns `200` with updated invoice on success
  • Returns `422` if invoice is already paid, cancelled, or overdue
  • `reason` is optional; stored when provided
  • `reason` longer than 200 chars returns `400`
  • Returns `403` if invoice belongs to another user
  • Returns `404` if invoice does not exist
  • Returns `401` for unauthenticated requests

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions