@@ -2,17 +2,17 @@ import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
22import { Admin , ITopicConfig , Kafka , KafkaConfig } from 'kafkajs' ;
33import { Logger } from '@nestjs/common/services/logger.service' ;
44import { ConfigService } from '@nestjs/config' ;
5- import { KAFKA_TOPICS , KAFKA_TOPICS_REPLY } from '../constants/kafka.constant' ;
65import { IKafkaAdminService } from 'src/kafka/interfaces/kafka.admin-service.interface' ;
6+ import { IKafkaCreateTopic } from 'src/kafka/interfaces/kafka.interface' ;
7+ import { KafkaCreateTopis } from 'src/kafka/constants/kafka.constant' ;
78
89@Injectable ( )
910export class KafkaAdminService
1011 implements IKafkaAdminService , OnModuleInit , OnModuleDestroy
1112{
1213 private readonly kafka : Kafka ;
1314 private readonly admin : Admin ;
14- private readonly topics : string [ ] ;
15- private readonly topicsReply : string [ ] ;
15+ private readonly topics : IKafkaCreateTopic [ ] ;
1616 private readonly brokers : string [ ] ;
1717 private readonly clientId : string ;
1818 private readonly kafkaOptions : KafkaConfig ;
@@ -24,8 +24,7 @@ export class KafkaAdminService
2424 this . clientId = this . configService . get < string > ( 'kafka.admin.clientId' ) ;
2525 this . brokers = this . configService . get < string [ ] > ( 'kafka.brokers' ) ;
2626
27- this . topics = KAFKA_TOPICS ;
28- this . topicsReply = KAFKA_TOPICS_REPLY ;
27+ this . topics = KafkaCreateTopis ;
2928
3029 this . kafkaOptions = {
3130 clientId : this . clientId ,
@@ -74,27 +73,30 @@ export class KafkaAdminService
7473
7574 async createTopics ( ) : Promise < boolean > {
7675 this . logger . log ( `Topics ${ this . topics } ` ) ;
77- this . logger . log ( `Topics Reply ${ this . topicsReply } ` ) ;
7876
7977 const currentTopic : string [ ] = await this . getAllTopicUnique ( ) ;
8078 const data : ITopicConfig [ ] = [ ] ;
8179
8280 for ( const topic of this . topics ) {
83- if ( ! currentTopic . includes ( topic ) ) {
81+ const partition : number = topic . partition ?? this . defaultPartition ;
82+ const replicationFactor : number =
83+ topic . replicationFactor &&
84+ topic . replicationFactor <= this . brokers . length
85+ ? topic . replicationFactor
86+ : this . brokers . length ;
87+ if ( ! currentTopic . includes ( topic . topic ) ) {
8488 data . push ( {
85- topic,
86- numPartitions : this . defaultPartition ,
87- replicationFactor : this . brokers . length ,
89+ topic : topic . topic ,
90+ numPartitions : partition ,
91+ replicationFactor : replicationFactor ,
8892 } ) ;
8993 }
90- }
9194
92- for ( const replyTopic of this . topicsReply ) {
93- if ( ! currentTopic . includes ( replyTopic ) ) {
95+ if ( ! currentTopic . includes ( topic . topicReply ) ) {
9496 data . push ( {
95- topic : replyTopic ,
96- numPartitions : this . defaultPartition ,
97- replicationFactor : this . brokers . length ,
97+ topic : topic . topicReply ,
98+ numPartitions : partition ,
99+ replicationFactor,
98100 } ) ;
99101 }
100102 }
@@ -117,14 +119,12 @@ export class KafkaAdminService
117119 const data = [ ] ;
118120
119121 for ( const topic of this . topics ) {
120- if ( currentTopic . includes ( topic ) ) {
121- data . push ( topic ) ;
122+ if ( currentTopic . includes ( topic . topic ) ) {
123+ data . push ( topic . topic ) ;
122124 }
123- }
124125
125- for ( const replyTopic of this . topicsReply ) {
126- if ( currentTopic . includes ( replyTopic ) ) {
127- data . push ( replyTopic ) ;
126+ if ( currentTopic . includes ( topic . topicReply ) ) {
127+ data . push ( topic . topicReply ) ;
128128 }
129129 }
130130
0 commit comments