Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
532 changes: 484 additions & 48 deletions README.md

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions apps/anti-fraud-service/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "anti-fraud-service",
"version": "1.0.0",
"description": "Anti-Fraud Validation Service",
"scripts": {
"start": "nest start",
"start:dev": "nest start --watch",
"build": "nest build"
}
}
17 changes: 17 additions & 0 deletions apps/anti-fraud-service/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import configuration from './config/configuration';
import { FraudDetectionModule } from './modules/fraud-detection/fraud-detection.module';
import { HealthController } from './health.controller';

@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [configuration],
}),
FraudDetectionModule,
],
controllers: [HealthController],
})
export class AppModule {}
8 changes: 8 additions & 0 deletions apps/anti-fraud-service/src/config/configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default () => ({
port: parseInt(process.env.PORT, 10) || 3001,
kafka: {
brokers: process.env.KAFKA_BROKERS || 'localhost:9092',
clientId: 'anti-fraud-service',
groupId: 'anti-fraud-consumer',
},
});
13 changes: 13 additions & 0 deletions apps/anti-fraud-service/src/health.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Controller, Get } from '@nestjs/common';

@Controller('health')
export class HealthController {
@Get()
check() {
return {
status: 'ok',
service: 'anti-fraud-service',
timestamp: new Date().toISOString(),
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Injectable, OnModuleInit, OnModuleDestroy, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Kafka, Producer } from 'kafkajs';

@Injectable()
export class KafkaProducer implements OnModuleInit, OnModuleDestroy {
private readonly logger = new Logger(KafkaProducer.name);
private kafka: Kafka;
private producer: Producer;

constructor(private configService: ConfigService) {
this.kafka = new Kafka({
clientId: this.configService.get<string>('kafka.clientId'),
brokers: [this.configService.get<string>('kafka.brokers')],
});

this.producer = this.kafka.producer();
}

async onModuleInit() {
await this.producer.connect();
this.logger.log('Kafka producer connected');
}

async onModuleDestroy() {
await this.producer.disconnect();
this.logger.log('Kafka producer disconnected');
}

async send(topic: string, message: any): Promise<void> {
try {
await this.producer.send({
topic,
messages: [
{
key: message.data.transactionExternalId,
value: JSON.stringify(message),
headers: {
'correlation-id': message.correlationId,
'event-type': message.eventType,
},
},
],
});

this.logger.debug(`Message sent to topic ${topic}`);
} catch (error) {
this.logger.error(`Failed to send message to ${topic}`, error);
throw error;
}
}
}
16 changes: 16 additions & 0 deletions apps/anti-fraud-service/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { NestFactory } from '@nestjs/factory';
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import path '@nestjs/factory' is incorrect. It should be '@nestjs/core'. NestFactory is exported from '@nestjs/core', not '@nestjs/factory'. This will cause a runtime error when trying to start the anti-fraud service.

Suggested change
import { NestFactory } from '@nestjs/factory';
import { NestFactory } from '@nestjs/core';

Copilot uses AI. Check for mistakes.
import { Logger } from '@nestjs/common';
import { AppModule } from './app.module';

async function bootstrap() {
const logger = new Logger('AntiFraudService');
const app = await NestFactory.create(AppModule);

const port = process.env.PORT || 3001;
await app.listen(port);

logger.log(`Anti-Fraud Service is running on: http://localhost:${port}`);
logger.log(`Listening to Kafka topic: transaction.created`);
}

bootstrap();
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Injectable, OnModuleInit, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Kafka, Consumer } from 'kafkajs';
import { TransactionCreatedEvent } from '@yape/shared-types';
import { FraudDetectionService } from '../services/fraud-detection.service';

@Injectable()
export class TransactionCreatedConsumer implements OnModuleInit {
private readonly logger = new Logger(TransactionCreatedConsumer.name);
private kafka: Kafka;
private consumer: Consumer;

constructor(
private configService: ConfigService,
private fraudDetectionService: FraudDetectionService,
) {
this.kafka = new Kafka({
clientId: this.configService.get<string>('kafka.clientId'),
brokers: [this.configService.get<string>('kafka.brokers')],
});

this.consumer = this.kafka.consumer({
groupId: this.configService.get<string>('kafka.groupId'),
});
}

async onModuleInit() {
await this.consumer.connect();
await this.consumer.subscribe({
topic: 'transaction.created',
fromBeginning: false,
});

await this.consumer.run({
eachMessage: async ({ topic, partition, message }) => {
try {
const event: TransactionCreatedEvent = JSON.parse(
message.value.toString(),
);

this.logger.log(`Processing transaction ${event.data.transactionExternalId}`);

// Validar transacción
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments are written in Spanish, which violates the coding guideline requiring all code, comments, and variable names to be in English. Comments should be translated to English for consistency.

Copilot generated this review using guidance from organization custom instructions.
await this.fraudDetectionService.validateTransaction(event);
} catch (error) {
this.logger.error('Error processing message', error);
}
},
});

this.logger.log('Anti-fraud consumer started and listening to transaction.created');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { FraudDetectionService } from './services/fraud-detection.service';
import { TransactionCreatedConsumer } from './consumers/transaction-created.consumer';
import { KafkaProducer } from '../../infrastructure/messaging/kafka.producer';

@Module({
providers: [FraudDetectionService, TransactionCreatedConsumer, KafkaProducer],
})
export class FraudDetectionModule {}
Loading