@mojaloop/central-services-shared 18.0.0 → 18.1.0-snapshot.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mojaloop/central-services-shared",
3
- "version": "18.0.0",
3
+ "version": "18.1.0-snapshot.0",
4
4
  "description": "Shared code for mojaloop central services",
5
5
  "license": "Apache-2.0",
6
6
  "author": "ModusBox",
@@ -182,12 +182,13 @@ const createParticipantTopicConf = (template, participantName, functionality, ac
182
182
  * @param {string} key - optional key that allows partitioning it occur
183
183
  * @param {number} partition - optional partition to produce to
184
184
  * @param {*} opaqueKey - optional opaque token, which gets passed along to your delivery reports
185
+ * @param {string} topicNameOverride - optional topic name override that skips topic name rendering
185
186
  *
186
187
  * @returns {object} - Returns newly created general topicConfig
187
188
  */
188
- const createGeneralTopicConf = (template, functionality, action, key = null, partition = null, opaqueKey = null) => {
189
+ const createGeneralTopicConf = (template, functionality, action, key = null, partition = null, opaqueKey = null, topicNameOverride = null) => {
189
190
  return {
190
- topicName: transformGeneralTopicName(template, functionality, action),
191
+ topicName: topicNameOverride || transformGeneralTopicName(template, functionality, action),
191
192
  key,
192
193
  partition,
193
194
  opaqueKey
@@ -223,13 +224,14 @@ const getFunctionalityAction = (functionality, action) => {
223
224
  * @param {object} state - state of the message being produced
224
225
  * @param {string} key - optional key that allows partitioning it occur
225
226
  * @param {object} span - the span for event logging
227
+ * @param {string} topicNameOverride - optional topic name override that skips topic name rendering
226
228
  *
227
229
  * @returns {object} - Returns a boolean: true if successful, or throws and error if failed
228
230
  */
229
- const produceGeneralMessage = async (defaultKafkaConfig, kafkaProducer, functionality, action, message, state, key = null, span = null) => {
231
+ const produceGeneralMessage = async (defaultKafkaConfig, kafkaProducer, functionality, action, message, state, key = null, span = null, topicNameOverride = null) => {
230
232
  const { functionalityMapped, actionMapped } = getFunctionalityAction(functionality, action)
231
233
  let messageProtocol = StreamingProtocol.updateMessageProtocolMetadata(message, functionality, action, state)
232
- const topicConfig = createGeneralTopicConf(defaultKafkaConfig.TOPIC_TEMPLATES.GENERAL_TOPIC_TEMPLATE.TEMPLATE, functionalityMapped, actionMapped, key)
234
+ const topicConfig = createGeneralTopicConf(defaultKafkaConfig.TOPIC_TEMPLATES.GENERAL_TOPIC_TEMPLATE.TEMPLATE, functionalityMapped, actionMapped, key, null, null, topicNameOverride)
233
235
  const kafkaConfig = getKafkaConfig(defaultKafkaConfig, Enum.Kafka.Config.PRODUCER, functionalityMapped.toUpperCase(), actionMapped.toUpperCase())
234
236
  if (span) {
235
237
  messageProtocol = await span.injectContextToMessage(messageProtocol)
@@ -285,7 +287,7 @@ const commitMessageSync = async (kafkaConsumer, kafkaTopic, message) => {
285
287
 
286
288
  const proceed = async (defaultKafkaConfig, params, opts) => {
287
289
  const { message, kafkaTopic, consumer, decodedPayload, span, producer } = params
288
- const { consumerCommit, fspiopError, eventDetail, fromSwitch, toDestination, messageKey } = opts
290
+ const { consumerCommit, fspiopError, eventDetail, fromSwitch, toDestination, messageKey, topicNameOverride } = opts
289
291
  let metadataState
290
292
 
291
293
  if (consumerCommit) {
@@ -310,7 +312,7 @@ const proceed = async (defaultKafkaConfig, params, opts) => {
310
312
  if (message.value.content.headers) message.value.content.headers[Enum.Http.Headers.FSPIOP.DESTINATION] = toDestination
311
313
  }
312
314
  if (eventDetail && producer) {
313
- await produceGeneralMessage(defaultKafkaConfig, producer, eventDetail.functionality, eventDetail.action, message.value, metadataState, messageKey, span)
315
+ await produceGeneralMessage(defaultKafkaConfig, producer, eventDetail.functionality, eventDetail.action, message.value, metadataState, messageKey, span, topicNameOverride)
314
316
  }
315
317
  return true
316
318
  }
@@ -234,6 +234,27 @@ Test('Utility Test', utilityTest => {
234
234
  }
235
235
  })
236
236
 
237
+ createGeneralTopicConfTest.test('return topic name override when specfied', test => {
238
+ const ModuleProxy = Proxyquire('../../../../src/util/kafka', {
239
+ '../../enums': {
240
+ topicMap: {
241
+ transfer: {
242
+ fulfil: {
243
+ functionality: 'transfer',
244
+ action: 'fulfil'
245
+ }
246
+ }
247
+ }
248
+ }
249
+ })
250
+ const response = ModuleProxy.createGeneralTopicConf(Config.KAFKA_CONFIG.TOPIC_TEMPLATES.GENERAL_TOPIC_TEMPLATE.TEMPLATE, TRANSFER, FULFIL, 0, null, null, 'topic-name-override')
251
+ test.equal(response.topicName, 'topic-name-override')
252
+ test.equal(response.key, 0)
253
+ test.equal(response.partition, null)
254
+ test.equal(response.opaqueKey, null)
255
+ test.end()
256
+ })
257
+
237
258
  createGeneralTopicConfTest.end()
238
259
  })
239
260
 
@@ -294,6 +315,25 @@ Test('Utility Test', utilityTest => {
294
315
  test.end()
295
316
  })
296
317
 
318
+ produceGeneralMessageTest.test('produce a message with topic name override when specified', async (test) => {
319
+ const ModuleProxy = Proxyquire('../../../../src/util/kafka', {
320
+ '../../enums': {
321
+ topicMap: {
322
+ transfer: {
323
+ prepare: {
324
+ functionality: 'transfer',
325
+ action: 'prepare'
326
+ }
327
+ }
328
+ }
329
+ }
330
+ })
331
+ const result = await ModuleProxy.produceGeneralMessage(Config.KAFKA_CONFIG, KafkaProducer, TRANSFER, PREPARE, messageProtocol, Enum.Events.EventStatus.SUCCESS, null, null, 'topic-name-override')
332
+ test.equal(KafkaProducer.produceMessage.getCall(0).args[1].topicName, 'topic-name-override')
333
+ test.equal(result, true)
334
+ test.end()
335
+ })
336
+
297
337
  produceGeneralMessageTest.test('produce a notification message using topicMap', async (test) => {
298
338
  const ModuleProxy = Proxyquire('../../../../src/util/kafka', {
299
339
  '../../enums': {
@@ -529,6 +569,20 @@ Test('Utility Test', utilityTest => {
529
569
  test.end()
530
570
  })
531
571
 
572
+ proceedTest.test('produce message when topicNameOverride specified', async test => {
573
+ const opts = { consumerCommit: true, eventDetail, messageKey: '101', topicNameOverride: 'topic-name-override' }
574
+ try {
575
+ const result = await UtilityProxy.proceed(Config.KAFKA_CONFIG, params, opts)
576
+ test.ok(commitMessageSyncStub.calledOnce, 'commitMessageSyncStub not called')
577
+ test.ok(produceGeneralMessageStub.withArgs(Config.KAFKA_CONFIG, producer, eventDetail.functionality, eventDetail.action, message.value, successState, '101', undefined, 'topic-name-override').calledOnce, 'produceGeneralMessageStub not called')
578
+ test.equal(result, true, 'result returned')
579
+ } catch (err) {
580
+ test.fail(err.message)
581
+ }
582
+
583
+ test.end()
584
+ })
585
+
532
586
  proceedTest.test('produce fromSwitch and do not stop timer', async test => {
533
587
  const opts = { fromSwitch: true, eventDetail }
534
588
  try {