# Leaves Backend API

Node.js / TypeScript REST API for leave, attendance, team shifts, and HR admin.

## Quick start

```bash
cd backend
cp .env.example .env   # or use existing .env
npm install
npx prisma generate
npx prisma migrate deploy   # applies leaves_init + db corrections (shared DB safe)
npm run seed
npm run dev
```

- API base: `http://localhost:3000/api/v1`
- Health: `GET /api/v1/health`

## Architecture

| Pattern | Location |
|---------|----------|
| Singleton | `src/config`, `src/infrastructure` |
| Facade | `src/facades/*` |
| Controller | `src/controllers/*` (routes only mount them) |
| Builder | `src/builders/*` |
| Abstract Factory | `src/factories/*` |
| Domain rules | `src/domain/*` |
| Jobs | `src/jobs/scheduledJobs.ts` |

Full layout and rules: [node.md](./node.md).

## Test fixtures (seed)

| Employee | ID | Role |
|----------|-----|------|
| Alice | GTPL-MGR | Team lead |
| Bob | GTPL-BOB | Member |
| Carol | GTPL-CAR | Member |

HR login: `hr.admin` / `hr-admin-pass`

## Scripts

| Command | Purpose |
|---------|---------|
| `npm run dev` | Development server |
| `npm run build` | Compile TypeScript |
| `npm test` | Unit tests (sandwich leave) |
| `npm run seed` | Platform team fixtures |
| `npm run test:api` | Newman Postman collection |

## Shared database

Uses MySQL `salaryslip` shared with salary-slip and review. Leaves tables are added via Prisma Migrate (`prisma/migrations/`) without altering salary-slip or review tables.

| Migration | Purpose |
|-----------|---------|
| `20250609000000_leaves_init` | Create 14 leaves module tables |
| `20250610000000_db_corrections` | FK ON UPDATE CASCADE + backfill |
| `20250610000001_align_updated_at_with_prisma` | `updated_at` columns match `@updatedAt` |

## API keys (`.env`)

- `ATTENDANCE_API_KEY` — header `X-Attendance-Api-Key`
- `CALENDAR_API_KEY` — header `X-Api-Key`

See [docs/openapi.yaml](../docs/openapi.yaml) for full contract.
