@mojaloop/sdk-scheme-adapter 12.3.0 → 13.0.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 (60) hide show
  1. package/.env.example +3 -0
  2. package/CHANGELOG.md +25 -0
  3. package/docker/ml-testing-toolkit/spec_files/api_definitions/fspiop_1.1/trigger_templates/transaction_request_followup.json +2 -2
  4. package/docker/ml-testing-toolkit/spec_files/rules_callback/default.json +7 -7
  5. package/docker/ml-testing-toolkit/spec_files/rules_response/default.json +16 -16
  6. package/docker/ml-testing-toolkit/spec_files/rules_response/default_pisp_rules.json +5 -5
  7. package/docker/ml-testing-toolkit/spec_files/rules_validation/default.json +10 -10
  8. package/package.json +3 -3
  9. package/src/InboundServer/handlers.js +114 -52
  10. package/src/OutboundServer/api.yaml +105 -32
  11. package/src/OutboundServer/api_interfaces/openapi.d.ts +46 -16
  12. package/src/OutboundServer/api_template/components/schemas/accountsResponse.yaml +9 -0
  13. package/src/OutboundServer/api_template/components/schemas/partiesByIdResponse.yaml +10 -3
  14. package/src/OutboundServer/api_template/components/schemas/quotesPostResponse.yaml +42 -34
  15. package/src/OutboundServer/api_template/components/schemas/simpleTransfersPostResponse.yaml +9 -2
  16. package/src/OutboundServer/api_template/components/schemas/transferRequest.yaml +3 -0
  17. package/src/OutboundServer/api_template/components/schemas/transferResponse.yaml +28 -2
  18. package/src/OutboundServer/api_template/components/schemas/transferStatusResponse.yaml +8 -1
  19. package/src/OutboundServer/handlers.js +1 -1
  20. package/src/config.js +1 -1
  21. package/src/lib/model/AccountsModel.js +13 -11
  22. package/src/lib/model/InboundTransfersModel.js +166 -24
  23. package/src/lib/model/OutboundRequestToPayModel.js +5 -6
  24. package/src/lib/model/OutboundRequestToPayTransferModel.js +2 -2
  25. package/src/lib/model/OutboundTransfersModel.js +261 -56
  26. package/src/lib/model/PartiesModel.js +15 -2
  27. package/src/lib/model/common/BackendError.js +28 -4
  28. package/src/lib/model/common/index.js +2 -1
  29. package/test/__mocks__/@mojaloop/sdk-standard-components.js +3 -2
  30. package/test/integration/lib/Outbound/parties.test.js +2 -0
  31. package/test/integration/lib/Outbound/quotes.test.js +2 -0
  32. package/test/integration/lib/Outbound/simpleTransfers.test.js +2 -0
  33. package/test/unit/InboundServer.test.js +9 -9
  34. package/test/unit/TestServer.test.js +11 -13
  35. package/test/unit/api/accounts/data/postAccountsErrorMojaloopResponse.json +11 -3
  36. package/test/unit/api/accounts/data/postAccountsSuccessResponse.json +14 -0
  37. package/test/unit/api/accounts/data/postAccountsSuccessResponseWithError1.json +13 -0
  38. package/test/unit/api/accounts/data/postAccountsSuccessResponseWithError2.json +18 -0
  39. package/test/unit/api/accounts/utils.js +15 -1
  40. package/test/unit/api/transfers/data/getTransfersCommittedResponse.json +18 -15
  41. package/test/unit/api/transfers/data/getTransfersErrorNotFound.json +1 -0
  42. package/test/unit/api/transfers/data/postTransfersErrorMojaloopResponse.json +9 -0
  43. package/test/unit/api/transfers/data/postTransfersErrorTimeoutResponse.json +1 -0
  44. package/test/unit/api/transfers/data/postTransfersSuccessResponse.json +74 -47
  45. package/test/unit/api/transfers/utils.js +85 -4
  46. package/test/unit/data/commonHttpHeaders.json +1 -0
  47. package/test/unit/inboundApi/handlers.test.js +45 -14
  48. package/test/unit/lib/model/AccountsModel.test.js +9 -6
  49. package/test/unit/lib/model/InboundTransfersModel.test.js +210 -30
  50. package/test/unit/lib/model/OutboundRequestToPayModel.test.js +1 -1
  51. package/test/unit/lib/model/OutboundRequestToPayTransferModel.test.js +3 -3
  52. package/test/unit/lib/model/OutboundTransfersModel.test.js +826 -157
  53. package/test/unit/lib/model/PartiesModel.test.js +13 -7
  54. package/test/unit/lib/model/QuotesModel.test.js +8 -2
  55. package/test/unit/lib/model/TransfersModel.test.js +8 -2
  56. package/test/unit/lib/model/data/defaultConfig.json +9 -9
  57. package/test/unit/lib/model/data/mockArguments.json +97 -40
  58. package/test/unit/lib/model/data/payeeParty.json +13 -11
  59. package/test/unit/lib/model/data/quoteResponse.json +36 -25
  60. package/test/unit/lib/model/data/transferFulfil.json +5 -3
@@ -10,6 +10,7 @@
10
10
 
11
11
  'use strict';
12
12
 
13
+ const util = require('util');
13
14
 
14
15
  const {
15
16
  BackendRequests,
@@ -21,6 +22,7 @@ const {
21
22
  Errors,
22
23
  } = require('@mojaloop/sdk-standard-components');
23
24
  const shared = require('./lib/shared');
25
+ const { TransferStateEnum } = require('./common');
24
26
 
25
27
  /**
26
28
  * Models the operations required for performing inbound transfers
@@ -70,6 +72,12 @@ class InboundTransfersModel {
70
72
  });
71
73
  }
72
74
 
75
+ updateStateWithError(err) {
76
+ this.data.lastError = err;
77
+ this.data.currentState = TransferStateEnum.ERROR_OCCURRED;
78
+ return this._save();
79
+ }
80
+
73
81
  /**
74
82
  * Queries the backend API for the specified party and makes a callback to the originator with the result
75
83
  */
@@ -162,7 +170,32 @@ class InboundTransfersModel {
162
170
  * Asks the backend for a response to an incoming quote request and makes a callback to the originator with
163
171
  * the result
164
172
  */
165
- async quoteRequest(quoteRequest, sourceFspId) {
173
+ async quoteRequest(request, sourceFspId) {
174
+ const quoteRequest = request.body;
175
+
176
+ // keep track of our state.
177
+ // note that instances of this model typically only live as long as it takes to
178
+ // handle an incoming request and send a response asynchronously, but we hold onto
179
+ // some state across async ops
180
+
181
+ this.data = {
182
+ // transferId: this follows the slightly dodgy assumption that transferId will be same as this transactionId.
183
+ // so far this has held in moja implementations but may not always be the case. regardless, future FSPIOP API
184
+ // versions MUST deal with this cleanly so we can expect to eliminate this assumption at some point.
185
+ transferId: quoteRequest.transactionId,
186
+ direction: 'INBOUND',
187
+ quoteRequest: {
188
+ headers: request.headers,
189
+ body: request.body
190
+ },
191
+ currentState: TransferStateEnum.QUOTE_REQUEST_RECEIVED,
192
+ initiatedTimestamp: new Date().toISOString(),
193
+ };
194
+
195
+ // persist the transfer record in the cache. if we crash after this at least we will
196
+ // have a record of the request in the cache.
197
+ await this._save();
198
+
166
199
  try {
167
200
  const internalForm = shared.mojaloopQuoteRequestToInternal(quoteRequest);
168
201
 
@@ -189,19 +222,24 @@ class InboundTransfersModel {
189
222
  mojaloopResponse.condition = condition;
190
223
 
191
224
  // now store the fulfilment and the quote data against the quoteId in our cache
192
- await this._cache.set(`quote_${quoteRequest.transactionId}`, {
225
+ this.data.quote = {
193
226
  request: quoteRequest,
194
227
  internalRequest: internalForm,
195
228
  response: response,
196
229
  mojaloopResponse: mojaloopResponse,
197
230
  fulfilment: fulfilment
198
- });
199
-
200
- // now store the quoteResponse data against the quoteId in our cache to be sent as a response to GET /quotes/{ID}
201
- await this._cache.set(`quoteResponse_${quoteRequest.quoteId}`, mojaloopResponse);
231
+ };
232
+ await this._save();
202
233
 
203
234
  // make a callback to the source fsp with the quote response
204
- return this._mojaloopRequests.putQuotes(quoteRequest.quoteId, mojaloopResponse, sourceFspId);
235
+ const res = await this._mojaloopRequests.putQuotes(quoteRequest.quoteId, mojaloopResponse, sourceFspId);
236
+ this.data.quoteResponse = {
237
+ headers: res.originalRequest.headers,
238
+ body: res.originalRequest.body,
239
+ };
240
+ this.data.currentState = TransferStateEnum.WAITING_FOR_QUOTE_ACCEPTANCE;
241
+ await this._save();
242
+ return res;
205
243
  }
206
244
  catch(err) {
207
245
  this._logger.push({ err }).log('Error in quoteRequest');
@@ -257,7 +295,7 @@ class InboundTransfersModel {
257
295
  return 'No response from backend';
258
296
  }
259
297
 
260
- // project our internal quote reponse into mojaloop quote response form
298
+ // project our internal quote response into mojaloop quote response form
261
299
  const mojaloopResponse = shared.internalTransactionRequestResponseToMojaloop(response);
262
300
 
263
301
  // make a callback to the source fsp with the quote response
@@ -277,25 +315,36 @@ class InboundTransfersModel {
277
315
  * Validates an incoming transfer prepare request and makes a callback to the originator with
278
316
  * the result
279
317
  */
280
- async prepareTransfer(prepareRequest, sourceFspId) {
318
+ async prepareTransfer(request, sourceFspId) {
319
+ const prepareRequest = request.body;
281
320
  try {
282
321
  // retrieve our quote data
283
- let quote;
284
-
285
322
  if (this._allowDifferentTransferTransactionId) {
286
323
  const transactionId = this._ilp.getTransactionObject(prepareRequest.ilpPacket).transactionId;
287
- quote = await this._cache.get(`quote_${transactionId}`);
324
+ this.data = await this._load(transactionId);
288
325
  } else {
289
- quote = await this._cache.get(`quote_${prepareRequest.transferId}`);
326
+ this.data = await this._load(prepareRequest.transferId);
290
327
  }
291
328
 
292
- if(!quote) {
329
+ const quote = this.data.quote;
330
+
331
+ if(!this.data || !quote) {
332
+ // If using the sdk-scheme-adapter in place of the deprecated `mojaloop-connector`
333
+ // make sure this is false. Scenarios that use `mojaloop-connector`
334
+ // absolutely requires a previous quote before allowing a transfer to proceed.
335
+ // This is a different to the a typical mojaloop sdk-scheme-adapter setup which allows this as an option.
336
+
293
337
  // Check whether to allow transfers without a previous quote.
294
338
  if(!this._allowTransferWithoutQuote) {
295
339
  throw new Error(`Corresponding quote not found for transfer ${prepareRequest.transferId}`);
296
340
  }
297
341
  }
298
342
 
343
+ // persist our state so we have a record if we crash during processing the prepare
344
+ this.data.prepare = request;
345
+ this.data.currentState = TransferStateEnum.PREPARE_RECEIVED;
346
+ await this._save();
347
+
299
348
  // Calculate or retrieve fulfilment and condition
300
349
  let fulfilment = null;
301
350
  let condition = null;
@@ -313,13 +362,13 @@ class InboundTransfersModel {
313
362
  throw new Error(`ILP condition in transfer prepare for ${prepareRequest.transferId} does not match quote`);
314
363
  }
315
364
 
316
-
317
- if (quote && this._rejectTransfersOnExpiredQuotes) {
365
+ if (this._rejectTransfersOnExpiredQuotes) {
318
366
  const now = new Date().toISOString();
319
367
  const expiration = quote.mojaloopResponse.expiration;
320
368
  if (now > expiration) {
321
369
  const error = Errors.MojaloopApiErrorObjectFromCode(Errors.MojaloopApiErrorCodes.QUOTE_EXPIRED);
322
370
  this._logger.error(`Error in prepareTransfer: quote expired for transfer ${prepareRequest.transferId}, system time=${now} > quote time=${expiration}`);
371
+ await this.updateStateWithError(error);
323
372
  return this._mojaloopRequests.putTransfersError(prepareRequest.transferId, error, sourceFspId);
324
373
  }
325
374
  }
@@ -336,6 +385,7 @@ class InboundTransfersModel {
336
385
  }
337
386
 
338
387
  this._logger.log(`Transfer accepted by backend returning homeTransactionId: ${response.homeTransactionId} for mojaloop transferId: ${prepareRequest.transferId}`);
388
+ this.data.homeTransactionId = response.homeTransactionId;
339
389
 
340
390
  // create a mojaloop transfer fulfil response
341
391
  const mojaloopResponse = {
@@ -350,10 +400,16 @@ class InboundTransfersModel {
350
400
  };
351
401
 
352
402
  // make a callback to the source fsp with the transfer fulfilment
353
- return this._mojaloopRequests.putTransfers(prepareRequest.transferId, mojaloopResponse,
403
+ const res = await this._mojaloopRequests.putTransfers(prepareRequest.transferId, mojaloopResponse,
354
404
  sourceFspId);
355
- }
356
- catch(err) {
405
+ this.data.fulfil = {
406
+ headers: res.originalRequest.headers,
407
+ body: res.originalRequest.body,
408
+ };
409
+ this.data.currentState = this._reserveNotification ? TransferStateEnum.RESERVED : TransferStateEnum.COMPLETED;
410
+ await this._save();
411
+ return res;
412
+ } catch(err) {
357
413
  this._logger.push({ err }).log('Error in prepareTransfer');
358
414
  const mojaloopError = await this._handleError(err);
359
415
  this._logger.push({ mojaloopError }).log(`Sending error response to ${sourceFspId}`);
@@ -717,22 +773,108 @@ class InboundTransfersModel {
717
773
  */
718
774
  async sendNotificationToPayee(body, transferId) {
719
775
  try {
720
- const res = await this._backendRequests.putTransfersNotification(body, transferId);
776
+ // load any cached state for this transfer e.g. quote request/response etc...
777
+ this.data = await this._load(transferId);
778
+
779
+ // if we didnt have anything cached, start from scratch
780
+ if(!this.data) {
781
+ this.data = {};
782
+ }
783
+
784
+ // tag the final notification body on to the state
785
+ this.data.finalNotification = body;
786
+
787
+ if(body.transferState === 'COMMITTED') {
788
+ // if the transfer was successful in the switch, set the overall transfer state to COMPLETED
789
+ this.data.currentState = TransferStateEnum.COMPLETED;
790
+ }
791
+ else {
792
+ // if the final notification has anything other than COMMITTED as the final state, set an error
793
+ // in the transfer state.
794
+ this.data.currentState == TransferStateEnum.ERROR_OCCURED;
795
+ this.data.lastError = 'Final notification state not COMMITTED';
796
+ }
797
+
798
+ await this._save();
799
+
800
+ const res = await this._backendRequests.putTransfersNotification(this.data, transferId);
721
801
  return res;
722
802
  } catch (err) {
723
- this._logger.push({ err }).log('Error in sendNotificationToPayee');
803
+ this._logger.push({ err }).log('Error notifying backend of final transfer state');
724
804
  }
725
805
  }
726
806
 
727
- async _handleError(err, mojaloopErrorCode = Errors.MojaloopApiErrorCodes.INTERNAL_SERVER_ERROR) {
807
+ async _handleError(err) {
808
+ // by default use a generic server error
809
+ let mojaloopError = (new Errors.MojaloopFSPIOPError(err, null, null, Errors.MojaloopApiErrorCodes.INTERNAL_SERVER_ERROR)).toApiErrorObject();
728
810
  if(err instanceof HTTPResponseError) {
811
+ // this is an http response error e.g. from calling DFSP backend
729
812
  const e = err.getData();
730
813
  if(e.res && e.res.data) {
731
- mojaloopErrorCode = Errors.MojaloopApiErrorCodeFromCode(`${e.res.data.statusCode}`);
814
+ // look for a standard mojaloop error that matches the statusCode
815
+ let mojaloopErrorCode = Errors.MojaloopApiErrorCodeFromCode(`${e.res.data.statusCode}`);
816
+ let errorDescription = e.res.data.message;
817
+ if(mojaloopErrorCode) {
818
+ // use the standard mojaloop error object
819
+ mojaloopError = (new Errors.MojaloopFSPIOPError(err, null, null, mojaloopErrorCode)).toApiErrorObject();
820
+ if(errorDescription) {
821
+ // if the error has a description, use that instead of the default mojaloop description
822
+ // note that the mojaloop API spec allows any string up to 128 utf8 characters to be sent
823
+ // in the errorDescription field.
824
+ mojaloopError.errorInformation.errorDescription = errorDescription;
825
+ }
826
+ }
827
+ else {
828
+ // this is a custom error, so construct a mojaloop spec body
829
+ mojaloopError = {
830
+ errorInformation: {
831
+ errorCode: e.res.data.statusCode,
832
+ errorDescription: e.res.data.message,
833
+ }
834
+ };
835
+ }
732
836
  }
733
837
  }
838
+ if(this.data) {
839
+ //we have persisted state so update that with this error
840
+ this.data.lastError = {
841
+ originalError: err.stack || util.inspect(err),
842
+ mojaloopError: mojaloopError,
843
+ };
844
+ this.data.currentState = TransferStateEnum.ERROR_OCCURRED;
845
+ await this._save();
846
+ }
847
+ return mojaloopError;
848
+ }
734
849
 
735
- return new Errors.MojaloopFSPIOPError(err, null, null, mojaloopErrorCode).toApiErrorObject();
850
+ /**
851
+ * Persists the model state to cache for reinstantiation at a later point
852
+ */
853
+ async _save() {
854
+ try {
855
+ const res = await this._cache.set(`transferModel_in_${this.data.transferId}`, this.data);
856
+ this._logger.push({ res }).log('Persisted transfer model in cache');
857
+ }
858
+ catch(err) {
859
+ this._logger.push({ err }).log('Error saving transfer model');
860
+ throw err;
861
+ }
862
+ }
863
+
864
+ /**
865
+ * Loads a transfer model from cache for resumption of the transfer process
866
+ *
867
+ * @param transferId {string} - UUID transferId of the model to load from cache
868
+ */
869
+ async _load(transferId) {
870
+ try {
871
+ const data = await this._cache.get(`transferModel_in_${transferId}`);
872
+ return data;
873
+ }
874
+ catch(err) {
875
+ this._logger.push({ err }).log('Error loading transfer model');
876
+ throw err;
877
+ }
736
878
  }
737
879
  }
738
880
 
@@ -155,17 +155,16 @@ class OutboundRequestToPayModel {
155
155
  // hook up a subscriber to handle response messages
156
156
  const subId = await this._cache.subscribe(payeeKey, (cn, msg, subId) => {
157
157
  try {
158
- let payee = JSON.parse(msg);
159
-
160
- if(payee.errorInformation) {
158
+ this.data.getPartiesResponse = JSON.parse(msg);
159
+ if(this.data.getPartiesResponse.body.errorInformation) {
161
160
  // this is an error response to our GET /parties request
162
- const err = new BackendError(`Got an error response resolving party: ${util.inspect(payee)}`, 500);
163
- err.mojaloopError = payee;
164
-
161
+ const err = new BackendError(`Got an error response resolving party: ${util.inspect(this.data.getPartiesResponse.body, { depth: Infinity })}`, 500);
162
+ err.mojaloopError = this.data.getPartiesResponse.body;
165
163
  // cancel the timeout handler
166
164
  clearTimeout(timeout);
167
165
  return reject(err);
168
166
  }
167
+ let payee = this.data.getPartiesResponse.body;
169
168
 
170
169
  if(!payee.party) {
171
170
  // we should never get a non-error response without a party, but just in case...
@@ -427,8 +427,8 @@ class OutboundRequestToPayTransferModel {
427
427
  return reject(error);
428
428
  }
429
429
 
430
- const quoteResponseBody = message.data;
431
- const quoteResponseHeaders = message.headers;
430
+ const quoteResponseBody = message.data.body;
431
+ const quoteResponseHeaders = message.data.headers;
432
432
  this._logger.push({ quoteResponseBody }).log('Quote response received');
433
433
 
434
434
  this.data.quoteResponse = quoteResponseBody;