|
| 1 | +import { Injectable } from '@nestjs/common'; |
| 2 | +import { CreditAssessmentResultDto } from './dto/credit-scoring-response.dto'; |
| 3 | + |
| 4 | +export interface AssessParams { |
| 5 | + amount: number; |
| 6 | + reputationScore: number; |
| 7 | + maxCredit: number; |
| 8 | + creditUtilization: number; |
| 9 | +} |
| 10 | + |
| 11 | +@Injectable() |
| 12 | +export class CreditScoringService { |
| 13 | + assess(params: AssessParams): CreditAssessmentResultDto { |
| 14 | + const { amount, reputationScore, maxCredit, creditUtilization } = params; |
| 15 | + const reasons: string[] = []; |
| 16 | + |
| 17 | + if (reputationScore < 60) { |
| 18 | + reasons.push( |
| 19 | + `Reputation score ${reputationScore} is below the minimum threshold of 60`, |
| 20 | + ); |
| 21 | + return { decision: 'rejected', score: reputationScore, reasons }; |
| 22 | + } |
| 23 | + |
| 24 | + if (amount > maxCredit) { |
| 25 | + reasons.push( |
| 26 | + `Loan amount $${amount} exceeds maximum credit limit of $${maxCredit}`, |
| 27 | + ); |
| 28 | + return { decision: 'rejected', score: reputationScore, reasons }; |
| 29 | + } |
| 30 | + |
| 31 | + if ( |
| 32 | + reputationScore >= 75 && |
| 33 | + amount <= maxCredit * 0.8 && |
| 34 | + creditUtilization < 0.7 |
| 35 | + ) { |
| 36 | + reasons.push( |
| 37 | + `Strong reputation score of ${reputationScore} with sufficient available credit`, |
| 38 | + ); |
| 39 | + return { decision: 'approved', score: reputationScore, reasons }; |
| 40 | + } |
| 41 | + |
| 42 | + if (reputationScore >= 75) { |
| 43 | + if (amount > maxCredit * 0.8) { |
| 44 | + reasons.push( |
| 45 | + `Loan amount ($${amount}) exceeds 80% of credit limit ($${maxCredit})`, |
| 46 | + ); |
| 47 | + } |
| 48 | + if (creditUtilization >= 0.7) { |
| 49 | + reasons.push( |
| 50 | + `Credit utilization at ${Math.round(creditUtilization * 100)}% exceeds 70% threshold`, |
| 51 | + ); |
| 52 | + } |
| 53 | + return { decision: 'manual_review', score: reputationScore, reasons }; |
| 54 | + } |
| 55 | + |
| 56 | + reasons.push( |
| 57 | + `Bronze tier reputation score (${reputationScore}) requires manual review`, |
| 58 | + ); |
| 59 | + return { decision: 'manual_review', score: reputationScore, reasons }; |
| 60 | + } |
| 61 | +} |
0 commit comments