# Internal CRM

AdMedia's internal HubSpot replacement (Jira epic **HUB-44**, build workstream **HUB-65**).
This repo is the backend: a Fastify REST API and a background worker on MySQL 5.7, deployed with PM2.
The frontend lives in its own repo and talks to this API over HTTP.

## Quick start

You need **Node 20.19+** (comes with npm 10), **Docker Desktop** (only for the local database) and **Git**.

```sh
npm install                 # installs every workspace and generates the Prisma client
cp .env.example .env
npm run db:up               # MySQL 5.7 in Docker on port 3307
npm run db:migrate          # apply migrations to your local database
npm run db:seed             # two test businesses and three dev users
npm run dev                 # API + worker with auto-reload
```

Then open:

- http://127.0.0.1:3340/docs for Swagger, where you can try every endpoint (the frontend team can use `/docs/json`)
- http://127.0.0.1:3340/healthz and http://127.0.0.1:3340/readyz

## Signing in locally

Google login comes later (HUB-81). Until then, with `DEV_AUTH_ENABLED=true`, you sign in by sending two headers.
In Swagger, click **Authorize** and fill them in once:

| Header           | Example              | Seeded values                                                                                                                               |
| ---------------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| `x-dev-user`     | `dev.admin@crm.test` | `dev.admin@crm.test` (admin in both), `dev.user.a@crm.test` / `dev.user.b@crm.test` (standard), `dev.viewer@crm.test` (read-only in test-a) |
| `x-dev-business` | `test-a`             | `test-a`, `test-b` (slug or id)                                                                                                             |

`GET /api/v1/me` shows who you are signed in as. With curl:

```sh
curl -H "x-dev-user: dev.admin@crm.test" -H "x-dev-business: test-a" http://127.0.0.1:3340/api/v1/me
```

## Commands

| Command                                  | What it does                                                   |
| ---------------------------------------- | -------------------------------------------------------------- |
| `npm run dev`                            | Run the API and the worker with auto-reload                    |
| `npm run dev:api` / `npm run dev:worker` | Run just one of them                                           |
| `npm run check`                          | Typecheck, lint, format check and tests. Run before every PR   |
| `npm test` / `npm run test:watch`        | Tests (Vitest). Needs the local MySQL running; uses `crm_test` |
| `npm run lint:fix` / `npm run format`    | Auto-fix lint and formatting                                   |
| `npm run db:migrate`                     | Create and apply a migration after editing `schema.prisma`     |
| `npm run db:seed`                        | Add the dev businesses and users (safe to re-run)              |
| `npm run business:create -- --name …`    | Create a business and its first admin (staging/production)     |
| `npm run business:suspend -- --slug …`   | Suspend a business (blocks sign-in and jobs, ends sessions)    |
| `npm run business:resume -- --slug …`    | Make a suspended business active again                         |
| `npm run db:studio`                      | Browse the local database in the browser                       |
| `npm run db:up` / `npm run db:down`      | Start or stop the local MySQL container                        |
| `npm run build`                          | Production build to `apps/*/dist`                              |
| `npm run deploy`                         | Shell deploy: pull, ci, build, migrate, PM2 (uses `.env`)      |

## Folder structure

```
apps/                     things that run
  api/                    Fastify REST API: routes, plugins, Swagger
    src/modules/<name>/   one folder per feature (routes.ts + routes.test.ts)
    src/plugins/          error handler, Swagger
  worker/                 runs background work: queued jobs and timed schedules
    src/queue-runner.ts   runs queued jobs (defined in core with defineJob)
    src/schedules/        work on a timer, one file each (heartbeat, stuck-job reaper)
packages/                 code shared by the apps
  core/                   business logic: services, errors, logger, env, pagination
    src/<name>/service.ts one service per feature, used by the API and the worker
  db/                     Prisma schema, migrations, client (MySQL 5.7)
  shared/                 Zod request/response schemas and types
deploy/                   PM2 ecosystem + deploy.sh (server deploy)
docs/                     architecture and how-to guides
```

Requests flow **route → service → database**. Routes never touch the database, and ESLint enforces this.

This is an npm workspaces monorepo. Always run `npm install` from the repo root, and add a dependency to one package with
`npm install <pkg> -w @crm/api` (or `@crm/worker`, `@crm/core`, `@crm/db`, `@crm/shared`).

## Read next

1. [AGENTS.md](AGENTS.md): coding rules for this repo, for people and AI assistants
2. [docs/architecture.md](docs/architecture.md): how the pieces fit together
3. [docs/adding-a-module.md](docs/adding-a-module.md): step-by-step recipe for a new feature
4. [CONTRIBUTING.md](CONTRIBUTING.md): branches, commits, pull requests
5. [deploy/README.md](deploy/README.md): deploying with `npm run deploy` (config from `.env`)
