@mojaloop/central-ledger 9.2.2 → 15.1.3-snapshot.2

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.
Files changed (256) hide show
  1. package/.circleci/config.yml +625 -204
  2. package/.env +3 -2
  3. package/.ncurc.yaml +9 -0
  4. package/.nvmrc +1 -1
  5. package/.versionrc +15 -0
  6. package/CHANGELOG.md +353 -0
  7. package/CODEOWNERS +31 -0
  8. package/Dockerfile +20 -14
  9. package/LICENSE.md +4 -4
  10. package/Onboarding.md +0 -7
  11. package/README.md +99 -22
  12. package/audit-resolve.json +136 -1
  13. package/config/default.json +82 -27
  14. package/docker/central-ledger/default.json +68 -21
  15. package/docker/ml-api-adapter/default.json +10 -1
  16. package/docker/wait-for/wait-for-objstore.sh +1 -6
  17. package/docker/wait-for/wait-for.env +1 -1
  18. package/docker-compose.integration.yml +8 -5
  19. package/docker-compose.yml +73 -16
  20. package/migrations/310203_transferParticipant-indexes.js +38 -0
  21. package/migrations/310503_participantLimit-indexes-composite.js +38 -0
  22. package/migrations/{400401_settlementWindowStateChange-indexes.js → 400401_settlmentWindowStateChange-indexes.js} +0 -0
  23. package/migrations/401301_settlementModel-indexes.js +2 -0
  24. package/migrations/500401_quote-hotfix-2719.js +58 -0
  25. package/migrations/500501_feature-fixSubIdRef.js +53 -0
  26. package/migrations/500601_party-2480.js +63 -0
  27. package/migrations/501001_quoteExtension-quote-service-174.js +46 -0
  28. package/migrations/501002_quoteExtension-2522.js +61 -0
  29. package/migrations/800101_feature-fixSubIdRef.js +90 -0
  30. package/migrations/910102_hotfix954.js +4 -1
  31. package/migrations/940100_quotePartyIdInfoExtension.js +52 -0
  32. package/migrations/940101_quotePartyIdInfoExtension-2522.js +64 -0
  33. package/migrations/950100_settlementContentAggregation-decimal184.js +40 -0
  34. package/migrations/950101_transferParticipantStateChange.js +46 -0
  35. package/migrations/950102_settlementModel-adjustPosition.js +42 -0
  36. package/migrations/950103_dropTransferParticipantStateChange.js +47 -0
  37. package/migrations/950104_settlementModel-settlementAccountTypeId.js +79 -0
  38. package/migrations/950106_unique_settlement_model_ledger_account_currency.js +38 -0
  39. package/migrations/950107_settlementWindowContent_foreign_settlementModel.js +60 -0
  40. package/package.json +99 -81
  41. package/scripts/_wait4_all.js +146 -0
  42. package/seeds/amountType.js +4 -6
  43. package/seeds/balanceOfPayments.js +4 -6
  44. package/seeds/bulkProcessingState.js +4 -6
  45. package/seeds/bulkTransferState.js +4 -6
  46. package/seeds/currency.js +4 -6
  47. package/seeds/endpointType.js +128 -7
  48. package/seeds/ledgerAccountType.js +11 -10
  49. package/seeds/ledgerEntryType.js +5 -7
  50. package/seeds/participant.js +4 -6
  51. package/seeds/participantLimitType.js +4 -6
  52. package/seeds/partyIdentifierType.js +4 -6
  53. package/seeds/partyType.js +4 -6
  54. package/seeds/settlementDelay.js +4 -6
  55. package/seeds/settlementGranularity.js +4 -6
  56. package/seeds/settlementInterchange.js +4 -6
  57. package/seeds/settlementState.js +4 -6
  58. package/seeds/settlementWindow1State.js +4 -6
  59. package/seeds/transactionInitiator.js +4 -6
  60. package/seeds/transactionInitiatorType.js +4 -6
  61. package/seeds/transactionScenario.js +4 -6
  62. package/seeds/transferParticipantRoleType.js +4 -6
  63. package/seeds/transferState.js +9 -6
  64. package/src/api/interface/swagger.json +728 -948
  65. package/src/api/ledgerAccountTypes/handler.js +55 -0
  66. package/src/api/ledgerAccountTypes/routes.js +63 -0
  67. package/src/api/metrics/handler.js +2 -2
  68. package/src/api/metrics/routes.js +1 -1
  69. package/src/api/participants/handler.js +43 -35
  70. package/src/api/participants/routes.js +13 -13
  71. package/src/api/root/routes.js +2 -2
  72. package/src/api/settlementModels/handler.js +9 -26
  73. package/src/api/settlementModels/routes.js +4 -4
  74. package/src/api/transactions/handler.js +46 -0
  75. package/src/api/transactions/routes.js +45 -0
  76. package/src/domain/bulkTransfer/index.js +6 -5
  77. package/src/domain/ledgerAccountTypes/index.js +58 -0
  78. package/src/domain/participant/index.js +55 -4
  79. package/src/domain/position/index.js +18 -2
  80. package/src/domain/settlement/index.js +39 -6
  81. package/src/domain/transactions/index.js +61 -0
  82. package/src/domain/transfer/index.js +22 -2
  83. package/src/domain/transfer/transform.js +3 -3
  84. package/src/handlers/admin/handler.js +24 -32
  85. package/src/handlers/bulk/fulfil/handler.js +89 -56
  86. package/src/handlers/bulk/get/handler.js +203 -0
  87. package/src/handlers/bulk/index.js +4 -1
  88. package/src/handlers/bulk/prepare/handler.js +30 -29
  89. package/src/handlers/bulk/processing/handler.js +61 -32
  90. package/src/handlers/bulk/shared/validator.js +13 -1
  91. package/src/handlers/index.js +18 -9
  92. package/src/handlers/positions/handler.js +67 -46
  93. package/src/handlers/register.js +7 -7
  94. package/src/handlers/timeouts/handler.js +9 -4
  95. package/src/handlers/transfers/handler.js +408 -182
  96. package/src/handlers/transfers/validator.js +41 -11
  97. package/src/lib/cache.js +8 -34
  98. package/src/lib/config.js +5 -7
  99. package/src/lib/enum.js +22 -22
  100. package/src/lib/enumCached.js +76 -0
  101. package/src/lib/healthCheck/subServiceHealth.js +2 -2
  102. package/src/lib/requestLogger.js +9 -9
  103. package/src/lib/urlParser.js +1 -1
  104. package/src/models/bulkTransfer/bulkTransfer.js +6 -6
  105. package/src/models/bulkTransfer/bulkTransferAssociation.js +3 -3
  106. package/src/models/bulkTransfer/bulkTransferDuplicateCheck.js +4 -4
  107. package/src/models/bulkTransfer/bulkTransferExtension.js +2 -2
  108. package/src/models/bulkTransfer/bulkTransferFulfilmentDuplicateCheck.js +4 -4
  109. package/src/models/bulkTransfer/bulkTransferStateChange.js +7 -7
  110. package/src/models/bulkTransfer/facade.js +54 -3
  111. package/src/models/bulkTransfer/individualTransfer.js +2 -2
  112. package/src/models/ilpPackets/ilpPacket.js +36 -0
  113. package/src/models/ledgerAccountType/ledgerAccountType.js +139 -3
  114. package/src/models/misc/migrationLock.js +1 -1
  115. package/src/models/misc/segment.js +1 -1
  116. package/src/models/participant/facade.js +226 -76
  117. package/src/models/participant/participant.js +6 -5
  118. package/src/models/participant/participantCached.js +8 -0
  119. package/src/models/participant/participantCurrency.js +7 -7
  120. package/src/models/participant/participantCurrencyCached.js +11 -1
  121. package/src/models/participant/participantLimit.js +13 -13
  122. package/src/models/participant/participantLimitCached.js +124 -0
  123. package/src/models/participant/participantPosition.js +34 -5
  124. package/src/models/participant/participantPositionChange.js +10 -2
  125. package/src/models/position/facade.js +112 -18
  126. package/src/models/position/participantPosition.js +6 -6
  127. package/src/models/settlement/settlementModel.js +92 -17
  128. package/src/models/settlement/settlementModelCached.js +139 -0
  129. package/src/models/transfer/facade.js +97 -32
  130. package/src/models/transfer/ilpPacket.js +4 -4
  131. package/src/models/transfer/transfer.js +7 -7
  132. package/src/models/transfer/transferDuplicateCheck.js +21 -5
  133. package/src/models/transfer/transferError.js +5 -5
  134. package/src/models/transfer/transferErrorDuplicateCheck.js +4 -4
  135. package/src/models/transfer/transferExtension.js +4 -4
  136. package/src/models/transfer/transferFulfilment.js +5 -5
  137. package/src/models/transfer/transferFulfilmentDuplicateCheck.js +4 -4
  138. package/src/models/transfer/transferParticipant.js +3 -3
  139. package/src/models/transfer/transferStateChange.js +11 -11
  140. package/src/models/transfer/transferTimeout.js +5 -5
  141. package/src/schema/bulkTransfer.js +4 -4
  142. package/src/shared/plugins.js +10 -9
  143. package/src/shared/setup.js +69 -46
  144. package/test/integration/domain/participant/index.test.js +2 -0
  145. package/test/integration/handlers/handlers.test.js +605 -166
  146. package/test/integration/handlers/root.test.js +1 -1
  147. package/test/integration/helpers/hubAccounts.js +62 -0
  148. package/test/integration/helpers/ilpPacket.js +1 -1
  149. package/test/integration/helpers/participantFundsInOut.js +80 -0
  150. package/test/integration/helpers/participantLimit.js +6 -1
  151. package/test/integration/helpers/settlementModels.js +88 -0
  152. package/test/integration/helpers/testConsumer.js +163 -0
  153. package/test/integration/helpers/testProducer.js +0 -1
  154. package/test/integration/helpers/transferDuplicateCheck.js +1 -1
  155. package/test/integration/helpers/transferStateChange.js +1 -1
  156. package/test/integration/helpers/transferTestHelper.js +3 -5
  157. package/test/integration/models/transfer/transferStateChange.test.js +6 -0
  158. package/test/unit/api/ledgerAccountTypes/handler.test.js +126 -0
  159. package/test/unit/api/ledgerAccountTypes/routes.test.js +52 -0
  160. package/test/unit/api/participants/handler.test.js +75 -33
  161. package/test/unit/api/root/handler.test.js +1 -1
  162. package/test/unit/api/root/routes.test.js +2 -2
  163. package/test/unit/api/settlementModels/handler.test.js +21 -57
  164. package/test/unit/api/transactions/handler.test.js +108 -0
  165. package/test/unit/api/transactions/routes.test.js +38 -0
  166. package/test/unit/domain/ledgerAccountTypes/index.test.js +197 -0
  167. package/test/unit/domain/participant/index.test.js +115 -2
  168. package/test/unit/domain/settlement/index.test.js +199 -29
  169. package/test/unit/domain/transactions/index.test.js +128 -0
  170. package/test/unit/domain/transfer/index.test.js +1 -0
  171. package/test/unit/domain/transfer/transform.test.js +2 -0
  172. package/test/unit/handlers/admin/handler.test.js +5 -5
  173. package/test/unit/handlers/bulk/get/handler.test.js +401 -0
  174. package/test/unit/handlers/index.test.js +6 -0
  175. package/test/unit/handlers/positions/handler.test.js +26 -0
  176. package/test/unit/handlers/register.test.js +2 -0
  177. package/test/unit/handlers/transfers/handler.test.js +491 -36
  178. package/test/unit/handlers/transfers/validator.test.js +31 -31
  179. package/test/unit/lib/cache.test.js +0 -64
  180. package/test/unit/lib/cachingOfEnums.test.js +121 -0
  181. package/test/unit/lib/config.test.js +34 -0
  182. package/test/unit/lib/enum.test.js +6 -1
  183. package/test/unit/lib/enumCached.test.js +82 -0
  184. package/test/unit/lib/healthCheck/subServiceHealth.test.js +2 -0
  185. package/test/unit/lib/requestLogger.test.js +14 -12
  186. package/test/unit/models/ilpPackets/ilpPacket.test.js +89 -0
  187. package/test/unit/models/ledgerAccountType/ledgerAccountType.test.js +255 -6
  188. package/test/unit/models/misc/migrationLock.test.js +4 -0
  189. package/test/unit/models/misc/segment.test.js +5 -0
  190. package/test/unit/models/participant/facade.test.js +493 -54
  191. package/test/unit/models/participant/participant.test.js +4 -0
  192. package/test/unit/models/participant/participantCurrency.test.js +22 -9
  193. package/test/unit/models/participant/participantCurrencyCached.test.js +1 -1
  194. package/test/unit/models/participant/participantLimit.test.js +97 -0
  195. package/test/unit/models/participant/participantLimitCached.test.js +232 -0
  196. package/test/unit/models/participant/participantPosition.test.js +217 -0
  197. package/test/unit/models/participant/participantPositionChange.test.js +3 -0
  198. package/test/unit/models/position/facade.test.js +318 -29
  199. package/test/unit/models/position/participantPosition.test.js +22 -7
  200. package/test/unit/models/settlement/settlementModel.test.js +18 -37
  201. package/test/unit/models/settlement/settlementModelCached.test.js +171 -0
  202. package/test/unit/models/transfer/facade.test.js +115 -0
  203. package/test/unit/models/transfer/ilpPacket.test.js +28 -11
  204. package/test/unit/models/transfer/transfer.test.js +13 -4
  205. package/test/unit/models/transfer/transferDuplicateCheck.test.js +4 -0
  206. package/test/unit/models/transfer/transferError.test.js +5 -1
  207. package/test/unit/models/transfer/transferErrorDuplicateCheck.test.js +4 -0
  208. package/test/unit/models/transfer/transferExtension.test.js +26 -9
  209. package/test/unit/models/transfer/transferFulfilment.test.js +14 -5
  210. package/test/unit/models/transfer/transferFulfilmentDuplicateCheck.test.js +4 -0
  211. package/test/unit/models/transfer/transferParticipant.test.js +14 -5
  212. package/test/unit/models/transfer/transferStateChange.test.js +3 -0
  213. package/test/unit/models/transfer/transferTimeout.test.js +6 -0
  214. package/test/unit/seeds/amountType.test.js +6 -22
  215. package/test/unit/seeds/balanceOfPayments.test.js +6 -22
  216. package/test/unit/seeds/bulkProcessingState.test.js +84 -0
  217. package/test/unit/{lib/sidecar/nullClient.test.js → seeds/bulkTransferState.test.js} +36 -39
  218. package/test/unit/seeds/currency.test.js +6 -22
  219. package/test/unit/seeds/endpointType.test.js +6 -22
  220. package/test/unit/seeds/ledgerAccountType.test.js +5 -22
  221. package/test/unit/seeds/ledgerEntryType.test.js +6 -22
  222. package/test/unit/seeds/participant.test.js +5 -22
  223. package/test/unit/seeds/participantLimitType.test.js +6 -22
  224. package/test/unit/seeds/partyIdentifierType.test.js +6 -22
  225. package/test/unit/seeds/partyType.test.js +6 -22
  226. package/test/unit/seeds/settlementState.test.js +5 -22
  227. package/test/unit/seeds/settlementWindow1State.test.js +5 -22
  228. package/test/unit/seeds/transactionInitiator.test.js +6 -22
  229. package/test/unit/seeds/transactionInitiatorType.test.js +6 -22
  230. package/test/unit/seeds/transactionScenario.test.js +6 -22
  231. package/test/unit/seeds/transferParticipantRoleType.test.js +6 -22
  232. package/test/unit/seeds/transferState.test.js +6 -22
  233. package/test/unit/shared/plugins.test.js +31 -1
  234. package/test/unit/shared/setup.test.js +25 -36
  235. package/test/util/helpers.js +37 -2
  236. package/test/util/randomTransfers.js +1 -1
  237. package/test/util/scripts/env.sh +6 -5
  238. package/test/util/scripts/populateTestData.sh +204 -181
  239. package/test/util/scripts/populateTestDataForLegacySimulator.sh +261 -0
  240. package/test/util/scripts/restartObjStore.sh +1 -1
  241. package/test-integration.Dockerfile +15 -15
  242. package/test.Dockerfile +12 -12
  243. package/.circleci/_set_up_deploy_envs.sh +0 -47
  244. package/.dockerignore +0 -17
  245. package/.ncurc.json +0 -3
  246. package/server.sh +0 -4
  247. package/sonar-project.properties +0 -17
  248. package/src/lib/sidecar/index.js +0 -47
  249. package/src/lib/sidecar/nullClient.js +0 -18
  250. package/test/.env +0 -8
  251. package/test/integration-config.json +0 -367
  252. package/test/integration-runner.env +0 -31
  253. package/test/integration-runner.sh +0 -264
  254. package/test/spec-runner.sh +0 -132
  255. package/test/unit/lib/sidecar/index.test.js +0 -148
  256. package/test/unit/models/position/participantLimit.test.js +0 -135
@@ -30,7 +30,9 @@ const Db = require('../../../../src/lib/db')
30
30
  const Logger = require('@mojaloop/central-services-logger')
31
31
  const ModelParticipant = require('../../../../src/models/participant/facade')
32
32
  const ModelPosition = require('../../../../src/models/position/facade')
33
+ const SettlementModelCached = require('../../../../src/models/settlement/settlementModelCached')
33
34
  const Enum = require('@mojaloop/central-services-shared').Enum
35
+ const MainUtil = require('@mojaloop/central-services-shared').Util
34
36
 
35
37
  Test('Position facade', async (positionFacadeTest) => {
36
38
  let sandbox
@@ -47,6 +49,11 @@ Test('Position facade', async (positionFacadeTest) => {
47
49
  Db.participantPosition = {
48
50
  query: sandbox.stub()
49
51
  }
52
+
53
+ Db.from = (table) => {
54
+ return Db[table]
55
+ }
56
+
50
57
  t.end()
51
58
  })
52
59
 
@@ -250,17 +257,41 @@ Test('Position facade', async (positionFacadeTest) => {
250
257
  thresholdAlarmPercentage: 0.5
251
258
  }
252
259
 
253
- const initialParticipantPosition = {
260
+ const insufficientParticipantLimit = {
261
+ participantCurrencyId: 1,
262
+ participantLimitTypeId: 1,
263
+ value: 100,
264
+ isActive: 1,
265
+ createdBy: 'unknown',
266
+ participantLimitId: 1,
267
+ thresholdAlarmPercentage: 0.5
268
+ }
269
+
270
+ const initialParticipantPositions = [{
254
271
  participantPositionId: 1,
272
+ participantCurrencyId: 1,
255
273
  value: 1000,
256
274
  reservedValue: 0
257
- }
275
+ },
276
+ {
277
+ participantPositionId: 2,
278
+ participantCurrencyId: 2,
279
+ value: -10000,
280
+ reservedValue: 0
281
+ }]
258
282
 
259
- const exceededParticipantPosition = {
283
+ const exceededParticipantPositions = [{
260
284
  participantPositionId: 1,
285
+ participantCurrencyId: 1,
261
286
  value: 10000,
262
287
  reservedValue: 0
263
- }
288
+ },
289
+ {
290
+ participantPositionId: 2,
291
+ participantCurrencyId: 2,
292
+ value: -1000,
293
+ reservedValue: 0
294
+ }]
264
295
 
265
296
  await prepareChangeParticipantPositionTransaction.test('adjust position of payer when transfer is RESERVED', async test => {
266
297
  // const listOfTransferStatesChanged = [transferStateChange, incorrectTransferStateChange]
@@ -286,25 +317,54 @@ Test('Position facade', async (positionFacadeTest) => {
286
317
  }),
287
318
  where: sandbox.stub().returns({
288
319
  update: sandbox.stub().returns(Promise.resolve()),
289
- forUpdate: sandbox.stub().returns({
290
- select: sandbox.stub().returns({
291
- first: sandbox.stub().returns(initialParticipantPosition)
292
- })
293
- }),
294
320
  orderBy: sandbox.stub().returns({
295
321
  first: sandbox.stub().resolves(Object.assign({}, transferStateChange))
296
322
  })
323
+ }),
324
+ whereIn: sandbox.stub().returns({
325
+ forUpdate: sandbox.stub().returns({
326
+ select: sandbox.stub().returns(initialParticipantPositions)
327
+ })
297
328
  })
298
329
  })
299
330
  })
300
331
 
301
332
  sandbox.stub(ModelParticipant, 'getParticipantLimitByParticipantCurrencyLimit').returns(Promise.resolve(participantLimit))
302
- sandbox.stub(ModelParticipant, 'getByNameAndCurrency').resolves({
333
+ const getByNameAndCurrencyStub = sandbox.stub(ModelParticipant, 'getByNameAndCurrency')
334
+
335
+ const allSettlementModels = [{
336
+ settlementModelId: 1,
337
+ name: 'DEFERREDNET',
338
+ isActive: 1,
339
+ settlementGranularityId: 2,
340
+ settlementInterchangeId: 2,
341
+ settlementDelayId: 2,
342
+ currencyId: null,
343
+ requireLiquidityCheck: 1,
344
+ ledgerAccountTypeId: 1,
345
+ autoPositionReset: 1,
346
+ adjustPosition: 0,
347
+ settlementAccountTypeId: 2
348
+ }]
349
+
350
+ sandbox.stub(SettlementModelCached, 'getAll').resolves(allSettlementModels)
351
+
352
+ getByNameAndCurrencyStub.withArgs('dfsp1', 'USD', 1).resolves({
303
353
  participantCurrencyId: 1,
304
354
  participantId: 1,
305
355
  currencyId: 'USD',
306
356
  isActive: 1
307
357
  })
358
+ getByNameAndCurrencyStub.withArgs('dfsp1', 'USD', 2).resolves({
359
+ participantCurrencyId: 2,
360
+ participantId: 1,
361
+ currencyId: 'USD',
362
+ isActive: 1
363
+ })
364
+ sandbox.stub(SettlementModelCached, 'getByLedgerAccountTypeId').resolves({
365
+ settlementDelayId: Enum.Settlements.SettlementDelay.DEFERRED,
366
+ settlementAccountTypeId: Enum.Accounts.LedgerAccountType.SETTLEMENT
367
+ })
308
368
  const { preparedMessagesList, limitAlarms } = await ModelPosition.prepareChangeParticipantPositionTransaction([{ value: messageProtocol }])
309
369
  test.ok(Array.isArray(preparedMessagesList), 'array of prepared transfers is returned')
310
370
  test.ok(Array.isArray(limitAlarms), 'array of limit alarms is returned')
@@ -320,6 +380,87 @@ Test('Position facade', async (positionFacadeTest) => {
320
380
  }
321
381
  })
322
382
 
383
+ await prepareChangeParticipantPositionTransaction.test('Should throw', async test => {
384
+ try {
385
+ sandbox.stub(Db, 'getKnex')
386
+ const knexStub = sandbox.stub()
387
+ const trxStub = sandbox.stub()
388
+
389
+ trxStub.commit = sandbox.stub()
390
+ knexStub.transaction = sandbox.stub().callsArgWith(0, trxStub)
391
+ knexStub.batchInsert = sandbox.stub()
392
+ knexStub.batchInsert.returns({
393
+ transacting: sandbox.stub().resolves([1])
394
+ })
395
+
396
+ Db.getKnex.returns(knexStub)
397
+ knexStub.returns({
398
+ transacting: sandbox.stub().returns({
399
+ forUpdate: sandbox.stub().returns({
400
+ whereIn: sandbox.stub().returns({
401
+ select: sandbox.stub().returns(Promise.resolve())
402
+ })
403
+ }),
404
+ where: sandbox.stub().returns({
405
+ update: sandbox.stub().returns(Promise.resolve()),
406
+ orderBy: sandbox.stub().returns({
407
+ first: sandbox.stub().resolves(Object.assign({}, transferStateChange))
408
+ })
409
+ }),
410
+ whereIn: sandbox.stub().returns({
411
+ forUpdate: sandbox.stub().returns({
412
+ select: sandbox.stub().returns(initialParticipantPositions)
413
+ })
414
+ })
415
+ })
416
+ })
417
+
418
+ sandbox.stub(ModelParticipant, 'getParticipantLimitByParticipantCurrencyLimit').throws(new Error('Error'))
419
+ const getByNameAndCurrencyStub = sandbox.stub(ModelParticipant, 'getByNameAndCurrency')
420
+
421
+ const allSettlementModels = [{
422
+ settlementModelId: 1,
423
+ name: 'DEFERREDNET',
424
+ isActive: 1,
425
+ settlementGranularityId: 2,
426
+ settlementInterchangeId: 2,
427
+ settlementDelayId: 2,
428
+ currencyId: null,
429
+ requireLiquidityCheck: 1,
430
+ ledgerAccountTypeId: 1,
431
+ autoPositionReset: 1,
432
+ adjustPosition: 0,
433
+ settlementAccountTypeId: 2
434
+ }]
435
+
436
+ sandbox.stub(SettlementModelCached, 'getAll').resolves(allSettlementModels)
437
+
438
+ getByNameAndCurrencyStub.withArgs('dfsp1', 'USD', 1).resolves({
439
+ participantCurrencyId: 1,
440
+ participantId: 1,
441
+ currencyId: 'USD',
442
+ isActive: 1
443
+ })
444
+ getByNameAndCurrencyStub.withArgs('dfsp1', 'USD', 2).resolves({
445
+ participantCurrencyId: 2,
446
+ participantId: 1,
447
+ currencyId: 'USD',
448
+ isActive: 1
449
+ })
450
+ sandbox.stub(SettlementModelCached, 'getByLedgerAccountTypeId').resolves({
451
+ settlementDelayId: Enum.Settlements.SettlementDelay.DEFERRED,
452
+ settlementAccountTypeId: Enum.Accounts.LedgerAccountType.SETTLEMENT
453
+ })
454
+ await ModelPosition.prepareChangeParticipantPositionTransaction([{ value: messageProtocol }])
455
+ test.fail()
456
+
457
+ test.end()
458
+ } catch (err) {
459
+ test.pass('completed successfully')
460
+ test.end()
461
+ }
462
+ })
463
+
323
464
  await prepareChangeParticipantPositionTransaction.test('abort transfer if state is not correct ', async test => {
324
465
  // const listOfTransferStatesChanged = [transferStateChange, incorrectTransferStateChange]
325
466
  try {
@@ -345,26 +486,55 @@ Test('Position facade', async (positionFacadeTest) => {
345
486
  }),
346
487
  where: sandbox.stub().returns({
347
488
  update: sandbox.stub().returns(Promise.resolve()),
348
- forUpdate: sandbox.stub().returns({
349
- select: sandbox.stub().returns({
350
- first: sandbox.stub().returns(initialParticipantPosition)
351
- })
352
- }),
353
489
  orderBy: sandbox.stub().returns({
354
490
  first: sandbox.stub().resolves(incorrectTransferStateChange)
355
491
  })
492
+ }),
493
+ whereIn: sandbox.stub().returns({
494
+ forUpdate: sandbox.stub().returns({
495
+ select: sandbox.stub().returns(initialParticipantPositions)
496
+ })
356
497
  })
357
498
  })
358
499
  })
359
500
 
360
501
  sandbox.stub(ModelParticipant, 'getParticipantLimitByParticipantCurrencyLimit').returns(Promise.resolve(participantLimit))
361
- sandbox.stub(ModelParticipant, 'getByNameAndCurrency').resolves({
502
+ const getByNameAndCurrencyStub = sandbox.stub(ModelParticipant, 'getByNameAndCurrency')
503
+ getByNameAndCurrencyStub.withArgs('dfsp1', 'USD', 1).resolves({
362
504
  participantCurrencyId: 1,
363
505
  participantId: 1,
364
506
  currencyId: 'USD',
365
507
  isActive: 1
366
508
  })
367
- const { preparedMessagesList, limitAlarms } = await ModelPosition.prepareChangeParticipantPositionTransaction([{ value: messageProtocol }])
509
+ getByNameAndCurrencyStub.withArgs('dfsp1', 'USD', 2).resolves({
510
+ participantCurrencyId: 2,
511
+ participantId: 1,
512
+ currencyId: 'USD',
513
+ isActive: 1
514
+ })
515
+ sandbox.stub(SettlementModelCached, 'getByLedgerAccountTypeId').resolves({
516
+ settlementDelayId: Enum.Settlements.SettlementDelay.DEFERRED,
517
+ settlementAccountTypeId: Enum.Accounts.LedgerAccountType.SETTLEMENT
518
+ })
519
+
520
+ const allSettlementModels = [{
521
+ settlementModelId: 1,
522
+ name: 'DEFERREDNET',
523
+ isActive: 1,
524
+ settlementGranularityId: 2,
525
+ settlementInterchangeId: 2,
526
+ settlementDelayId: 2,
527
+ currencyId: null,
528
+ requireLiquidityCheck: 1,
529
+ ledgerAccountTypeId: 1,
530
+ autoPositionReset: 1,
531
+ adjustPosition: 0,
532
+ settlementAccountTypeId: 2
533
+ }]
534
+
535
+ sandbox.stub(SettlementModelCached, 'getAll').resolves(allSettlementModels)
536
+ const messageProtocolCopy = MainUtil.clone(messageProtocol)
537
+ const { preparedMessagesList, limitAlarms } = await ModelPosition.prepareChangeParticipantPositionTransaction([{ value: messageProtocolCopy }])
368
538
  test.ok(Array.isArray(preparedMessagesList), 'array of prepared transfers is returned')
369
539
  test.ok(Array.isArray(limitAlarms), 'array of limit alarms is returned')
370
540
  test.ok(knexStub.withArgs('participantPosition').calledThrice, 'knex called with participantPosition twice')
@@ -401,7 +571,7 @@ Test('Position facade', async (positionFacadeTest) => {
401
571
  }
402
572
  })
403
573
 
404
- await prepareChangeParticipantPositionTransaction.test('abort transfer if net-debit-cap is exceeded ', async test => {
574
+ await prepareChangeParticipantPositionTransaction.test('abort transfer if settlement balance is not enough ', async test => {
405
575
  // const listOfTransferStatesChanged = [transferStateChange, incorrectTransferStateChange]
406
576
  try {
407
577
  sandbox.stub(Db, 'getKnex')
@@ -426,28 +596,147 @@ Test('Position facade', async (positionFacadeTest) => {
426
596
  }),
427
597
  where: sandbox.stub().returns({
428
598
  update: sandbox.stub().returns(Promise.resolve()),
429
- forUpdate: sandbox.stub().returns({
430
- select: sandbox.stub().returns({
431
- first: sandbox.stub().returns(exceededParticipantPosition)
432
- })
433
- }),
434
599
  orderBy: sandbox.stub().returns({
435
- first: sandbox.stub().resolves(transferStateChange)
600
+ first: sandbox.stub().resolves(MainUtil.clone(transferStateChange))
601
+ })
602
+ }),
603
+ whereIn: sandbox.stub().returns({
604
+ forUpdate: sandbox.stub().returns({
605
+ select: sandbox.stub().returns(exceededParticipantPositions)
436
606
  })
437
607
  })
438
608
  })
439
609
  })
440
610
 
441
611
  sandbox.stub(ModelParticipant, 'getParticipantLimitByParticipantCurrencyLimit').returns(Promise.resolve(participantLimit))
442
- sandbox.stub(ModelParticipant, 'getByNameAndCurrency').resolves({
612
+ const getByNameAndCurrencyStub = sandbox.stub(ModelParticipant, 'getByNameAndCurrency')
613
+ getByNameAndCurrencyStub.withArgs('dfsp1', 'USD', 1).resolves({
443
614
  participantCurrencyId: 1,
444
615
  participantId: 1,
445
616
  currencyId: 'USD',
446
617
  isActive: 1
447
618
  })
448
- const { preparedMessagesList, limitAlarms } = await ModelPosition.prepareChangeParticipantPositionTransaction([{ value: messageProtocol }])
619
+ getByNameAndCurrencyStub.withArgs('dfsp1', 'USD', 2).resolves({
620
+ participantCurrencyId: 2,
621
+ participantId: 1,
622
+ currencyId: 'USD',
623
+ isActive: 1
624
+ })
625
+ sandbox.stub(SettlementModelCached, 'getByLedgerAccountTypeId').resolves({
626
+ settlementDelayId: Enum.Settlements.SettlementDelay.DEFERRED,
627
+ settlementAccountTypeId: Enum.Accounts.LedgerAccountType.SETTLEMENT
628
+ })
629
+
630
+ const allSettlementModels = [{
631
+ settlementModelId: 1,
632
+ name: 'DEFERREDNET',
633
+ isActive: 1,
634
+ settlementGranularityId: 2,
635
+ settlementInterchangeId: 2,
636
+ settlementDelayId: 2,
637
+ currencyId: null,
638
+ requireLiquidityCheck: 1,
639
+ ledgerAccountTypeId: 1,
640
+ autoPositionReset: 1,
641
+ adjustPosition: 0,
642
+ settlementAccountTypeId: 2
643
+ }]
644
+
645
+ sandbox.stub(SettlementModelCached, 'getAll').resolves(allSettlementModels)
646
+ const messageProtocolCopy = MainUtil.clone(messageProtocol)
647
+ const { preparedMessagesList, limitAlarms } = await ModelPosition.prepareChangeParticipantPositionTransaction([{ value: messageProtocolCopy }])
648
+ test.ok(Array.isArray(preparedMessagesList), 'array of prepared transfers is returned')
649
+ test.ok(Array.isArray(limitAlarms), 'array of limit alarms is returned')
650
+ test.ok(preparedMessagesList[0].rawMessage.value.content.payload.errorInformation.errorCode === '4001')
651
+ test.ok(knexStub.withArgs('participantPosition').calledThrice, 'knex called with participantPosition twice')
652
+ test.ok(knexStub.withArgs('transferStateChange').calledOnce, 'knex called with transferStateChange twice')
653
+ test.ok(knexStub.withArgs('transfer').calledOnce, 'knex called with transferStateChange twice')
654
+ test.pass('completed successfully')
655
+ test.end()
656
+ } catch (err) {
657
+ Logger.error(`prepareChangeParticipantPositionTransaction failed with error - ${err}`)
658
+ test.fail()
659
+ test.end()
660
+ }
661
+ })
662
+
663
+ await prepareChangeParticipantPositionTransaction.test('abort transfer if net-debit-cap is exceeded ', async test => {
664
+ // const listOfTransferStatesChanged = [transferStateChange, incorrectTransferStateChange]
665
+ try {
666
+ sandbox.stub(Db, 'getKnex')
667
+ const knexStub = sandbox.stub()
668
+ const trxStub = sandbox.stub()
669
+
670
+ trxStub.commit = sandbox.stub()
671
+ knexStub.transaction = sandbox.stub().callsArgWith(0, trxStub)
672
+
673
+ knexStub.batchInsert = sandbox.stub()
674
+ knexStub.batchInsert.returns({
675
+ transacting: sandbox.stub().resolves([1])
676
+ })
677
+
678
+ Db.getKnex.returns(knexStub)
679
+ knexStub.returns({
680
+ transacting: sandbox.stub().returns({
681
+ forUpdate: sandbox.stub().returns({
682
+ whereIn: sandbox.stub().returns({
683
+ select: sandbox.stub().returns(Promise.resolve())
684
+ })
685
+ }),
686
+ where: sandbox.stub().returns({
687
+ update: sandbox.stub().returns(Promise.resolve()),
688
+ orderBy: sandbox.stub().returns({
689
+ first: sandbox.stub().resolves(MainUtil.clone(transferStateChange))
690
+ })
691
+ }),
692
+ whereIn: sandbox.stub().returns({
693
+ forUpdate: sandbox.stub().returns({
694
+ select: sandbox.stub().returns(initialParticipantPositions)
695
+ })
696
+ })
697
+ })
698
+ })
699
+
700
+ sandbox.stub(ModelParticipant, 'getParticipantLimitByParticipantCurrencyLimit').returns(Promise.resolve(insufficientParticipantLimit))
701
+ const getByNameAndCurrencyStub = sandbox.stub(ModelParticipant, 'getByNameAndCurrency')
702
+ getByNameAndCurrencyStub.withArgs('dfsp1', 'USD', 1).resolves({
703
+ participantCurrencyId: 1,
704
+ participantId: 1,
705
+ currencyId: 'USD',
706
+ isActive: 1
707
+ })
708
+ getByNameAndCurrencyStub.withArgs('dfsp1', 'USD', 2).resolves({
709
+ participantCurrencyId: 2,
710
+ participantId: 1,
711
+ currencyId: 'USD',
712
+ isActive: 1
713
+ })
714
+ sandbox.stub(SettlementModelCached, 'getByLedgerAccountTypeId').resolves({
715
+ settlementDelayId: Enum.Settlements.SettlementDelay.DEFERRED,
716
+ settlementAccountTypeId: Enum.Accounts.LedgerAccountType.SETTLEMENT
717
+ })
718
+
719
+ const allSettlementModels = [{
720
+ settlementModelId: 1,
721
+ name: 'DEFERREDNET',
722
+ isActive: 1,
723
+ settlementGranularityId: 2,
724
+ settlementInterchangeId: 2,
725
+ settlementDelayId: 2,
726
+ currencyId: null,
727
+ requireLiquidityCheck: 1,
728
+ ledgerAccountTypeId: 1,
729
+ autoPositionReset: 1,
730
+ adjustPosition: 0,
731
+ settlementAccountTypeId: 2
732
+ }]
733
+
734
+ sandbox.stub(SettlementModelCached, 'getAll').resolves(allSettlementModels)
735
+ const messageProtocolCopy = MainUtil.clone(messageProtocol)
736
+ const { preparedMessagesList, limitAlarms } = await ModelPosition.prepareChangeParticipantPositionTransaction([{ value: messageProtocolCopy }])
449
737
  test.ok(Array.isArray(preparedMessagesList), 'array of prepared transfers is returned')
450
738
  test.ok(Array.isArray(limitAlarms), 'array of limit alarms is returned')
739
+ test.ok(preparedMessagesList[0].rawMessage.value.content.payload.errorInformation.errorCode === '4200')
451
740
  test.ok(knexStub.withArgs('participantPosition').calledThrice, 'knex called with participantPosition twice')
452
741
  test.ok(knexStub.withArgs('transferStateChange').calledOnce, 'knex called with transferStateChange twice')
453
742
  test.ok(knexStub.withArgs('transfer').calledOnce, 'knex called with transferStateChange twice')
@@ -515,7 +804,7 @@ Test('Position facade', async (positionFacadeTest) => {
515
804
  })
516
805
  })
517
806
 
518
- await ModelPosition.changeParticipantPositionTransaction(participantCurrencyId, isIncrease, amount, transferStateChange)
807
+ await ModelPosition.changeParticipantPositionTransaction(participantCurrencyId, isIncrease, amount, MainUtil.clone(transferStateChange))
519
808
  test.pass('completed successfully')
520
809
  test.ok(knexStub.withArgs('participantPosition').calledTwice, 'knex called with participantPosition twice')
521
810
  test.ok(knexStub.withArgs('transferStateChange').calledTwice, 'knex called with transferStateChange twice')
@@ -556,7 +845,7 @@ Test('Position facade', async (positionFacadeTest) => {
556
845
  })
557
846
  })
558
847
 
559
- await ModelPosition.changeParticipantPositionTransaction(participantCurrencyId, isIncrease, amount, transferStateChange)
848
+ await ModelPosition.changeParticipantPositionTransaction(participantCurrencyId, isIncrease, amount, MainUtil.clone(transferStateChange))
560
849
  test.pass('completed successfully')
561
850
  test.ok(knexStub.withArgs('participantPosition').calledTwice, 'knex called with participantPosition twice')
562
851
  test.ok(knexStub.withArgs('transferStateChange').calledTwice, 'knex called with transferStateChange twice')
@@ -580,7 +869,7 @@ Test('Position facade', async (positionFacadeTest) => {
580
869
 
581
870
  knexStub.throws(new Error())
582
871
 
583
- await ModelPosition.changeParticipantPositionTransaction(participantCurrencyId, isIncrease, amount, transferStateChange)
872
+ await ModelPosition.changeParticipantPositionTransaction(participantCurrencyId, isIncrease, amount, MainUtil.clone(transferStateChange))
584
873
  test.fail('Error not thrown!')
585
874
  test.end()
586
875
  } catch (err) {
@@ -24,13 +24,14 @@
24
24
 
25
25
  'use strict'
26
26
 
27
- const Test = require('tape')
27
+ const Test = require('tapes')(require('tape'))
28
28
  const Sinon = require('sinon')
29
29
  const Db = require('../../../../src/lib/db')
30
30
  const Logger = require('@mojaloop/central-services-logger')
31
31
  const Model = require('../../../../src/models/position/participantPosition')
32
32
 
33
33
  Test('Participant Limit model', async (participantPositionTest) => {
34
+ let sandbox
34
35
  const participantPosition1 = {
35
36
  participantCurrencyId: 1,
36
37
  value: 100,
@@ -38,12 +39,26 @@ Test('Participant Limit model', async (participantPositionTest) => {
38
39
  changedDate: new Date()
39
40
  }
40
41
 
41
- const sandbox = Sinon.createSandbox()
42
- Db.participantPosition = {
43
- insert: sandbox.stub(),
44
- update: sandbox.stub(),
45
- findOne: sandbox.stub()
46
- }
42
+ participantPositionTest.beforeEach(t => {
43
+ sandbox = Sinon.createSandbox()
44
+ Db.participantPosition = {
45
+ insert: sandbox.stub(),
46
+ update: sandbox.stub(),
47
+ findOne: sandbox.stub()
48
+ }
49
+
50
+ Db.from = (table) => {
51
+ return Db[table]
52
+ }
53
+
54
+ t.end()
55
+ })
56
+
57
+ participantPositionTest.afterEach(t => {
58
+ sandbox.restore()
59
+
60
+ t.end()
61
+ })
47
62
 
48
63
  await participantPositionTest.test('insert participant position', async (assert) => {
49
64
  try {
@@ -34,7 +34,6 @@ const SettlementModelModel = require('../../../../src/models/settlement/settleme
34
34
  Test('Settlement model', async (settlementTest) => {
35
35
  const settlementModelId = 1
36
36
  const settlementModel = [
37
-
38
37
  {
39
38
  settlementModelId: 106,
40
39
  name: 'DEFERRED_NET',
@@ -48,7 +47,24 @@ Test('Settlement model', async (settlementTest) => {
48
47
  }
49
48
  ]
50
49
 
51
- const sandbox = Sinon.createSandbox()
50
+ let sandbox
51
+
52
+ settlementTest.beforeEach(t => {
53
+ sandbox = Sinon.createSandbox()
54
+
55
+ Db.from = (table) => {
56
+ return Db[table]
57
+ }
58
+
59
+ t.end()
60
+ })
61
+
62
+ settlementTest.afterEach(t => {
63
+ sandbox.restore()
64
+
65
+ t.end()
66
+ })
67
+
52
68
  await settlementTest.test('create settlement model', async (assert) => {
53
69
  Db.settlementModel = {
54
70
  insert: sandbox.stub().returns(true),
@@ -80,41 +96,6 @@ Test('Settlement model', async (settlementTest) => {
80
96
  assert.end()
81
97
  })
82
98
 
83
- await settlementTest.test('get settlement model', async (assert) => {
84
- try {
85
- Db.settlementModel.find.withArgs({ name: 'test' }).returns([
86
- {
87
- settlementModelId: 106,
88
- name: 'testingSevennnnN91',
89
- isActive: 1,
90
- settlementGranularityId: 1,
91
- settlementInterchangeId: 1,
92
- settlementDelayId: 2,
93
- currencyId: null,
94
- requireLiquidityCheck: 1,
95
- ledgerAccountTypeId: 6
96
- }])
97
- const expected = {
98
- settlementModelId: 106,
99
- name: 'testingSevennnnN91',
100
- isActive: 1,
101
- settlementGranularityId: 1,
102
- settlementInterchangeId: 1,
103
- settlementDelayId: 2,
104
- currencyId: null,
105
- requireLiquidityCheck: 1,
106
- ledgerAccountTypeId: 6
107
- }
108
- const result = await SettlementModelModel.getByName('test')
109
- assert.equal(JSON.stringify(result), JSON.stringify(expected))
110
- assert.end()
111
- } catch (err) {
112
- Logger.error(`get settlement model by name failed with error - ${err}`)
113
- assert.fail()
114
- assert.end()
115
- }
116
- })
117
-
118
99
  await settlementTest.test('get with empty name', async (assert) => {
119
100
  Db.settlementModel.find.withArgs({ name: '' }).throws(new Error())
120
101
  try {