How harg-15/loanlens works
1. Overall shape
LoanLens is a single Spring Boot 3.2 backend monolith. Its core architecture is a conventional layered design: REST controllers expose the API, service classes hold business logic, Spring Data JPA repositories persist data, and JPA entities model the loan domain in PostgreSQL.
2. Request/response boundary
All external interaction enters through the controllers under com.loanlens.controller. The API is organized by business area: authentication, borrowers, loans, repayments, portfolio reporting, and risk alerts. DTOs in com.loanlens.dto.request and com.loanlens.dto.response isolate the HTTP contract from the persistence model.
3. Domain model
The main persisted entities are User, Borrower, Loan, LoanInstallment, Repayment, RiskAlert, and PortfolioSnapshot. These represent the operational loan system, the repayment lifecycle, the computed alert state, and historical portfolio snapshots used for monitoring.
4. Business service layer
Services in com.loanlens.service are the primary orchestration layer. They connect incoming controller requests to repositories, enforce domain rules, and shape responses. This is where the application likely coordinates loan creation, repayment posting, borrower management, portfolio summaries, and alert generation.
5. Persistence layer
Repositories in com.loanlens.repository are Spring Data JPA interfaces for each major entity. PostgreSQL is the production datastore, while tests use H2 as an in-memory replacement. The repository layer is the only boundary that should directly talk to the database.
6. Authentication and authorization
Security is JWT-based and stateless. JwtAuthFilter, JwtService, UserDetailsServiceImpl, and SecurityConfig form the authentication boundary, while AuthController and AuthService handle login and registration flows. The presence of Role suggests role-based access control over the loan and risk operations.
7. Risk scoring subsystem
The scoring subsystem is separated from the standard CRUD flow. ScoringEngine applies a set of ScoringStrategy implementations such as payment timeliness, overdue streak, partial payment behavior, and outstanding principal. This produces risk-related outputs that feed alerting and portfolio monitoring.
8. Scheduled background processing
Two schedulers run periodic jobs outside the request/response cycle: ScoringScheduler and PortfolioSnapshotScheduler. These background tasks likely re-evaluate loans for risk and persist snapshots of portfolio health, making the system proactive rather than purely transactional.
9. Early warning and alerting
Risk alerts are a first-class part of the domain, with dedicated RiskAlert entities, repository, service, and controller. This indicates a separate lifecycle for generated alerts: they are produced by scoring/scheduled jobs, stored, and then queried or managed through the API.
10. API documentation and operational setup
OpenApiConfig integrates Swagger/OpenAPI, so the service exposes machine-readable and interactive API docs. Runtime configuration comes from application.properties plus environment variables in .env.example, including database credentials, JWT secret/expiration, and default admin credentials.
11. Error handling
GlobalExceptionHandler centralizes error-to-response mapping, with explicit exceptions for bad requests and missing resources. This keeps failure handling consistent across all controllers and prevents domain errors from leaking directly to clients.
12. Test boundary
The test setup is lightweight and backend-focused: LoanLensApplicationTests plus H2-backed test properties validate the Spring context and likely core flows without needing PostgreSQL. This reinforces that the important architectural seams are the REST API, service layer, security layer, and scheduled scoring pipeline.
Main entry points: GlobalExceptionHandler.java Error handler · response Response DTOs · entity Domain entities · repository Repositories
Components 24
API layer
REST boundary and request/response DTOs
- Auth API · Login and registration endpoints
src/main/java/com/loanlens/controller/AuthController.java - Borrower API · Borrower management endpoints
src/main/java/com/loanlens/controller/BorrowerController.java - Loan API · Loan lifecycle endpoints
src/main/java/com/loanlens/controller/LoanController.java - Repayment API · Repayment posting endpoints
src/main/java/com/loanlens/controller/RepaymentController.java - Portfolio API · Portfolio summary endpoints
src/main/java/com/loanlens/controller/PortfolioController.java - Alert API · Risk alert query endpoints
src/main/java/com/loanlens/controller/RiskAlertController.java - Request DTOs · Inbound API request models
src/main/java/com/loanlens/dto/request - Response DTOs · Outbound API response models
src/main/java/com/loanlens/dto/response
Core services
Business orchestration and risk processing
- Auth service · Authentication and registration flows
src/main/java/com/loanlens/service/AuthService.java - Borrower service · Borrower business rules
src/main/java/com/loanlens/service/BorrowerService.java - Loan service · Loan creation and servicing logic
src/main/java/com/loanlens/service/LoanService.java - Repayment service · Repayment posting and allocation
src/main/java/com/loanlens/service/RepaymentService.java - Portfolio service · Portfolio summaries and snapshots
src/main/java/com/loanlens/service/PortfolioService.java - Alert service · Risk alert lifecycle management
src/main/java/com/loanlens/service/RiskAlertService.java - Scoring engine · Combines scoring strategies into risk outputs
src/main/java/com/loanlens/scoring/ScoringEngine.java - Strategies · Payment timeliness, overdue streak, partial payment, principal
src/main/java/com/loanlens/scoring/strategies
Data model
Persistent entities and repositories
- Domain entities · Borrower, loan, repayment, alert, and snapshot state
src/main/java/com/loanlens/entity - Repositories · Persistence interfaces for major entities
src/main/java/com/loanlens/repository
Platform
Security, scheduling, configuration, and error handling
- Security config · Filter chain and OpenAPI wiring
src/main/java/com/loanlens/security/SecurityConfig.java - JWT service · Token creation and validation
src/main/java/com/loanlens/security/JwtService.java - User details · Loads users for Spring Security
src/main/java/com/loanlens/security/UserDetailsServiceImpl.java - JWT filter · Stateless request authentication
src/main/java/com/loanlens/security/JwtAuthFilter.java - Error handler · Centralized API error responses
src/main/java/com/loanlens/exception/GlobalExceptionHandler.java - Schedulers · Periodic scoring and portfolio snapshot jobs
src/main/java/com/loanlens/scheduler
Connections 42
- Auth API → Request DTOs: accepts · Login and registration payloads
- Borrower API → Request DTOs: accepts · Borrower create/update payloads
- Loan API → Request DTOs: accepts · Loan request payloads
- Repayment API → Request DTOs: accepts · Repayment request payloads
- Auth API → Response DTOs: returns · Auth responses
- Borrower API → Response DTOs: returns · Borrower responses
- Loan API → Response DTOs: returns · Loan and installment responses
- Repayment API → Response DTOs: returns · Repayment responses
- Portfolio API → Response DTOs: returns · Portfolio summaries
- Alert API → Response DTOs: returns · Risk alert responses
- Auth API → Auth service: calls · Authentication flow
- Borrower API → Borrower service: calls · Borrower management
- Loan API → Loan service: calls · Loan orchestration
- Repayment API → Repayment service: calls · Repayment processing
- Portfolio API → Portfolio service: calls · Portfolio reporting
- Alert API → Alert service: calls · Alert querying and management
- Auth service → User details: uses · Loads authenticated users
- Auth service → JWT service: uses · Issues and validates tokens
- Security config → JWT filter: installs · Stateless auth filter
- Security config → JWT service: uses · Token handling
- Security config → User details: uses · Security user lookup
- Loan service → Repositories: persists via · Loan and installment data access
- Repayment service → Repositories: persists via · Repayment data access
- Borrower service → Repositories: persists via · Borrower data access
- Portfolio service → Repositories: persists via · Portfolio snapshot access
- Alert service → Repositories: persists via · Risk alert access
- Scoring engine → Strategies: composes · Applies scoring rules
- Schedulers → Scoring engine: triggers · Periodic loan re-scoring
- Schedulers → Portfolio service: triggers · Periodic portfolio snapshots
- Scoring engine → Alert service: feeds · Produces alert-worthy risk outputs
- Loan service → Domain entities: reads/writes · Loan domain state
- Repayment service → Domain entities: reads/writes · Repayment and installment state
- Portfolio service → Domain entities: reads/writes · Snapshot and portfolio state
- Alert service → Domain entities: reads/writes · Risk alert state
- Borrower service → Domain entities: reads/writes · Borrower state
- Auth service → Domain entities: reads/writes · User and role state
- Error handler → Auth API: handles · Controller error mapping
- Error handler → Borrower API: handles · Controller error mapping
- Error handler → Loan API: handles · Controller error mapping
- Error handler → Repayment API: handles · Controller error mapping
- Error handler → Portfolio API: handles · Controller error mapping
- Error handler → Alert API: handles · Controller error mapping