Skip to content

[routes-b] GET /api/routes-b/invoices/[id]/activity — audit events for an invoice #405

@davedumto

Description

@davedumto

Overview

Implement a `GET` handler that returns all audit events related to a specific invoice.

⚠️ Scope: Write your code only inside `app/api/routes-b/invoices/[id]/activity/route.ts`. Do not touch any file outside this folder.


Create the route file

File: `app/api/routes-b/invoices/[id]/activity/route.ts`

The `AuditEvent` model already exists in `prisma/schema.prisma`.

Handler logic

  1. Verify auth
  2. Find invoice by `params.id` — verify `invoice.userId === user.id`
  3. Fetch `AuditEvent` records where `resourceType === 'invoice'` AND `resourceId === params.id`
  4. Order by `createdAt asc` (chronological)

Expected response

```json
{
"activity": [
{
"id": "uuid",
"action": "invoice_created",
"ipAddress": "192.168.1.1",
"createdAt": "2025-01-01T00:00:00.000Z"
},
{
"id": "uuid",
"action": "invoice_viewed",
"ipAddress": "41.58.0.1",
"createdAt": "2025-01-02T09:00:00.000Z"
}
]
}
```


Acceptance criteria

  • Returns `200` with chronologically ordered activity array
  • Returns empty array (not `404`) when no audit events exist for the invoice
  • Returns `403` if invoice belongs to a different 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