Skip to content

Commit dc14ce3

Browse files
authored
Merge pull request #142 from shamsss04/shams4
Shams4
2 parents 9ba5901 + 5a8a761 commit dc14ce3

12 files changed

Lines changed: 184 additions & 5 deletions

dist/entities/invoice.entity.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/health/health.controller.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/risk/risk.module.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/tsconfig.build.tsbuildinfo

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/dynamic/dynamic.serivice.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,19 @@ export interface ParsedInvoice {
66
dueDate?: string;
77
}
88

9+
/**
10+
* Service for parsing PDF invoice files using optical character recognition (OCR) patterns.
11+
* Extracts key financial metadata like total amounts and due dates.
12+
*/
913
@Injectable()
1014
export class PdfService {
15+
/**
16+
* Parses a PDF buffer to extract invoice data.
17+
*
18+
* @param pdfBuffer - The raw binary buffer of the PDF file.
19+
* @returns A promise resolving to a ParsedInvoice object.
20+
* @throws Error if the PDF parsing fails.
21+
*/
1122
async parseInvoicePdf(pdfBuffer: Buffer): Promise<ParsedInvoice> {
1223
try {
1324
const data = await pdfParse(pdfBuffer);
@@ -25,6 +36,14 @@ export class PdfService {
2536
}
2637
}
2738

39+
/**
40+
* Searches the text content for potential currency amounts using regex patterns.
41+
* Identifies the largest amount found as the likely total.
42+
*
43+
* @param text - The raw text extracted from the PDF.
44+
* @returns The largest positive amount found, or undefined if none found.
45+
* @private
46+
*/
2847
private extractTotalAmount(text: string): number | undefined {
2948
// Regex patterns to find currency amounts
3049
const patterns = [
@@ -57,6 +76,13 @@ export class PdfService {
5776
return amounts.length > 0 ? Math.max(...amounts) : undefined;
5877
}
5978

79+
/**
80+
* Searches the text content for potential due dates using regex patterns.
81+
*
82+
* @param text - The raw text extracted from the PDF.
83+
* @returns The first valid date found in ISO format, or undefined.
84+
* @private
85+
*/
6086
private extractDueDate(text: string): string | undefined {
6187
// Regex patterns to find dates
6288
const patterns = [
@@ -80,6 +106,13 @@ export class PdfService {
80106
return undefined;
81107
}
82108

109+
/**
110+
* Normalizes a date string into ISO 8601 format (YYYY-MM-DD).
111+
*
112+
* @param dateString - The raw date string to normalize.
113+
* @returns The normalized date string, or the original if invalid.
114+
* @private
115+
*/
83116
private normalizeDate(dateString: string): string {
84117
// Convert various date formats to ISO format (YYYY-MM-DD)
85118
const date = new Date(dateString);

src/entities/invoice.entity.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,86 @@
11
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne, JoinColumn } from 'typeorm';
22
import { User } from './user.entity';
33

4+
/**
5+
* Database entity representing a financial invoice.
6+
* Tracks amount, customer details, risk assessment, and processing status.
7+
*/
48
@Entity('invoices')
59
export class Invoice {
10+
/**
11+
* Internal auto-incrementing identifier.
12+
*/
613
@PrimaryGeneratedColumn()
714
id: number;
815

16+
/**
17+
* The total monetary amount of the invoice.
18+
*/
919
@Column({ type: 'decimal', precision: 10, scale: 2 })
1020
amount: number;
1121

22+
/**
23+
* The date the invoice was issued.
24+
*/
1225
@Column({ type: 'timestamp' })
1326
date: Date;
1427

28+
/**
29+
* Name of the customer or counterparty.
30+
*/
1531
@Column()
1632
customer: string;
1733

34+
/**
35+
* Optional description or notes regarding the invoice.
36+
*/
1837
@Column({ type: 'text', nullable: true })
1938
description?: string;
2039

40+
/**
41+
* Risk score calculated by the RiskService (0-100).
42+
*/
2143
@Column({ type: 'int', default: 0 })
2244
riskScore: number;
2345

46+
/**
47+
* Current status of the invoice in the workflow.
48+
*/
2449
@Column({
2550
type: 'varchar',
2651
default: 'Pending',
2752
enum: ['Pending', 'Approved', 'High Risk', 'Rejected']
2853
})
2954
status: string;
3055

56+
/**
57+
* Timestamp indicating when the invoice was processed by the system.
58+
*/
3159
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
3260
processedAt: Date;
3361

62+
/**
63+
* The user who uploaded or owns this invoice.
64+
*/
3465
@ManyToOne(() => User, { nullable: true })
3566
@JoinColumn({ name: 'userId' })
3667
user?: User;
3768

69+
/**
70+
* Foreign key reference to the user.
71+
*/
3872
@Column({ nullable: true })
3973
userId?: string;
4074

75+
/**
76+
* Record creation timestamp.
77+
*/
4178
@CreateDateColumn()
4279
createdAt: Date;
4380

81+
/**
82+
* Record update timestamp.
83+
*/
4484
@UpdateDateColumn()
4585
updatedAt: Date;
4686
}

src/entities/sentry.entity.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,86 @@
11
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne, JoinColumn } from 'typeorm';
22
import { User } from './user.entity';
33

4+
/**
5+
* Database entity for Sentry-related invoice data.
6+
* Used for monitoring and tracking specific high-priority or automated invoice flows.
7+
*/
48
@Entity('Sentry')
59
export class Sentry {
10+
/**
11+
* Internal auto-incrementing identifier.
12+
*/
613
@PrimaryGeneratedColumn()
714
id: number;
815

16+
/**
17+
* The total monetary amount associated with this record.
18+
*/
919
@Column({ type: 'decimal', precision: 10, scale: 2 })
1020
amount: number;
1121

22+
/**
23+
* The date associated with this entry.
24+
*/
1225
@Column({ type: 'timestamp' })
1326
date: Date;
1427

28+
/**
29+
* Name of the customer or source entity.
30+
*/
1531
@Column()
1632
customer: string;
1733

34+
/**
35+
* Optional description or metadata notes.
36+
*/
1837
@Column({ type: 'text', nullable: true })
1938
description?: string;
2039

40+
/**
41+
* Risk assessment score (0-100).
42+
*/
2143
@Column({ type: 'int', default: 0 })
2244
riskScore: number;
2345

46+
/**
47+
* Current processing status.
48+
*/
2449
@Column({
2550
type: 'varchar',
2651
default: 'Pending',
2752
enum: ['Pending', 'Approved', 'High Risk', 'Rejected']
2853
})
2954
status: string;
3055

56+
/**
57+
* Timestamp of when this entry was processed.
58+
*/
3159
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
3260
processedAt: Date;
3361

62+
/**
63+
* The user associated with this entry.
64+
*/
3465
@ManyToOne(() => User, { nullable: true })
3566
@JoinColumn({ name: 'userId' })
3667
user?: User;
3768

69+
/**
70+
* Foreign key reference to the user.
71+
*/
3872
@Column({ nullable: true })
3973
userId?: string;
4074

75+
/**
76+
* Record creation timestamp.
77+
*/
4178
@CreateDateColumn()
4279
createdAt: Date;
4380

81+
/**
82+
* Record update timestamp.
83+
*/
4484
@UpdateDateColumn()
4585
updatedAt: Date;
4686
}

src/health/health.controller.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import { Controller, Get } from '@nestjs/common';
22
import { HealthService } from './health.service';
33

4+
/**
5+
* Controller for protocol health and monitoring endpoints.
6+
* Provides public status information about system components.
7+
*/
48
@Controller('api/v1/protocol')
59
export class HealthController {
610
constructor(private readonly healthService: HealthService) {}
711

12+
/**
13+
* Retrieves the current operational status of the protocol.
14+
*
15+
* @returns A detailed health report including database and indexer status.
16+
*/
817
@Get('health')
918
async getHealth() {
1019
return this.healthService.getProtocolHealth();

src/health/health.service.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
import { Injectable } from '@nestjs/common';
1+
import { Injectable } from '@nestjs/common';
22
import { PrismaService } from '../prisma/prisma.service';
33

4+
/**
5+
* Service responsible for monitoring the operational health of the protocol.
6+
* Checks database connectivity, indexer latency, and pool availability.
7+
*/
48
@Injectable()
59
export class HealthService {
610
constructor(private readonly prisma: PrismaService) {}
711

12+
/**
13+
* Performs a comprehensive health check across all core system components.
14+
*
15+
* @returns An object containing the overall status, timestamp, and detailed component metrics.
16+
*/
817
async getProtocolHealth() {
918
const timestamp = new Date().toISOString();
1019

src/risk/risk.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { Module } from '@nestjs/common';
22
import { RiskService } from './risk.service';
33

4+
/**
5+
* Module responsible for financial risk assessment.
6+
* Exports the RiskService for use in other parts of the application.
7+
*/
48
@Module({
59
providers: [RiskService],
610
exports: [RiskService],

0 commit comments

Comments
 (0)