# Add an Audit Log to a NestJS API from a Linear Issue

An agent records every NestJS user create, update and delete in audit_logs inside the same transaction, behind an admin-only read endpoint.

[Run this on Rush](https://cloud.getrush.ai/run/add-soft-delete-audit/nestjs/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads `CLAUDE.md`, `.claude/skills/generate/SKILL.md`, `users.controller.ts`, `users.service.ts`, both user repository adapters and `roles.guard.ts`, then greps every caller of `usersService.create`, `update` and `remove`. Writes reach users from the admin `/users` routes, `/auth/me`, and register, email confirmation, password reset and social login in `auth.service.ts`.

It scaffolds the resource with `npm run generate:resource:all-db -- --name AuditLog` and `add:property:to-all-db`, then strips the generated write routes so the log is append-only. Every `UsersService` write now takes a `UserAuditContext`, so the compiler finds each call site; actors come from the JWT, and auth flows record the user as their own actor (`SELF_AUDIT`). The relational adapter inserts the audit row inside `manager.transaction` with the soft delete or update.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "A Linear issue labeled agent requests user-change auditing: record who created, updated or deleted a user, the target, action and timestamp in audit_logs, and expose GET /api/v1/audit-logs to admins only. Read the existing user controller, service, repository adapters, role checks and generator instructions first. Preserve soft deletion. Obtain actors from authenticated context, never caller-supplied DTO fields; define coverage for other user-write entry points and keep each mutation and audit entry atomic. Follow the repository's resource generators and state the relational/document scope. Exclude passwords, tokens and full request bodies. Add database-backed tests for create/update/delete records, rollback behavior, admin reads and denied ordinary/anonymous requests without side effects. Run npm run lint, npm test -- --runInBand and the Postgres e2e suite in non-watch mode in the sandbox. Open a pull request linking the supplied Linear issue and list actual checks and limits.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

Tried on brocoders/nestjs-boilerplate at 9620f15: 29 files changed, 1154 insertions(+), 112 deletions(-), including a migration and two e2e specs. The Postgres e2e suite went from `Tests:       20 passed, 20 total` to `Tests:       36 passed, 36 total`, `npm run lint` exited 0 before and after, and the unit run ended `Tests:       2 passed, 2 total`. The untouched clone's `npm test` found no specs and exited 1, and its `npm ci` failed on a lockfile missing `gcp-metadata@7.0.1`.

For the reviewer: the MongoDB adapter compiles but never ran, since the sandbox had no MongoDB, and its transactions need a replica set; the Mongo adapter also still hard-deletes users, as before. Update entries name no changed fields, and the actor and target columns carry no foreign keys, so entries outlive deleted users.

## Run it on every Linear issue labeled agent

An automation with a `linear` trigger on `linear.issue.labeled` and the label `agent` runs the `linear-label-to-pr` recipe's prompt when someone in your Rush org labels an audit request. An issue that lists the write paths to cover, or rules the MongoDB adapter in or out, narrows a change that here touched 29 files.

## Why run it on Rush?

An audit log that misses a write path is worse than none, because it looks complete. The run found five auth flows that write users without touching the users controller, and the new `UserAuditContext` argument makes the compiler refuse any future call that skips it. The rollback spec forced the audit insert to fail with a database `CHECK` constraint and saw the create, update and soft delete all rolled back.

## Frequently asked questions

### Can a client forge who made the change?

No. The admin routes take the actor from `request.user.id`, and `/auth/me` uses the token's own id. The e2e test sends an `actorId` in the body and checks the stored row ignores it.

### What did the e2e setup need?

An embedded Postgres 17.5 and maildev 2.0.5 instead of the repository's Docker setup, and `test/generators` was left out because it needs Docker.

## More NestJS jobs

- [Dockerize a NestJS App](https://getrush.ai/run/dockerize-app/nestjs/)
- [Fix a NestJS API Bug From a Datadog Monitor Alert](https://getrush.ai/run/fix-datadog-alerts/nestjs/)
- [Fix Failing GitHub Actions Checks in a NestJS API](https://getrush.ai/run/fix-failing-ci/nestjs/)

## More jobs like this: Turn a Linear label into a PR

- [Add Soft Delete to a Fastify API from a Linear Issue](https://getrush.ai/run/add-soft-delete/fastify/)
- [Add Soft Delete to GORM Models](https://getrush.ai/run/add-soft-delete/gorm/)
- [Add Soft Delete to SQLAlchemy Models](https://getrush.ai/run/add-soft-delete/sqlalchemy/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
