|
| 1 | +import { Injectable, NotFoundException } from '@nestjs/common'; |
| 2 | + |
| 3 | +import { DocumentsService } from '../documents/documents.service'; |
| 4 | +import { Document } from '../documents/entities/document.entity'; |
| 5 | + |
| 6 | +export enum RiskFlag { |
| 7 | + MISSING_PARCEL_ID = 'MISSING_PARCEL_ID', |
| 8 | + OVERLAPPING_CLAIM = 'OVERLAPPING_CLAIM', |
| 9 | + FORGED_SIGNATURE_INDICATOR = 'FORGED_SIGNATURE_INDICATOR', |
| 10 | + EXPIRED_DOCUMENT = 'EXPIRED_DOCUMENT', |
| 11 | + INCOMPLETE_OWNERSHIP_CHAIN = 'INCOMPLETE_OWNERSHIP_CHAIN', |
| 12 | + UNKNOWN_ISSUER = 'UNKNOWN_ISSUER', |
| 13 | +} |
| 14 | + |
| 15 | +export interface RiskResult { |
| 16 | + score: number; |
| 17 | + flags: RiskFlag[]; |
| 18 | +} |
| 19 | + |
| 20 | +const FLAG_WEIGHTS: Record<RiskFlag, number> = { |
| 21 | + [RiskFlag.MISSING_PARCEL_ID]: 20, |
| 22 | + [RiskFlag.OVERLAPPING_CLAIM]: 20, |
| 23 | + [RiskFlag.FORGED_SIGNATURE_INDICATOR]: 25, |
| 24 | + [RiskFlag.EXPIRED_DOCUMENT]: 15, |
| 25 | + [RiskFlag.INCOMPLETE_OWNERSHIP_CHAIN]: 10, |
| 26 | + [RiskFlag.UNKNOWN_ISSUER]: 10, |
| 27 | +}; |
| 28 | + |
| 29 | +@Injectable() |
| 30 | +export class RiskAssessmentService { |
| 31 | + constructor(private readonly documentsService: DocumentsService) {} |
| 32 | + |
| 33 | + async assessDocument(documentId: string): Promise<RiskResult> { |
| 34 | + const document = await this.documentsService.findById(documentId); |
| 35 | + if (!document) { |
| 36 | + throw new NotFoundException('Document not found'); |
| 37 | + } |
| 38 | + |
| 39 | + const flags = await this.detectFlags(document); |
| 40 | + const score = this.calculateScore(flags); |
| 41 | + |
| 42 | + await this.documentsService.updateRisk(documentId, score, flags); |
| 43 | + |
| 44 | + return { score, flags }; |
| 45 | + } |
| 46 | + |
| 47 | + private async detectFlags(document: Document): Promise<RiskFlag[]> { |
| 48 | + const flags: RiskFlag[] = []; |
| 49 | + |
| 50 | + if (!document.title || !/\d/.test(document.title)) { |
| 51 | + flags.push(RiskFlag.MISSING_PARCEL_ID); |
| 52 | + } |
| 53 | + |
| 54 | + const ownerDocuments = await this.documentsService.findByOwner(document.ownerId); |
| 55 | + if (ownerDocuments.some((doc) => doc.id !== document.id)) { |
| 56 | + flags.push(RiskFlag.OVERLAPPING_CLAIM); |
| 57 | + } |
| 58 | + |
| 59 | + if ( |
| 60 | + document.mimeType === 'application/pdf' && |
| 61 | + document.fileSize !== undefined && |
| 62 | + document.fileSize < 50_000 |
| 63 | + ) { |
| 64 | + flags.push(RiskFlag.FORGED_SIGNATURE_INDICATOR); |
| 65 | + } |
| 66 | + |
| 67 | + if (document.title?.toLowerCase().includes('expired')) { |
| 68 | + flags.push(RiskFlag.EXPIRED_DOCUMENT); |
| 69 | + } |
| 70 | + |
| 71 | + if (!document.title || document.title.trim().length < 12) { |
| 72 | + flags.push(RiskFlag.INCOMPLETE_OWNERSHIP_CHAIN); |
| 73 | + } |
| 74 | + |
| 75 | + if (!document.title?.toLowerCase().includes('issued')) { |
| 76 | + flags.push(RiskFlag.UNKNOWN_ISSUER); |
| 77 | + } |
| 78 | + |
| 79 | + return Array.from(new Set(flags)); |
| 80 | + } |
| 81 | + |
| 82 | + private calculateScore(flags: RiskFlag[]): number { |
| 83 | + const rawScore = flags.reduce((total, flag) => total + (FLAG_WEIGHTS[flag] ?? 0), 0); |
| 84 | + return Math.min(100, Math.max(0, rawScore)); |
| 85 | + } |
| 86 | +} |
0 commit comments