# Tasks: MySQL Review Persistence

## Review Workload Forecast

| Field | Value |
|-------|-------|
| Estimated changed lines | 550-750 |
| 400-line budget risk | High |
| Chained PRs recommended | Yes |
| Suggested split | PR 1 config/pool → PR 2 repository/service → PR 3 route wiring |
| Delivery strategy | auto-forecast |
| Chain strategy | stacked-to-main |

Decision needed before apply: Yes
Chained PRs recommended: Yes
Chain strategy: stacked-to-main
400-line budget risk: High

### Suggested Work Units

| Unit | Goal | Likely PR | Notes |
|------|------|-----------|-------|
| 1 | DB dependency/config/pool with tests | PR 1 | independent foundation; `npm test`, `npm run typecheck` |
| 2 | Repository and mapper/service with tests | PR 2 | depends on PR 1; no route behavior change |
| 3 | Route/app integration and docs | PR 3 | depends on PR 2; preserves response contract |

## Phase 1: Foundation / RED-GREEN

- [x] 1.1 RED: Add `src/config.test.ts` cases for missing DB env and invalid `DB_PORT` startup failures.
- [x] 1.2 GREEN: Update `src/config.ts` with required DB config and safe port parsing.
- [x] 1.3 RED: Add `src/database/mysqlPool.test.ts` for pool creation from config without credential defaults.
- [x] 1.4 GREEN: Add `mysql2` to `package.json`/`package-lock.json` and create `src/database/mysqlPool.ts`.

## Phase 2: Persistence Core / RED-GREEN

- [x] 2.1 RED: Create `src/services/reviewPersistenceService.test.ts` for `puntuacion` mapping, nullable `authorName`, and null `authorUrl`.
- [x] 2.2 GREEN: Create `src/repositories/reviewRepository.ts` and `src/services/reviewPersistenceService.ts` mapper/orchestrator.
- [x] 2.3 RED: Create `src/repositories/mysqlReviewRepository.test.ts` asserting placeholder upsert SQL and ordered values.
- [x] 2.4 GREEN: Create `src/repositories/mysqlReviewRepository.ts` with `INSERT ... ON DUPLICATE KEY UPDATE`.

## Phase 3: Route/App Integration / RED-GREEN

- [x] 3.1 RED: Extend `src/routes/reviewRoute.test.ts` to expect persistence before Gemini and unchanged `{ text, name }` success.
- [x] 3.2 GREEN: Update `src/routes/reviewRoute.ts` to accept a persistence dependency and await it before `generateResponse`.
- [x] 3.3 RED: Add `src/routes/reviewRoute.test.ts` failure case: DB error blocks Gemini and does not log secrets.
- [x] 3.4 GREEN: Update route error handling as needed, preserving existing validation/auth behavior.
- [x] 3.5 Wire `src/server/app.ts` to create pool, repository, persistence service, and existing Gemini service.

## Phase 4: Refactor / Verification

- [x] 4.1 Update `.env.example` with DB variable names only, no real secrets.
- [x] 4.2 REFACTOR: Remove duplication in test fixtures across `src/config.test.ts` and `src/routes/reviewRoute.test.ts`.
- [x] 4.3 Run `npm test` and `npm run typecheck`; fix failures without expanding scope.
