account-lookup-service 15.5.0-iso.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (209) hide show
  1. package/.circleci/config.yml +11 -0
  2. package/.ncurc.yaml +6 -0
  3. package/.nvmrc +1 -0
  4. package/.nycrc.yml +20 -0
  5. package/.versionrc +15 -0
  6. package/CHANGELOG.md +330 -0
  7. package/CODEOWNERS +38 -0
  8. package/Dockerfile +45 -0
  9. package/LICENSE.md +10 -0
  10. package/README.md +252 -0
  11. package/audit-ci.jsonc +32 -0
  12. package/audit-resolve.json +161 -0
  13. package/config/default.json +109 -0
  14. package/config/knexfile.js +21 -0
  15. package/docker/account-lookup-service/default.json +106 -0
  16. package/docker/account-lookup-service/make-default-json.sh +5 -0
  17. package/docker/account-lookup-service/override.json +15 -0
  18. package/docker/central-ledger/default.json +458 -0
  19. package/docker/config-modifier/account-lookup-service.js +31 -0
  20. package/docker/kafka/consumer.properties +26 -0
  21. package/docker/kafka/producer.properties +45 -0
  22. package/docker/kafka/server.properties +143 -0
  23. package/docker/kafka/tools-log4j.properties +21 -0
  24. package/docker/mock-proxy/Dockerfile +15 -0
  25. package/docker/mock-proxy/package-lock.json +4986 -0
  26. package/docker/mock-proxy/package.json +24 -0
  27. package/docker/mock-proxy/src/config.ts +14 -0
  28. package/docker/mock-proxy/src/server.ts +94 -0
  29. package/docker/mock-proxy/src/utils.ts +29 -0
  30. package/docker/mock-proxy/tsconfig.json +24 -0
  31. package/docker/sql-init/01_permissions.sql +2 -0
  32. package/docker/sql-init-central-ledger/01_permissions.sql +2 -0
  33. package/docker/wait-for/wait-for-account-lookup-service.sh +10 -0
  34. package/docker/wait-for/wait-for-central-ledger.sh +11 -0
  35. package/docker/wait-for/wait-for-kafka.sh +7 -0
  36. package/docker/wait-for/wait-for-ml-api-adapter.sh +9 -0
  37. package/docker/wait-for/wait-for-mockserver.sh +20 -0
  38. package/docker/wait-for/wait-for-mysql-als.sh +14 -0
  39. package/docker/wait-for/wait-for-mysql-central-ledger.sh +11 -0
  40. package/docker/wait-for/wait-for-mysql.sh +11 -0
  41. package/docker/wait-for/wait-for-objstore.sh +12 -0
  42. package/docker/wait-for/wait-for.env +18 -0
  43. package/docker/wait-for/wait-for.sh +81 -0
  44. package/docker-compose.integration.yml +29 -0
  45. package/docker-compose.yml +243 -0
  46. package/jest-int.config.js +8 -0
  47. package/jest.config.js +16 -0
  48. package/jsdoc.json +38 -0
  49. package/migrations/01_currency.js +42 -0
  50. package/migrations/02_endpointType.js +43 -0
  51. package/migrations/03_endpointType-indexes.js +37 -0
  52. package/migrations/04_partyIdType.js +43 -0
  53. package/migrations/05_partyIdType-indexes.js +38 -0
  54. package/migrations/08_oracleEndpoint.js +51 -0
  55. package/migrations/09_oracleEndpoint-indexes.js +41 -0
  56. package/migrations/10_oracleEndpoint-remove-constraints.js +38 -0
  57. package/package.json +180 -0
  58. package/scripts/_wait4_all.js +143 -0
  59. package/scripts/test-functional.sh +76 -0
  60. package/secrets/jwsSigningKey.key +27 -0
  61. package/seeds/currency.js +765 -0
  62. package/seeds/endpointType.js +65 -0
  63. package/seeds/partyIdType.js +79 -0
  64. package/src/api/endpointcache.js +67 -0
  65. package/src/api/health.js +66 -0
  66. package/src/api/index.js +85 -0
  67. package/src/api/oracles/{ID}.js +100 -0
  68. package/src/api/oracles.js +96 -0
  69. package/src/api/participants/{ID}/error.js +44 -0
  70. package/src/api/participants/{ID}.js +44 -0
  71. package/src/api/participants/{Type}/{ID}/error.js +74 -0
  72. package/src/api/participants/{Type}/{ID}/{SubId}/error.js +68 -0
  73. package/src/api/participants/{Type}/{ID}/{SubId}.js +113 -0
  74. package/src/api/participants/{Type}/{ID}.js +133 -0
  75. package/src/api/participants.js +63 -0
  76. package/src/api/parties/{Type}/{ID}/error.js +66 -0
  77. package/src/api/parties/{Type}/{ID}/{SubId}/error.js +56 -0
  78. package/src/api/parties/{Type}/{ID}/{SubId}.js +77 -0
  79. package/src/api/parties/{Type}/{ID}.js +98 -0
  80. package/src/api/routes.js +294 -0
  81. package/src/constants.js +16 -0
  82. package/src/domain/oracle/index.js +33 -0
  83. package/src/domain/oracle/oracle.js +234 -0
  84. package/src/domain/participants/index.js +35 -0
  85. package/src/domain/participants/participants.js +560 -0
  86. package/src/domain/parties/getPartiesByTypeAndID.js +239 -0
  87. package/src/domain/parties/index.js +32 -0
  88. package/src/domain/parties/parties.js +215 -0
  89. package/src/domain/parties/utils.js +84 -0
  90. package/src/domain/timeout/dto.js +48 -0
  91. package/src/domain/timeout/index.js +104 -0
  92. package/src/handlers/TimeoutHandler.js +94 -0
  93. package/src/handlers/index.js +70 -0
  94. package/src/handlers/monitoring/index.js +51 -0
  95. package/src/handlers/monitoring/plugins/health.js +61 -0
  96. package/src/handlers/monitoring/plugins/metrics.js +48 -0
  97. package/src/handlers/register.js +102 -0
  98. package/src/index.js +66 -0
  99. package/src/interface/admin-swagger.yaml +804 -0
  100. package/src/interface/admin_swagger.json +959 -0
  101. package/src/interface/api-swagger-iso20022-parties.yaml +1734 -0
  102. package/src/interface/api-swagger.yaml +1733 -0
  103. package/src/interface/api_swagger.json +3046 -0
  104. package/src/interface/fspiop-rest-v2.0-ISO20022_parties.yaml +2256 -0
  105. package/src/interface/thirdparty/admin-swagger.yaml +808 -0
  106. package/src/interface/thirdparty/admin_swagger.json +961 -0
  107. package/src/interface/thirdparty/api-swagger.yaml +1739 -0
  108. package/src/interface/thirdparty/api_swagger.json +3142 -0
  109. package/src/lib/argv.js +39 -0
  110. package/src/lib/cache.js +126 -0
  111. package/src/lib/config.js +183 -0
  112. package/src/lib/db.js +26 -0
  113. package/src/lib/headers.js +53 -0
  114. package/src/lib/healthCheck/subServiceHealth.js +84 -0
  115. package/src/lib/index.js +11 -0
  116. package/src/lib/migrator.js +17 -0
  117. package/src/lib/requestLogger.js +54 -0
  118. package/src/lib/util.js +66 -0
  119. package/src/metrics/handler.js +33 -0
  120. package/src/metrics/plugin.js +52 -0
  121. package/src/metrics/routes.js +43 -0
  122. package/src/models/currency/currency.js +48 -0
  123. package/src/models/currency/index.js +32 -0
  124. package/src/models/endpointType/endpointType.js +48 -0
  125. package/src/models/endpointType/index.js +32 -0
  126. package/src/models/misc/migrationLock.js +49 -0
  127. package/src/models/oracle/facade.js +341 -0
  128. package/src/models/oracle/index.js +41 -0
  129. package/src/models/oracle/oracleEndpoint.js +192 -0
  130. package/src/models/oracle/oracleEndpointCached.js +108 -0
  131. package/src/models/participantEndpoint/facade.js +238 -0
  132. package/src/models/partyIdType/index.js +32 -0
  133. package/src/models/partyIdType/partyIdType.js +41 -0
  134. package/src/plugins.js +139 -0
  135. package/src/server.js +199 -0
  136. package/test/fixtures/index.js +131 -0
  137. package/test/fixtures/iso.js +110 -0
  138. package/test/integration/.env +8 -0
  139. package/test/integration/api/parties.test.js +137 -0
  140. package/test/integration/constants.js +20 -0
  141. package/test/integration/domain/oracle/index.test.js +324 -0
  142. package/test/integration/domain/timeout/index.test.js +75 -0
  143. package/test/integration/env.sh +15 -0
  144. package/test/integration/example.test.js +12 -0
  145. package/test/integration/models/currency/currency.test.js +68 -0
  146. package/test/integration/plugins.test.js +62 -0
  147. package/test/integration/prepareTestParticipants.js +30 -0
  148. package/test/integration/setup.js +5 -0
  149. package/test/integration-config.json +81 -0
  150. package/test/integration-runner.sh +108 -0
  151. package/test/unit/api/health.test.js +142 -0
  152. package/test/unit/api/oracles/{ID}.test.js +264 -0
  153. package/test/unit/api/oracles.test.js +173 -0
  154. package/test/unit/api/participants/participants.test.js +117 -0
  155. package/test/unit/api/participants/{Type}/{ID}/error.test.js +155 -0
  156. package/test/unit/api/participants/{Type}/{ID}/{SubId}/error.test.js +131 -0
  157. package/test/unit/api/participants/{Type}/{ID}/{SubId}.test.js +377 -0
  158. package/test/unit/api/participants/{Type}/{ID}.test.js +383 -0
  159. package/test/unit/api/participants.test.js +108 -0
  160. package/test/unit/api/parties/endpointcache.test.js +83 -0
  161. package/test/unit/api/parties/parties.test.js +102 -0
  162. package/test/unit/api/parties/{Type}/{ID}/error.test.js +145 -0
  163. package/test/unit/api/parties/{Type}/{ID}/{SubId}/error.test.js +141 -0
  164. package/test/unit/api/parties/{Type}/{ID}/{SubId}.test.js +241 -0
  165. package/test/unit/api/parties/{Type}/{ID}.test.js +240 -0
  166. package/test/unit/domain/oracle/oracle.test.js +505 -0
  167. package/test/unit/domain/participants/participants.test.js +1724 -0
  168. package/test/unit/domain/parties/parties.test.js +940 -0
  169. package/test/unit/domain/timeout/dto.test.js +28 -0
  170. package/test/unit/domain/timeout/index.test.js +81 -0
  171. package/test/unit/handlers/TimeoutHandler.test.js +125 -0
  172. package/test/unit/handlers/index.test.js +56 -0
  173. package/test/unit/handlers/register.test.js +90 -0
  174. package/test/unit/index.test.js +139 -0
  175. package/test/unit/iso20022/partiesValidation.test.js +129 -0
  176. package/test/unit/lib/TransformFacades.test.js +18 -0
  177. package/test/unit/lib/argv.test.js +40 -0
  178. package/test/unit/lib/cache.test.js +172 -0
  179. package/test/unit/lib/config.test.js +108 -0
  180. package/test/unit/lib/healthCheck/subServiceHealth.test.js +89 -0
  181. package/test/unit/lib/migrator.test.js +52 -0
  182. package/test/unit/lib/requestLogger.test.js +115 -0
  183. package/test/unit/lib/util.test.js +68 -0
  184. package/test/unit/mocks.js +66 -0
  185. package/test/unit/models/currency/currency.test.js +91 -0
  186. package/test/unit/models/endpointType/endpointType.test.js +69 -0
  187. package/test/unit/models/misc/migrationLock.test.js +96 -0
  188. package/test/unit/models/oracle/facade.test.js +546 -0
  189. package/test/unit/models/oracle/oracleEndpoint.test.js +409 -0
  190. package/test/unit/models/oracle/oracleEndpointCached.test.js +153 -0
  191. package/test/unit/models/participantEndpoint/facade.test.js +295 -0
  192. package/test/unit/models/partyIdType/partyIdType.test.js +88 -0
  193. package/test/unit/plugins.test.js +89 -0
  194. package/test/unit/setup.js +7 -0
  195. package/test/util/apiClients/AlsApiClient.js +44 -0
  196. package/test/util/apiClients/BasicApiClient.js +34 -0
  197. package/test/util/apiClients/ProxyApiClient.js +25 -0
  198. package/test/util/apiClients/index.js +7 -0
  199. package/test/util/helper.js +332 -0
  200. package/test/util/index.js +11 -0
  201. package/test/util/mockgen.js +43 -0
  202. package/test/util/onboarding.js +132 -0
  203. package/test/util/scripts/addAlsDb.sh +33 -0
  204. package/test/util/scripts/configureMockServer.sh +35 -0
  205. package/test/util/scripts/env.sh +19 -0
  206. package/test/util/scripts/populateTestData.sh +62 -0
  207. package/test/util/scripts/startMockCentralServer.sh +45 -0
  208. package/test/util/scripts/startMockOracleServer.sh +45 -0
  209. package/test/util/testConfig.js +44 -0
@@ -0,0 +1,341 @@
1
+ /*****
2
+ License
3
+ --------------
4
+ Copyright © 2017 Bill & Melinda Gates Foundation
5
+ The Mojaloop files are made available by the Bill & Melinda Gates Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at
6
+ http://www.apache.org/licenses/LICENSE-2.0
7
+ Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
8
+ Contributors
9
+ --------------
10
+ This is the official list of the Mojaloop project contributors for this file.
11
+ Names of the original copyright holders (individuals or organizations)
12
+ should be listed with a '*' in the first column. People who have
13
+ contributed from an organization can be listed under the organization
14
+ that actually holds the copyright for their contributions (see the
15
+ Gates Foundation organization for an example). Those individuals should have
16
+ their names indented and be marked with a '-'. Email address can be added
17
+ optionally within square brackets <email>.
18
+ * Gates Foundation
19
+ * Name Surname <name.surname@gatesfoundation.com>
20
+
21
+ * Rajiv Mothilal <rajiv.mothilal@modusbox.com>
22
+ * Steven Oderayi <steven.oderayi@modusbox.com>
23
+
24
+ --------------
25
+ ******/
26
+
27
+ 'use strict'
28
+
29
+ const Mustache = require('mustache')
30
+ const request = require('@mojaloop/central-services-shared').Util.Request
31
+ const Enums = require('@mojaloop/central-services-shared').Enum
32
+ const Logger = require('@mojaloop/central-services-logger')
33
+ const ErrorHandler = require('@mojaloop/central-services-error-handling')
34
+ const Metrics = require('@mojaloop/central-services-metrics')
35
+
36
+ const Config = require('../../lib/config')
37
+ const oracleEndpointCached = require('../oracle/oracleEndpointCached')
38
+ const { hubNameRegex } = require('../../lib/util').hubNameConfig
39
+
40
+ /**
41
+ * @function oracleRequest
42
+ *
43
+ * @description This sends a request to the oracles that are registered to the ALS
44
+ *
45
+ * @param {object} headers - incoming http request headers
46
+ * @param {string} method - incoming http request method
47
+ * @param {object} params - uri parameters of the http request
48
+ * @param {object} query - the query parameter on the uri of the http request
49
+ * @param {object} payload - payload of the request being sent out
50
+ *
51
+ * @returns {object} returns the response from the oracle
52
+ */
53
+ exports.oracleRequest = async (headers, method, params = {}, query = {}, payload = undefined, cache) => {
54
+ try {
55
+ let url
56
+ const partyIdType = params.Type
57
+ const partyIdentifier = params.ID
58
+ const currency = (payload && payload.currency) ? payload.currency : (query && query.currency) ? query.currency : undefined
59
+ const partySubIdOrType = (params && params.SubId) ? params.SubId : (query && query.partySubIdOrType) ? query.partySubIdOrType : undefined
60
+ const isGetRequest = method.toUpperCase() === Enums.Http.RestMethods.GET
61
+ if (currency && partySubIdOrType && isGetRequest) {
62
+ url = await _getOracleEndpointByTypeCurrencyAndSubId(partyIdType, partyIdentifier, currency, partySubIdOrType)
63
+ } else if (currency && isGetRequest) {
64
+ url = await _getOracleEndpointByTypeAndCurrency(partyIdType, partyIdentifier, currency)
65
+ } else if (partySubIdOrType && isGetRequest) {
66
+ url = await _getOracleEndpointByTypeAndSubId(partyIdType, partyIdentifier, partySubIdOrType)
67
+ } else {
68
+ url = await _getOracleEndpointByType(partyIdType, partyIdentifier)
69
+ if (partySubIdOrType) {
70
+ payload = { ...payload, partySubIdOrType }
71
+ }
72
+ }
73
+ Logger.isDebugEnabled && Logger.debug(`Oracle endpoints: ${url}`)
74
+ const histTimerEnd = Metrics.getHistogram(
75
+ 'egress_oracleRequest',
76
+ 'Egress: oracleRequest',
77
+ ['success', 'hit']
78
+ ).startTimer()
79
+ try {
80
+ if (isGetRequest) {
81
+ let cachedOracleFspResponse
82
+ cachedOracleFspResponse = cache && cache.get(cache.createKey(`oracleSendRequest_${url}`))
83
+ if (!cachedOracleFspResponse) {
84
+ cachedOracleFspResponse = await request.sendRequest({
85
+ url,
86
+ headers,
87
+ source: headers[Enums.Http.Headers.FSPIOP.SOURCE],
88
+ destination: headers[Enums.Http.Headers.FSPIOP.DESTINATION] || Config.HUB_NAME,
89
+ method: method.toUpperCase(),
90
+ payload,
91
+ hubNameRegex
92
+ })
93
+ // Trying to cache the whole response object will fail because it contains circular references
94
+ // so we'll just cache the data property of the response.
95
+ cachedOracleFspResponse = {
96
+ data: cachedOracleFspResponse.data
97
+ }
98
+ cache && cache.set(
99
+ cache.createKey(`oracleSendRequest_${url}`),
100
+ cachedOracleFspResponse
101
+ )
102
+ histTimerEnd({ success: true, hit: false })
103
+ } else {
104
+ cachedOracleFspResponse = cachedOracleFspResponse.item
105
+ histTimerEnd({ success: true, hit: true })
106
+ Logger.isDebugEnabled && Logger.debug(`${new Date().toISOString()}, [oracleRequest]: cache hit for fsp for partyId lookup`)
107
+ }
108
+
109
+ return cachedOracleFspResponse
110
+ }
111
+
112
+ return await request.sendRequest({
113
+ url,
114
+ headers,
115
+ source: headers[Enums.Http.Headers.FSPIOP.SOURCE],
116
+ destination: headers[Enums.Http.Headers.FSPIOP.DESTINATION] || Config.HUB_NAME,
117
+ method: method.toUpperCase(),
118
+ payload,
119
+ hubNameRegex
120
+ })
121
+ } catch (err) {
122
+ histTimerEnd({ success: false, hit: false })
123
+ throw err
124
+ }
125
+ } catch (err) {
126
+ Logger.isErrorEnabled && Logger.error(`error in oracleRequest: ${err?.stack}`)
127
+ // If the error was a 400 from the Oracle, we'll modify the error to generate a response to the
128
+ // initiator of the request.
129
+ if (
130
+ err.name === 'FSPIOPError' &&
131
+ err.apiErrorCode.code === ErrorHandler.Enums.FSPIOPErrorCodes.DESTINATION_COMMUNICATION_ERROR.code
132
+ ) {
133
+ if (err.extensions.some(ext => (ext.key === 'status' && ext.value === Enums.Http.ReturnCodes.BADREQUEST.CODE))) {
134
+ throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.PARTY_NOT_FOUND)
135
+ // Added error 404 to cover a special case of the Mowali implementation
136
+ // which uses mojaloop/als-oracle-pathfinder and currently returns 404
137
+ // and in which case the Mowali implementation expects back `DESTINATION_FSP_ERROR`.
138
+ } else if (err.extensions.some(ext => (ext.key === 'status' && ext.value === Enums.Http.ReturnCodes.NOTFOUND.CODE))) {
139
+ throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.DESTINATION_FSP_ERROR)
140
+ }
141
+ }
142
+ throw ErrorHandler.Factory.reformatFSPIOPError(err)
143
+ }
144
+ }
145
+
146
+ /**
147
+ * @function _getOracleEndpointByTypeAndCurrency
148
+ *
149
+ * @description Retirieves and returns the URL to an oracle by partyIdType and currency
150
+ *
151
+ * @param {string} partyIdType - party ID type (e.g MSISDN)
152
+ * @param {string} partyIdentifier - party ID
153
+ * @param {string} currency - currency ID
154
+ *
155
+ * @returns {string} returns the endpoint to the oracle
156
+ */
157
+ const _getOracleEndpointByTypeAndCurrency = async (partyIdType, partyIdentifier, currency) => {
158
+ let url
159
+ const oracleEndpointModel = await oracleEndpointCached.getOracleEndpointByTypeAndCurrency(partyIdType, currency)
160
+ if (oracleEndpointModel.length > 0) {
161
+ if (oracleEndpointModel.length > 1) {
162
+ const defautOracle = oracleEndpointModel.filter(oracle => oracle.isDefault).pop()
163
+ if (defautOracle) {
164
+ url = Mustache.render(
165
+ defautOracle.value + Enums.EndPoints.FspEndpointTemplates.ORACLE_PARTICIPANTS_TYPE_ID_CURRENCY,
166
+ { partyIdType, partyIdentifier, currency }
167
+ )
168
+ }
169
+ } else {
170
+ url = Mustache.render(
171
+ oracleEndpointModel[0].value + Enums.EndPoints.FspEndpointTemplates.ORACLE_PARTICIPANTS_TYPE_ID_CURRENCY,
172
+ { partyIdType, partyIdentifier, currency }
173
+ )
174
+ }
175
+ } else {
176
+ Logger.isErrorEnabled && Logger.error(`Oracle type:${partyIdType} and currency:${currency} not found`)
177
+ throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.ADD_PARTY_INFO_ERROR, `Oracle type:${partyIdType} and currency:${currency} not found`).toApiErrorObject(Config.ERROR_HANDLING)
178
+ }
179
+ return url
180
+ }
181
+
182
+ /**
183
+ * @function _getOracleEndpointByType
184
+ *
185
+ * @description Retrieves and returns the URL to an oracle by partyIdType
186
+ *
187
+ * @param {string} partyIdType - party ID type (e.g MSISDN)
188
+ * @param {string} partyIdentifier - party ID
189
+ *
190
+ * @returns {string} returns the endpoint to the oracle
191
+ */
192
+ const _getOracleEndpointByType = async (partyIdType, partyIdentifier) => {
193
+ let url
194
+ const oracleEndpointModel = await oracleEndpointCached.getOracleEndpointByType(partyIdType)
195
+ if (oracleEndpointModel.length > 0) {
196
+ if (oracleEndpointModel.length > 1) {
197
+ const defaultOracle = oracleEndpointModel.filter(oracle => oracle.isDefault).pop()
198
+ if (defaultOracle) {
199
+ url = Mustache.render(
200
+ defaultOracle.value + Enums.EndPoints.FspEndpointTemplates.ORACLE_PARTICIPANTS_TYPE_ID,
201
+ { partyIdType, partyIdentifier }
202
+ )
203
+ }
204
+ } else {
205
+ url = Mustache.render(
206
+ oracleEndpointModel[0].value + Enums.EndPoints.FspEndpointTemplates.ORACLE_PARTICIPANTS_TYPE_ID,
207
+ { partyIdType, partyIdentifier }
208
+ )
209
+ }
210
+ } else {
211
+ Logger.isErrorEnabled && Logger.error(`Oracle type:${partyIdType} not found`)
212
+ throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.ADD_PARTY_INFO_ERROR, `Oracle type: ${partyIdType} not found`)
213
+ }
214
+ return url
215
+ }
216
+
217
+ /**
218
+ * @function _getOracleEndpointByTypeAndSubId
219
+ *
220
+ * @description Retrieves and returns the URL to an oracle by partyIdType and subId
221
+ *
222
+ * @param {string} partyIdType - party ID type (e.g MSISDN)
223
+ * @param {string} partyIdentifier - party ID
224
+ * @param {string} partySubIdOrType - party subId
225
+ *
226
+ * @returns {string} returns the endpoint to the oracle
227
+ */
228
+ const _getOracleEndpointByTypeAndSubId = async (partyIdType, partyIdentifier, partySubIdOrType) => {
229
+ let url
230
+ const oracleEndpointModel = await oracleEndpointCached.getOracleEndpointByType(partyIdType)
231
+ if (oracleEndpointModel.length > 0) {
232
+ if (oracleEndpointModel.length > 1) {
233
+ const defautOracle = oracleEndpointModel.filter(oracle => oracle.isDefault).pop()
234
+ if (defautOracle) {
235
+ url = Mustache.render(
236
+ defautOracle.value + Enums.EndPoints.FspEndpointTemplates.ORACLE_PARTICIPANTS_TYPE_ID_SUB_ID,
237
+ { partyIdType, partyIdentifier, partySubIdOrType }
238
+ )
239
+ }
240
+ } else {
241
+ url = Mustache.render(
242
+ oracleEndpointModel[0].value + Enums.EndPoints.FspEndpointTemplates.ORACLE_PARTICIPANTS_TYPE_ID_SUB_ID,
243
+ { partyIdType, partyIdentifier, partySubIdOrType }
244
+ )
245
+ }
246
+ } else {
247
+ Logger.isErrorEnabled && Logger.error(`Oracle type: ${partyIdType} and subId: ${partySubIdOrType} not found`)
248
+ throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.ADD_PARTY_INFO_ERROR, `Oracle type: ${partyIdType} and subId: ${partySubIdOrType} not found`).toApiErrorObject(Config.ERROR_HANDLING)
249
+ }
250
+ return url
251
+ }
252
+
253
+ /**
254
+ * @function _getOracleEndpointByTypeCurrencyAndSubId
255
+ *
256
+ * @description Retirieves and returns the URL to an oracle by partyIdType and currency
257
+ *
258
+ * @param {string} partyIdType - party ID type (e.g MSISDN)
259
+ * @param {string} partyIdentifier - party ID
260
+ * @param {string} currency - currency ID
261
+ * @param {string} partySubIdOrType - party subId
262
+ *
263
+ * @returns {string} returns the endpoint to the oracle
264
+ */
265
+ const _getOracleEndpointByTypeCurrencyAndSubId = async (partyIdType, partyIdentifier, currency, partySubIdOrType) => {
266
+ let url
267
+ const oracleEndpointModel = await oracleEndpointCached.getOracleEndpointByTypeAndCurrency(partyIdType, currency)
268
+ if (oracleEndpointModel.length > 0) {
269
+ if (oracleEndpointModel.length > 1) {
270
+ const defautOracle = oracleEndpointModel.filter(oracle => oracle.isDefault).pop()
271
+ if (defautOracle) {
272
+ url = Mustache.render(
273
+ defautOracle.value + Enums.EndPoints.FspEndpointTemplates.ORACLE_PARTICIPANTS_TYPE_ID_CURRENCY_SUB_ID,
274
+ { partyIdType, partyIdentifier, currency, partySubIdOrType }
275
+ )
276
+ }
277
+ } else {
278
+ url = Mustache.render(
279
+ oracleEndpointModel[0].value + Enums.EndPoints.FspEndpointTemplates.ORACLE_PARTICIPANTS_TYPE_ID_CURRENCY_SUB_ID,
280
+ { partyIdType, partyIdentifier, currency, partySubIdOrType }
281
+ )
282
+ }
283
+ } else {
284
+ Logger.isErrorEnabled && Logger.error(`Oracle type: ${partyIdType}, currency: ${currency}, and subId: ${partySubIdOrType} not found`)
285
+ throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.ADD_PARTY_INFO_ERROR, `Oracle type:${partyIdType}, currency:${currency} and subId: ${partySubIdOrType} not found`).toApiErrorObject(Config.ERROR_HANDLING)
286
+ }
287
+ return url
288
+ }
289
+
290
+ /**
291
+ * @function oracleBatchRequest
292
+ *
293
+ * @description This sends a request to the oracles that are registered to the ALS
294
+ *
295
+ * @param {object} headers - incoming http request headers
296
+ * @param {object} method - incoming http request method
297
+ * @param {object} requestPayload - the requestPayload from the original request
298
+ * @param {string} type - oracle type
299
+ * @param {object} payload - the payload to send in the request
300
+ *
301
+ * @returns {object} returns the response from the oracle
302
+ */
303
+ exports.oracleBatchRequest = async (headers, method, requestPayload, type, payload) => {
304
+ try {
305
+ let oracleEndpointModel
306
+ let url
307
+ if ((requestPayload && requestPayload.currency && requestPayload.currency.length !== 0)) {
308
+ oracleEndpointModel = await oracleEndpointCached.getOracleEndpointByTypeAndCurrency(type, requestPayload.currency)
309
+ } else {
310
+ oracleEndpointModel = await oracleEndpointCached.getOracleEndpointByType(type)
311
+ }
312
+ if (oracleEndpointModel.length > 0) {
313
+ if (oracleEndpointModel.length > 1) {
314
+ for (const record of oracleEndpointModel) {
315
+ if (record.isDefault) {
316
+ url = record.value + Enums.EndPoints.FspEndpointTemplates.ORACLE_PARTICIPANTS_BATCH
317
+ break
318
+ }
319
+ }
320
+ } else {
321
+ url = oracleEndpointModel[0].value + Enums.EndPoints.FspEndpointTemplates.ORACLE_PARTICIPANTS_BATCH
322
+ }
323
+ Logger.isDebugEnabled && Logger.debug(`Oracle endpoints: ${url}`)
324
+ return await request.sendRequest({
325
+ url,
326
+ headers,
327
+ source: headers[Enums.Http.Headers.FSPIOP.SOURCE],
328
+ destination: headers[Enums.Http.Headers.FSPIOP.DESTINATION] || Config.HUB_NAME,
329
+ method,
330
+ payload,
331
+ hubNameRegex
332
+ })
333
+ } else {
334
+ Logger.isErrorEnabled && Logger.error(`Oracle type:${type} not found`)
335
+ throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.ADD_PARTY_INFO_ERROR, `Oracle type:${type} not found`)
336
+ }
337
+ } catch (err) {
338
+ Logger.isErrorEnabled && Logger.error(err)
339
+ throw ErrorHandler.Factory.reformatFSPIOPError(err)
340
+ }
341
+ }
@@ -0,0 +1,41 @@
1
+ /*****
2
+ License
3
+ --------------
4
+ Copyright © 2017 Bill & Melinda Gates Foundation
5
+ The Mojaloop files are made available by the Bill & Melinda Gates Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at
6
+ http://www.apache.org/licenses/LICENSE-2.0
7
+ Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
8
+ Contributors
9
+ --------------
10
+ This is the official list of the Mojaloop project contributors for this file.
11
+ Names of the original copyright holders (individuals or organizations)
12
+ should be listed with a '*' in the first column. People who have
13
+ contributed from an organization can be listed under the organization
14
+ that actually holds the copyright for their contributions (see the
15
+ Gates Foundation organization for an example). Those individuals should have
16
+ their names indented and be marked with a '-'. Email address can be added
17
+ optionally within square brackets <email>.
18
+ * Gates Foundation
19
+ - Name Surname <name.surname@gatesfoundation.com>
20
+
21
+ - Rajiv Mothilal <rajiv.mothilal@modusbox.com>
22
+
23
+ --------------
24
+ ******/
25
+
26
+ 'use strict'
27
+
28
+ const oracleEndpointModel = require('./oracleEndpoint')
29
+
30
+ module.exports = {
31
+ getOracleEndpointByType: oracleEndpointModel.getOracleEndpointByType,
32
+ getOracleEndpointByTypeAndCurrency: oracleEndpointModel.getOracleEndpointByTypeAndCurrency,
33
+ getOracleEndpointByCurrency: oracleEndpointModel.getOracleEndpointByCurrency,
34
+ getAllOracleEndpoint: oracleEndpointModel.getAllOracleEndpoint,
35
+ getAllOracleEndpointsByMatchCondition: oracleEndpointModel.getAllOracleEndpointsByMatchCondition,
36
+ createOracleEndpoint: oracleEndpointModel.createOracleEndpoint,
37
+ updateOracleEndpointById: oracleEndpointModel.updateOracleEndpointById,
38
+ setIsActiveOracleEndpoint: oracleEndpointModel.setIsActiveOracleEndpoint,
39
+ destroyOracleEndpointById: oracleEndpointModel.destroyOracleEndpointById,
40
+ getOracleEndpointById: oracleEndpointModel.getOracleEndpointById
41
+ }
@@ -0,0 +1,192 @@
1
+ /*****
2
+ License
3
+ --------------
4
+ Copyright © 2017 Bill & Melinda Gates Foundation
5
+ The Mojaloop files are made available by the Bill & Melinda Gates Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at
6
+ http://www.apache.org/licenses/LICENSE-2.0
7
+ Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
8
+ Contributors
9
+ --------------
10
+ This is the official list of the Mojaloop project contributors for this file.
11
+ Names of the original copyright holders (individuals or organizations)
12
+ should be listed with a '*' in the first column. People who have
13
+ contributed from an organization can be listed under the organization
14
+ that actually holds the copyright for their contributions (see the
15
+ Gates Foundation organization for an example). Those individuals should have
16
+ their names indented and be marked with a '-'. Email address can be added
17
+ optionally within square brackets <email>.
18
+ * Gates Foundation
19
+ - Name Surname <name.surname@gatesfoundation.com>
20
+
21
+ * Rajiv Mothilal <rajiv.mothilal@modusbox.com>
22
+
23
+ --------------
24
+ ******/
25
+
26
+ 'use strict'
27
+
28
+ const Db = require('../../lib/db')
29
+ const ErrorHandler = require('@mojaloop/central-services-error-handling')
30
+
31
+ const getOracleEndpointByType = async (type) => {
32
+ try {
33
+ return Db.from('oracleEndpoint').query(builder => {
34
+ return builder.innerJoin('endpointType AS et', 'oracleEndpoint.endpointTypeId', 'et.endpointTypeId')
35
+ .innerJoin('partyIdType AS pt', 'oracleEndpoint.partyIdTypeId', 'pt.partyIdTypeId')
36
+ .where({
37
+ 'pt.name': type,
38
+ 'pt.isActive': 1,
39
+ 'oracleEndpoint.isActive': 1,
40
+ 'et.isActive': 1
41
+ })
42
+ .select('oracleEndpoint.oracleEndpointId', 'et.type as endpointType', 'oracleEndpoint.value',
43
+ 'pt.name as idType', 'oracleEndpoint.currencyId as currency', 'oracleEndpoint.isDefault')
44
+ })
45
+ } catch (err) {
46
+ throw ErrorHandler.Factory.reformatFSPIOPError(err)
47
+ }
48
+ }
49
+
50
+ const getOracleEndpointByTypeAndCurrency = async (type, currencyId) => {
51
+ try {
52
+ return Db.from('oracleEndpoint').query(builder => {
53
+ return builder.innerJoin('currency AS cu', 'oracleEndpoint.currencyId', 'cu.currencyId')
54
+ .innerJoin('endpointType AS et', 'oracleEndpoint.endpointTypeId', 'et.endpointTypeId')
55
+ .innerJoin('partyIdType AS pt', 'oracleEndpoint.partyIdTypeId', 'pt.partyIdTypeId')
56
+ .where({
57
+ 'pt.name': type,
58
+ 'cu.currencyId': currencyId,
59
+ 'pt.isActive': 1,
60
+ 'oracleEndpoint.isActive': 1,
61
+ 'et.isActive': 1
62
+ })
63
+ .select('oracleEndpoint.oracleEndpointId', 'et.type as endpointType', 'oracleEndpoint.value',
64
+ 'pt.name as idType', 'oracleEndpoint.currencyId as currency', 'oracleEndpoint.isDefault')
65
+ })
66
+ } catch (err) {
67
+ throw ErrorHandler.Factory.reformatFSPIOPError(err)
68
+ }
69
+ }
70
+
71
+ const getOracleEndpointByCurrency = async (currencyId) => {
72
+ try {
73
+ return Db.from('oracleEndpoint').query(builder => {
74
+ return builder.innerJoin('currency AS cu', 'oracleEndpoint.currencyId', 'cu.currencyId')
75
+ .innerJoin('endpointType AS et', 'oracleEndpoint.endpointTypeId', 'et.endpointTypeId')
76
+ .innerJoin('partyIdType AS pt', 'oracleEndpoint.partyIdTypeId', 'pt.partyIdTypeId')
77
+ .where({
78
+ 'cu.currencyId': currencyId,
79
+ 'pt.isActive': 1,
80
+ 'oracleEndpoint.isActive': 1,
81
+ 'et.isActive': 1
82
+ })
83
+ .select('oracleEndpoint.oracleEndpointId', 'et.type as endpointType', 'oracleEndpoint.value',
84
+ 'pt.name as idType', 'oracleEndpoint.currencyId as currency', 'oracleEndpoint.isDefault')
85
+ })
86
+ } catch (err) {
87
+ throw ErrorHandler.Factory.reformatFSPIOPError(err)
88
+ }
89
+ }
90
+
91
+ const getOracleEndpointById = async (oracleEndpointId) => {
92
+ try {
93
+ return Db.from('oracleEndpoint').query(builder => {
94
+ return builder.innerJoin('currency AS cu', 'oracleEndpoint.currencyId', 'cu.currencyId')
95
+ .innerJoin('endpointType AS et', 'oracleEndpoint.endpointTypeId', 'et.endpointTypeId')
96
+ .innerJoin('partyIdType AS pt', 'oracleEndpoint.partyIdTypeId', 'pt.partyIdTypeId')
97
+ .where({
98
+ 'oracleEndpoint.oracleEndpointId': oracleEndpointId,
99
+ 'pt.isActive': 1,
100
+ 'oracleEndpoint.isActive': 1,
101
+ 'et.isActive': 1
102
+ })
103
+ .select('oracleEndpoint.oracleEndpointId', 'et.type as endpointType', 'oracleEndpoint.value',
104
+ 'pt.name as idType', 'oracleEndpoint.currencyId as currency', 'oracleEndpoint.isDefault')
105
+ })
106
+ } catch (err) {
107
+ throw ErrorHandler.Factory.reformatFSPIOPError(err)
108
+ }
109
+ }
110
+
111
+ const getAllOracleEndpoint = async () => {
112
+ try {
113
+ return Db.from('oracleEndpoint').query(builder => {
114
+ return builder.innerJoin('endpointType AS et', 'oracleEndpoint.endpointTypeId', 'et.endpointTypeId')
115
+ .innerJoin('partyIdType AS pt', 'oracleEndpoint.partyIdTypeId', 'pt.partyIdTypeId')
116
+ .where({
117
+ 'pt.isActive': 1,
118
+ 'oracleEndpoint.isActive': 1,
119
+ 'et.isActive': 1
120
+ })
121
+ .select('oracleEndpoint.oracleEndpointId', 'et.type as endpointType', 'oracleEndpoint.value',
122
+ 'pt.name as idType', 'oracleEndpoint.currencyId as currency', 'oracleEndpoint.isDefault')
123
+ })
124
+ } catch (err) {
125
+ throw ErrorHandler.Factory.reformatFSPIOPError(err)
126
+ }
127
+ }
128
+
129
+ const getAllOracleEndpointsByMatchCondition = async (oracleEndpointModel, partyIdTypeId, endpointTypeId) => {
130
+ try {
131
+ return Db.from('oracleEndpoint').query(builder => {
132
+ return builder.innerJoin('endpointType AS et', 'oracleEndpoint.endpointTypeId', 'et.endpointTypeId')
133
+ .innerJoin('partyIdType AS pt', 'oracleEndpoint.partyIdTypeId', 'pt.partyIdTypeId')
134
+ .where({
135
+ 'oracleEndpoint.currencyId': oracleEndpointModel.currency,
136
+ 'et.endpointTypeId': endpointTypeId,
137
+ 'pt.partyIdTypeId': partyIdTypeId,
138
+ 'oracleEndpoint.isActive': 1
139
+ })
140
+ .select('oracleEndpoint.oracleEndpointId', 'et.type as endpointType', 'oracleEndpoint.value',
141
+ 'pt.name as idType', 'oracleEndpoint.currencyId as currency', 'oracleEndpoint.isDefault',
142
+ 'oracleEndpoint.isActive', 'oracleEndpoint.partyIdTypeId', 'oracleEndpoint.endpointTypeId', 'oracleEndpoint.currencyId')
143
+ })
144
+ } catch (err) {
145
+ throw ErrorHandler.Factory.reformatFSPIOPError(err)
146
+ }
147
+ }
148
+
149
+ const createOracleEndpoint = async (oracleEndpointModel) => {
150
+ try {
151
+ return await Db.from('oracleEndpoint').insert(oracleEndpointModel)
152
+ } catch (err) {
153
+ throw ErrorHandler.Factory.reformatFSPIOPError(err)
154
+ }
155
+ }
156
+
157
+ const updateOracleEndpointById = async (id, oracleEndpointModel) => {
158
+ try {
159
+ return await Db.from('oracleEndpoint').update({ oracleEndpointId: id }, oracleEndpointModel)
160
+ } catch (err) {
161
+ throw ErrorHandler.Factory.reformatFSPIOPError(err)
162
+ }
163
+ }
164
+
165
+ const setIsActiveOracleEndpoint = async (oracleType, isActive) => {
166
+ try {
167
+ return await Db.from('oracleEndpoint').update({ oracleType }, { isActive })
168
+ } catch (err) {
169
+ throw ErrorHandler.Factory.reformatFSPIOPError(err)
170
+ }
171
+ }
172
+
173
+ const destroyOracleEndpointById = async (oracleEndpointId) => {
174
+ try {
175
+ return await Db.from('oracleEndpoint').update({ oracleEndpointId }, { isActive: false })
176
+ } catch (err) {
177
+ throw ErrorHandler.Factory.reformatFSPIOPError(err)
178
+ }
179
+ }
180
+
181
+ module.exports = {
182
+ getOracleEndpointByType,
183
+ getOracleEndpointByTypeAndCurrency,
184
+ getOracleEndpointByCurrency,
185
+ getAllOracleEndpoint,
186
+ getOracleEndpointById,
187
+ getAllOracleEndpointsByMatchCondition,
188
+ createOracleEndpoint,
189
+ updateOracleEndpointById,
190
+ setIsActiveOracleEndpoint,
191
+ destroyOracleEndpointById
192
+ }
@@ -0,0 +1,108 @@
1
+ /*****
2
+ License
3
+ --------------
4
+ Copyright © 2017 Bill & Melinda Gates Foundation
5
+ The Mojaloop files are made available by the Bill & Melinda Gates Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at
6
+ http://www.apache.org/licenses/LICENSE-2.0
7
+ Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
8
+ Contributors
9
+ --------------
10
+ This is the official list of the Mojaloop project contributors for this file.
11
+ Names of the original copyright holders (individuals or organizations)
12
+ should be listed with a '*' in the first column. People who have
13
+ contributed from an organization can be listed under the organization
14
+ that actually holds the copyright for their contributions (see the
15
+ Gates Foundation organization for an example). Those individuals should have
16
+ their names indented and be marked with a '-'. Email address can be added
17
+ optionally within square brackets <email>.
18
+ * Gates Foundation
19
+ * Name Surname <name.surname@gatesfoundation.com>
20
+
21
+ * Kevin Leyow <kevin.leyow@infitx.com>
22
+
23
+ --------------
24
+ ******/
25
+
26
+ 'use strict'
27
+
28
+ const ErrorHandler = require('@mojaloop/central-services-error-handling')
29
+ const Cache = require('../../lib/cache')
30
+ const Metrics = require('@mojaloop/central-services-metrics')
31
+ const OracleEndpointUncached = require('./oracleEndpoint')
32
+
33
+ let cacheClient
34
+
35
+ const getCacheKey = (params) => {
36
+ return cacheClient.createKey(`${Object.values(params).join('__')}`)
37
+ }
38
+
39
+ const getOracleEndpointCached = async (params) => {
40
+ const histTimer = Metrics.getHistogram(
41
+ 'model_oracleEndpoints',
42
+ 'model_getOracleEndpointsCached - Metrics for oracle endpoints cached model',
43
+ ['success', 'queryName', 'hit']
44
+ ).startTimer()
45
+ const partyIdType = params.partyIdType || null
46
+ const currency = params.currency || null
47
+
48
+ // Do we have valid participants list in the cache ?
49
+ const cacheKey = getCacheKey(params)
50
+ let cachedEndpoints = cacheClient.get(cacheKey)
51
+ if (!cachedEndpoints) {
52
+ // No oracleEndpoint in the cache, so fetch from participant API
53
+ let oracleEndpoints
54
+ if (partyIdType && currency) {
55
+ oracleEndpoints = await OracleEndpointUncached.getOracleEndpointByTypeAndCurrency(partyIdType, currency)
56
+ } else if (currency) {
57
+ oracleEndpoints = await OracleEndpointUncached.getOracleEndpointByCurrency(currency)
58
+ } else {
59
+ oracleEndpoints = await OracleEndpointUncached.getOracleEndpointByType(partyIdType)
60
+ }
61
+ // store in cache
62
+ cacheClient.set(cacheKey, oracleEndpoints)
63
+ cachedEndpoints = oracleEndpoints
64
+ histTimer({ success: true, queryName: 'model_getOracleEndpointCached', hit: false })
65
+ } else {
66
+ // unwrap oracleEndpoints list from catbox structure
67
+ cachedEndpoints = cachedEndpoints.item
68
+ histTimer({ success: true, queryName: 'model_getOracleEndpointCached', hit: true })
69
+ }
70
+ return cachedEndpoints
71
+ }
72
+
73
+ /*
74
+ Public API
75
+ */
76
+ exports.initialize = async () => {
77
+ /* Register as cache client */
78
+ const oracleEndpointCacheClientMeta = {
79
+ id: 'oracleEndpoints',
80
+ preloadCache: async () => Promise.resolve()
81
+ }
82
+
83
+ cacheClient = Cache.registerCacheClient(oracleEndpointCacheClientMeta)
84
+ }
85
+
86
+ exports.getOracleEndpointByTypeAndCurrency = async (partyIdType, currency) => {
87
+ try {
88
+ return await getOracleEndpointCached({ partyIdType, currency })
89
+ } catch (err) {
90
+ throw ErrorHandler.Factory.reformatFSPIOPError(err)
91
+ }
92
+ }
93
+
94
+ exports.getOracleEndpointByType = async (partyIdType) => {
95
+ try {
96
+ return await getOracleEndpointCached({ partyIdType })
97
+ } catch (err) {
98
+ throw ErrorHandler.Factory.reformatFSPIOPError(err)
99
+ }
100
+ }
101
+
102
+ exports.getOracleEndpointByCurrency = async (currency) => {
103
+ try {
104
+ return await getOracleEndpointCached({ currency })
105
+ } catch (err) {
106
+ throw ErrorHandler.Factory.reformatFSPIOPError(err)
107
+ }
108
+ }