forked from yaperos/app-nodejs-codechallenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrated Kafka: implemented message production and consumption for …
…specific topics
- Loading branch information
1 parent
35948c5
commit 867a318
Showing
14 changed files
with
179 additions
and
70 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
11 changes: 11 additions & 0 deletions
11
apps/transactions/src/domain/dtos/kafkaTransactionMessage.dto.ts
This file contains 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,11 @@ | ||
export interface KafkaTransactionMessage { | ||
transactionExternalId: string; | ||
transactionType: { | ||
name: number; | ||
}; | ||
transactionStatus: { | ||
name: string; | ||
}; | ||
value: number; | ||
createdAt: Date; | ||
} |
This file contains 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 |
---|---|---|
|
@@ -7,5 +7,5 @@ export interface UpdatedData { | |
name: string; | ||
}; | ||
value: number; | ||
createdAt: Date; | ||
createdAt: string; | ||
} |
34 changes: 34 additions & 0 deletions
34
apps/transactions/src/infrastructure/kafka/kafkaConsumer.ts
This file contains 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,34 @@ | ||
import { Kafka } from 'kafkajs'; | ||
import dotenv from 'dotenv'; | ||
import { TransactionRepositoryImpl } from '../../domain/repositories/transaction.repository'; | ||
import { TransactionController } from '../../adapters/controllers/transaction.controller'; | ||
|
||
dotenv.config(); | ||
|
||
const kafka = new Kafka({ | ||
clientId: process.env.KAFKA_CLIENT_ID, | ||
brokers: [process.env.KAFKA_BROKER || 'localhost:9092'], | ||
}); | ||
|
||
const consumer = kafka.consumer({ groupId: process.env.KAFKA_GROUP_ID as string }); | ||
const transactionRepository = new TransactionRepositoryImpl(); | ||
const transactionController = new TransactionController(transactionRepository) | ||
|
||
export const consumeTransactionMessages = async () => { | ||
await consumer.connect(); | ||
await consumer.subscribe({ topic: process.env.KAFKA_TOPIC_UPDATE || 'antifraud-transactions-status', fromBeginning: true }); | ||
|
||
await consumer.run({ | ||
eachMessage: async ({ topic, partition, message }) => { | ||
const transactionData = JSON.parse(message.value?.toString() || '{}'); | ||
console.log(`Mensaje recibido en el tópico ${topic} - Partición ${partition}:`, transactionData); | ||
|
||
try { | ||
await transactionController.updateTransaction(transactionData); | ||
console.log("mensaje procesado exitosamente"); | ||
} catch (error) { | ||
console.error(`Error al procesar la transacción en el tópico ${topic} - message ${message}: `, error); | ||
} | ||
}, | ||
}); | ||
}; |
16 changes: 16 additions & 0 deletions
16
apps/transactions/src/infrastructure/mappers/transaction.mapper.ts
This file contains 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 { Transaction } from "../../domain/entities/transaction.entity"; | ||
import { KafkaTransactionMessage } from "../../domain/dtos/kafkaTransactionMessage.dto"; | ||
|
||
export const mapTransactionToKafkaMessage = (transaction: Transaction): KafkaTransactionMessage => { | ||
return { | ||
transactionExternalId: transaction.id, | ||
transactionType: { | ||
name: transaction.transferTypeId, | ||
}, | ||
transactionStatus: { | ||
name: transaction.status, | ||
}, | ||
value: transaction.value, | ||
createdAt: transaction.createdAt, | ||
}; | ||
}; |
33 changes: 33 additions & 0 deletions
33
apps/transactions/src/infrastructure/messageBroker/kafkaProducer.ts
This file contains 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,33 @@ | ||
import { Kafka } from 'kafkajs'; | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
const kafka = new Kafka({ | ||
clientId: 'transactions-service', | ||
brokers: [process.env.KAFKA_BROKER || 'localhost:9092'], | ||
}); | ||
|
||
const producer = kafka.producer(); | ||
|
||
|
||
export const sendTransactionMessage = async (transactionData: any) => { | ||
await producer.connect(); | ||
|
||
try { | ||
await producer.send({ | ||
topic: process.env.KAFKA_TOPIC || 'transactions', | ||
messages: [ | ||
{ | ||
value: JSON.stringify(transactionData), | ||
}, | ||
], | ||
}); | ||
console.log('Transaction message sent:', transactionData); | ||
} catch (error) { | ||
console.error('Error sending transaction message:', error); | ||
} finally { | ||
await producer.disconnect(); | ||
} | ||
}; | ||
|
This file contains 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,18 @@ | ||
import AppDataSource from "./database/transaction.ormconfig"; | ||
import { consumeTransactionMessages } from "./kafka/kafkaConsumer"; | ||
|
||
|
||
export const initializeApp = async () => { | ||
try { | ||
AppDataSource.initialize() | ||
.then(() => { | ||
console.log('Conexión a la base de datos establecida'); | ||
}) | ||
.catch((error) => console.log('Error al conectar a la base de datos', error)); | ||
|
||
await consumeTransactionMessages(); | ||
} catch (error) { | ||
console.error('Error al inicializar la aplicación:', error); | ||
process.exit(1); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,21 +1,23 @@ | ||
import express from 'express'; | ||
import transactionRoutes from './adapters/routes/transaction.routes'; | ||
import 'reflect-metadata'; | ||
import AppDataSource from './infrastructure/database/transaction.ormconfig'; | ||
import { initializeApp } from './infrastructure/startup'; | ||
|
||
const app = express(); | ||
const startServer = async () => { | ||
await initializeApp() | ||
} | ||
|
||
app.use(express.json()); | ||
|
||
app.use('/api/transactions', transactionRoutes); | ||
|
||
AppDataSource.initialize() | ||
.then(() => { | ||
console.log('Conexión a la base de datos establecida'); | ||
startServer().catch((err) => { | ||
console.error('Error al iniciar el servidor:', err); | ||
process.exit(1); | ||
}); | ||
|
||
app.listen(3000, () => { | ||
console.log('Servidor ejecutándose en http://localhost:3000'); | ||
}); | ||
}) | ||
.catch((error) => console.log('Error al conectar a la base de datos', error)); | ||
app.listen(3000, () => { | ||
console.log('Servidor ejecutándose en http://localhost:3000'); | ||
}); | ||
|
This file contains 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
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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