-
Notifications
You must be signed in to change notification settings - Fork 468
SHIP: solution #572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sebastianperudev2001
wants to merge
1
commit into
yaperos:main
Choose a base branch
from
sebastianperudev2001:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
SHIP: solution #572
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(), | ||
| }; | ||
| } | ||
| } |
52 changes: 52 additions & 0 deletions
52
apps/anti-fraud-service/src/infrastructure/messaging/kafka.producer.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { NestFactory } from '@nestjs/factory'; | ||
| 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(); | ||
53 changes: 53 additions & 0 deletions
53
.../anti-fraud-service/src/modules/fraud-detection/consumers/transaction-created.consumer.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
||
| 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'); | ||
| } | ||
| } | ||
9 changes: 9 additions & 0 deletions
9
apps/anti-fraud-service/src/modules/fraud-detection/fraud-detection.module.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.