A comprehensive NestJS-based loan management system with AI-powered features, supporting both Oracle 23ai and PostgreSQL + pgvector databases.
- Customer Management - Full CRUD operations with validation
- Loan Processing - Complete loan lifecycle management
- AI-Powered Analysis - Risk assessment and loan recommendations
- Vector Search - Similar loan finding using embeddings
- Oracle 23ai - Native vector operations with
dbms_vector_chain.utl_to_embedding
- PostgreSQL + pgvector - Vector similarity search and storage
- Dynamic Configuration - Switch between databases via environment variables
- Loan Analysis - AI-powered application analysis
- Risk Assessment - Automated risk scoring
- Vector Embeddings - Text-to-vector conversion
- Similarity Search - Find similar loans using vector operations
- Loan Recommendations - AI-generated recommendations
backend/
βββ src/
β βββ app.module.ts
β βββ main.ts
β βββ config/
β β βββ database.strategy.ts # Strategy pattern for DB connections
β β βββ oracle.service.ts # Oracle 23ai implementation
β β βββ postgres.service.ts # PostgreSQL + pgvector implementation
β βββ customer/
β β βββ customer.controller.ts
β β βββ customer.service.ts
β β βββ dto/
β βββ loan/
β β βββ loan.controller.ts
β β βββ loan.service.ts
β β βββ dto/
β βββ ai/
β β βββ ai.controller.ts
β β βββ ai.service.ts
β βββ entities/ # TypeORM entities
β β βββ client.entity.ts
β β βββ loan-application.entity.ts
β β βββ ... (12 entities total)
βββ ormconfig.ts # TypeORM configuration
βββ .env # Environment configuration
βββ Dockerfile
-
Clone the repository
git clone https://github.com/BillyOkoth/loan-backend.git cd loan-backend
-
Install dependencies
npm install
-
Configure environment
cp env.example .env # Edit .env with your actual credentials and API keys # β οΈ IMPORTANT: Never commit .env files to version control
-
Start the application
npm run start:dev
# Run PostgreSQL with pgvector
docker run -d \
--name pgvector-dev \
-e POSTGRES_USER=USER \
-e POSTGRES_PASSWORD=SECRET \
-e POSTGRES_DB=DB \
-p 5432:5432 \
ankane/pgvector
- Install Oracle 23ai with vector capabilities
- Configure connection string in
.env
.env
files to version control. The .env
file contains sensitive information like API keys and database passwords.
cp env.example .env
Edit the .env
file with your actual values:
# Application Configuration
NODE_ENV=development
PORT=3000
# Database Type (oracle | postgres)
DATABASE_TYPE=postgres
# PostgreSQL + pgvector Configuration
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
POSTGRES_DB=db
POSTGRES_USER=your_user
POSTGRES_PASSWORD=your_password
# Oracle Configuration
ORACLE_HOST=localhost
ORACLE_PORT=1521
ORACLE_USER=your_user
ORACLE_PASSWORD=your_password
ORACLE_SID=XEPDB1
# AI Configuration
AI_MODEL_ENDPOINT=https://api.openai.com/v1/embeddings
AI_API_KEY=your_actual_openai_api_key_here
# pgAdmin Configuration
PGADMIN_DEFAULT_EMAIL=admin@loanapp.com
PGADMIN_DEFAULT_PASSWORD=your_secure_pgadmin_password_here
POSTGRES_PASSWORD
: Use a strong, unique passwordAI_API_KEY
: Your actual OpenAI API keyPGADMIN_DEFAULT_PASSWORD
: Secure password for pgAdmin access
For different environments, create separate files:
.env.development
- Development environment.env.test
- Testing environment.env.production
- Production environment.env.staging
- Staging environment
For production deployments:
- Use environment-specific secret management
- Rotate passwords regularly
- Use strong, unique passwords for each service
- Consider using Docker secrets or Kubernetes secrets
POST /customers
- Create customerGET /customers
- Get all customers (paginated)GET /customers/:id
- Get customer by IDPUT /customers/:id
- Update customerDELETE /customers/:id
- Delete customerGET /customers/:id/loans
- Get customer loans
POST /loans
- Create loanGET /loans
- Get all loans (paginated)GET /loans/:id
- Get loan by IDPUT /loans/:id
- Update loanDELETE /loans/:id
- Delete loanPOST /loans/:id/approve
- Approve loanPOST /loans/:id/reject
- Reject loanGET /loans/customer/:customerId
- Get loans by customer
POST /ai/analyze-loan
- Analyze loan applicationPOST /ai/generate-embedding
- Generate text embeddingsPOST /ai/vector-search
- Search similar vectorsPOST /ai/store-vector
- Store vector dataGET /ai/similar-loans/:loanId
- Find similar loansPOST /ai/risk-assessment
- Assess customer riskPOST /ai/loan-recommendation
- Get loan recommendations
-
Setup Environment
cp env.example .env # Edit .env with your actual values
-
Start Services
docker-compose up -d
-
Access Services
- Application: http://localhost:3000
- pgAdmin: http://localhost:8080
- PostgreSQL: localhost:5432
- Prometheus: http://localhost:9090
- Grafana: http://localhost:3001 (admin/admin123)
# Build image
docker build -t loan-backend .
# Run container with environment file
docker run -p 3000:3000 --env-file .env loan-backend
# Or run with individual environment variables
docker run -p 3000:3000 \
-e NODE_ENV=production \
-e POSTGRES_HOST=your_db_host \
-e POSTGRES_PASSWORD=your_password \
-e AI_API_KEY=your_api_key \
loan-backend
For production, use Docker secrets or environment variables:
# Using Docker secrets
docker run -p 3000:3000 \
--secret db_password \
--secret api_key \
loan-backend
# Using environment variables
docker run -p 3000:3000 \
-e POSTGRES_PASSWORD=$(cat /path/to/db_password) \
-e AI_API_KEY=$(cat /path/to/api_key) \
loan-backend
The application includes comprehensive monitoring with Prometheus and Grafana:
- Prometheus Metrics:
http://localhost:3000/metrics
- Available Metrics:
- HTTP request rate and duration
- Error rates by status code
- Memory and CPU usage
- Active connections
- Loan application counts
- Customer registration counts
- Grafana: http://localhost:3001
- Default credentials:
admin/admin123
- Pre-configured dashboard: "Loan Backend Monitoring"
- Real-time metrics visualization
- Default credentials:
- Prometheus: http://localhost:9090
- Scrapes metrics every 15 seconds
- Stores data for 200 hours
- Monitors application, database, and system metrics
The application exposes custom business metrics:
loan_applications_total
- Total loan applications by statuscustomer_registrations_total
- Customer registration counthttp_requests_total
- HTTP request counts by method/route/statushttp_request_duration_seconds
- Response time histograms
# Unit tests
npm run test
# E2E tests
npm run test:e2e
# Test coverage
npm run test:cov
The application includes 12 entities following the PostgreSQL schema:
clients
- Customer informationloan_applications
- Loan applicationsclient_debt
- Customer debt informationmock_loan_data
- Mock loan dataloan_chunk
- Vector embeddingsclients_to_loan_recommendations
- Loan recommendations- And 6 additional entities for complete loan management
- Input validation using class-validator
- Environment-based configuration
- Secure database connections
- API key management for AI services
- Environment Variables: Never commit
.env
files to version control - Secrets Management: Use secure secret management for production
- Database Security: Use strong passwords and secure connections
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
This project is licensed under the MIT License.
Billy Okoth
- GitHub: @BillyOkoth
- Website: www.masaicode.com