Skip to content

Implement Postgres repositories for RiskEvaluation and Transaction (only CreditLine is Postgres-backed today) #229

Description

@greatest0fallt1me

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

  • PostgresRiskEvaluationRepository and PostgresTransactionRepository implement every interface method.
  • Container uses the Postgres implementations whenever DATABASE_URL is set and NODE_ENV !== 'test'.
  • calculateAvailableCredit reflects persisted draws instead of always returning the full limit.
  • npm run db:validate passes against the schema the new repositories query.
  • In-memory repositories remain the default for tests; existing suites stay green.
  • docs/REPOSITORY_ARCHITECTURE.md documents the new implementations and the available-credit calculation.

Suggested execution

1. Fork the repo and create a branchgit 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.

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions