Description
src/container/Container.ts (lines 78-90) selects PostgresCreditLineRepository when DATABASE_URL is set, but explicitly falls back to InMemoryRiskEvaluationRepository and InMemoryTransactionRepository with a // TODO: Implement PostgreSQL versions of other repositories. This means risk evaluations and transactions are lost on restart even in production, and PostgresCreditLineRepository.calculateAvailableCredit (lines 303-309) cannot subtract real draws because no transaction rows are persisted. This issue adds Postgres implementations behind the existing repository interfaces.
Requirements and context
- Implement
PostgresRiskEvaluationRepository satisfying src/repositories/interfaces/RiskEvaluationRepository.ts and PostgresTransactionRepository satisfying src/repositories/interfaces/TransactionRepository.ts (8 methods incl. findByStatus, updateStatus, deleteExpired, isValid).
- Target the
risk_evaluations and transactions tables defined in migrations/001_initial_schema.sql; add a migration if a column is missing and keep src/db/validate-schema.ts green.
- Wire both into
Container.initializeRepositories() so the useDatabase branch no longer instantiates in-memory stores, mirroring the PostgresCreditLineRepository pattern (use the shared DbClient from src/db/client.ts).
- Once
PostgresTransactionRepository exists, complete calculateAvailableCredit to subtract SUM(draws) for the credit line as the existing TODO describes.
- Non-functional: parameterized queries only (no string interpolation),
string-typed monetary columns preserved, and evaluatedAt/expiresAt mapped to/from SQL timestamps consistently with the in-memory behavior.
Acceptance criteria
Suggested execution
1. Fork the repo and create a branch — git checkout -b feature/postgres-risk-transaction-repos.
2. Implement changes — add files under src/repositories/postgres/, update src/container/Container.ts, and finish calculateAvailableCredit in src/repositories/postgres/PostgresCreditLineRepository.ts.
3. Write/extend tests — Vitest + Supertest; follow the layout of the existing src/repositories/postgres/__tests__/PostgresCreditLineRepository.test.ts with a mocked DbClient, and extend src/container/__tests__/Container.postgres.test.ts.
4. Test and commit —
npm run lint
npm run typecheck
npm test
npm run test:coverage
npm run build
Example commit message
feat(repositories): add Postgres RiskEvaluation and Transaction repositories
Guidelines
CI enforces a 95% coverage threshold (vitest.config.ts, run via npm run test:coverage) — every new query branch needs a test. Update docs/REPOSITORY_ARCHITECTURE.md and docs/data-model.md. Timeframe: 96 hours.
Description
src/container/Container.ts(lines 78-90) selectsPostgresCreditLineRepositorywhenDATABASE_URLis set, but explicitly falls back toInMemoryRiskEvaluationRepositoryandInMemoryTransactionRepositorywith a// TODO: Implement PostgreSQL versions of other repositories. This means risk evaluations and transactions are lost on restart even in production, andPostgresCreditLineRepository.calculateAvailableCredit(lines 303-309) cannot subtract real draws because no transaction rows are persisted. This issue adds Postgres implementations behind the existing repository interfaces.Requirements and context
PostgresRiskEvaluationRepositorysatisfyingsrc/repositories/interfaces/RiskEvaluationRepository.tsandPostgresTransactionRepositorysatisfyingsrc/repositories/interfaces/TransactionRepository.ts(8 methods incl.findByStatus,updateStatus,deleteExpired,isValid).risk_evaluationsandtransactionstables defined inmigrations/001_initial_schema.sql; add a migration if a column is missing and keepsrc/db/validate-schema.tsgreen.Container.initializeRepositories()so theuseDatabasebranch no longer instantiates in-memory stores, mirroring thePostgresCreditLineRepositorypattern (use the sharedDbClientfromsrc/db/client.ts).PostgresTransactionRepositoryexists, completecalculateAvailableCreditto subtractSUM(draws)for the credit line as the existingTODOdescribes.string-typed monetary columns preserved, andevaluatedAt/expiresAtmapped to/from SQL timestamps consistently with the in-memory behavior.Acceptance criteria
PostgresRiskEvaluationRepositoryandPostgresTransactionRepositoryimplement every interface method.Containeruses the Postgres implementations wheneverDATABASE_URLis set andNODE_ENV !== 'test'.calculateAvailableCreditreflects persisted draws instead of always returning the full limit.npm run db:validatepasses against the schema the new repositories query.docs/REPOSITORY_ARCHITECTURE.mddocuments the new implementations and the available-credit calculation.Suggested execution
1. Fork the repo and create a branch —
git checkout -b feature/postgres-risk-transaction-repos.2. Implement changes — add files under
src/repositories/postgres/, updatesrc/container/Container.ts, and finishcalculateAvailableCreditinsrc/repositories/postgres/PostgresCreditLineRepository.ts.3. Write/extend tests — Vitest + Supertest; follow the layout of the existing
src/repositories/postgres/__tests__/PostgresCreditLineRepository.test.tswith a mockedDbClient, and extendsrc/container/__tests__/Container.postgres.test.ts.4. Test and commit —
npm run lint npm run typecheck npm test npm run test:coverage npm run buildExample commit message
Guidelines
CI enforces a 95% coverage threshold (
vitest.config.ts, run vianpm run test:coverage) — every new query branch needs a test. Updatedocs/REPOSITORY_ARCHITECTURE.mdanddocs/data-model.md. Timeframe: 96 hours.