diff --git a/audit-ci.jsonc b/audit-ci.jsonc index 4096eab..e2abbc1 100644 --- a/audit-ci.jsonc +++ b/audit-ci.jsonc @@ -4,7 +4,7 @@ // Only use one of ["low": true, "moderate": true, "high": true, "critical": true] "moderate": true, "allowlist": [ - // dev dependencies our third party dependencies use + // dev dependencies "GHSA-v88g-cgmw-v5xw", "GHSA-93q8-gq69-wqmw", "GHSA-phwq-j96m-2c2q", diff --git a/package-lock.json b/package-lock.json index ae95690..b2c7c7d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "sims", - "version": "12.1.0", + "version": "12.2.1-snapshot.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "sims", - "version": "12.1.0", + "version": "12.2.1-snapshot.1", "license": "Apache-2.0", "dependencies": { "@hapi/boom": "10.0.1", @@ -21,7 +21,7 @@ "@mojaloop/central-services-shared": "18.2.0", "@mojaloop/event-sdk": "^14.0.0", "@mojaloop/sdk-standard-components": "17.1.3", - "axios": "1.6.2", + "axios": "1.6.4", "base64url": "3.0.1", "blipp": "4.0.2", "glob": "10.3.10", @@ -40,7 +40,7 @@ "standard-version": "^9.5.0" }, "engines": { - "node": "=16.x" + "node": "=18.x" } }, "node_modules/@aashutoshrathi/word-wrap": { @@ -1271,6 +1271,16 @@ } } }, + "node_modules/@mojaloop/central-services-shared/node_modules/axios": { + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.2.tgz", + "integrity": "sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==", + "dependencies": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/@mojaloop/central-services-shared/node_modules/yaml": { "version": "2.3.4", "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.3.4.tgz", @@ -2362,11 +2372,11 @@ } }, "node_modules/axios": { - "version": "1.6.2", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.2.tgz", - "integrity": "sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==", + "version": "1.6.4", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.4.tgz", + "integrity": "sha512-heJnIs6N4aa1eSthhN9M5ioILu8Wi8vmQW9iHQ9NUvfkJb0lEEDUiIdQNAuBtfUt3FxReaKdpQA5DbmMOqzF/A==", "dependencies": { - "follow-redirects": "^1.15.0", + "follow-redirects": "^1.15.4", "form-data": "^4.0.0", "proxy-from-env": "^1.1.0" } @@ -5589,9 +5599,9 @@ "integrity": "sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw==" }, "node_modules/follow-redirects": { - "version": "1.15.3", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.3.tgz", - "integrity": "sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==", + "version": "1.15.4", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.4.tgz", + "integrity": "sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==", "funding": [ { "type": "individual", @@ -15450,6 +15460,16 @@ "yaml": "2.3.4" }, "dependencies": { + "axios": { + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.2.tgz", + "integrity": "sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==", + "requires": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "yaml": { "version": "2.3.4", "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.3.4.tgz", @@ -16311,11 +16331,11 @@ "dev": true }, "axios": { - "version": "1.6.2", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.2.tgz", - "integrity": "sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==", + "version": "1.6.4", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.4.tgz", + "integrity": "sha512-heJnIs6N4aa1eSthhN9M5ioILu8Wi8vmQW9iHQ9NUvfkJb0lEEDUiIdQNAuBtfUt3FxReaKdpQA5DbmMOqzF/A==", "requires": { - "follow-redirects": "^1.15.0", + "follow-redirects": "^1.15.4", "form-data": "^4.0.0", "proxy-from-env": "^1.1.0" } @@ -18779,9 +18799,9 @@ "integrity": "sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw==" }, "follow-redirects": { - "version": "1.15.3", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.3.tgz", - "integrity": "sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==" + "version": "1.15.4", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.4.tgz", + "integrity": "sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==" }, "for-each": { "version": "0.3.3", diff --git a/package.json b/package.json index e664100..a31c270 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sims", - "version": "12.1.0", + "version": "12.2.1-snapshot.1", "description": "A super-simple fsp simulator", "main": "src/index.js", "author": "ModusBox", @@ -51,7 +51,7 @@ "@mojaloop/central-services-shared": "18.2.0", "@mojaloop/event-sdk": "^14.0.0", "@mojaloop/sdk-standard-components": "17.1.3", - "axios": "1.6.2", + "axios": "1.6.4", "base64url": "3.0.1", "blipp": "4.0.2", "glob": "10.3.10", diff --git a/src/backend/handler.js b/src/backend/handler.js new file mode 100644 index 0000000..7156e79 --- /dev/null +++ b/src/backend/handler.js @@ -0,0 +1,134 @@ +/***** + License + -------------- + Copyright © 2017 Bill & Melinda Gates Foundation + 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 + http://www.apache.org/licenses/LICENSE-2.0 + 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. + Contributors + -------------- + This is the official list of the Mojaloop project contributors for this file. + Names of the original copyright holders (individuals or organizations) + should be listed with a '*' in the first column. People who have + contributed from an organization can be listed under the organization + that actually holds the copyright for their contributions (see the + Gates Foundation organization for an example). Those individuals should have + their names indented and be marked with a '-'. Email address can be added + optionally within square brackets . + * Gates Foundation + - Aaron Reynoza + -------------- + ******/ + +'use strict' +const NodeCache = require('node-cache') +const requestCache = new NodeCache() +const Logger = require('@mojaloop/central-services-logger') +const Enums = require('@mojaloop/central-services-shared').Enum +const Metrics = require('../lib/metrics') + +const transfersFulfilResponseDisabled = (process.env.TRANSFERS_FULFIL_RESPONSE_DISABLED !== undefined && process.env.TRANSFERS_FULFIL_RESPONSE_DISABLED !== 'false') + +exports.postQuoteRequest = function (req, h) { + const histTimerEnd = Metrics.getHistogram( + 'sim_request', + 'Histogram for Simulator http operations', + ['success', 'fsp', 'operation', 'source', 'destination'] + ).startTimer() + + const metadata = `${req.method} ${req.path}` + const quotesRequest = req.payload + Logger.isInfoEnabled && Logger.info((new Date().toISOString()), ['IN /backend/quoteRequest::'], `received: ${metadata}. `) + Logger.isInfoEnabled && Logger.info(`incoming request: ${quotesRequest.quoteId}`) + + // Saving Incoming request + const incomingRequest = { + headers: req.headers, + data: req.payload + } + requestCache.set(quotesRequest.quoteId, incomingRequest) + + const quotesResponse = { + payeeFspCommissionAmount: quotesRequest.feesCurrency, + payeeFspCommissionAmountCurrency: quotesRequest.feesCurrency, + payeeFspFeeAmount: quotesRequest.feesAmount, + payeeFspFeeAmountCurrency: quotesRequest.feesCurrency, + // Fee currency and currency should be the same in order to have the right value + payeeReceiveAmount: (Number(quotesRequest.amount) - Number(quotesRequest.feesAmount)), + payeeReceiveAmountCurrency: quotesRequest.currency, + quoteId: quotesRequest.quoteId, + transactionId: quotesRequest.transactionId, + transferAmount: quotesRequest.amount, + transferAmountCurrency: quotesRequest.currency, + expiration: new Date(new Date().getTime() + 10000) + } + + histTimerEnd({ success: true, fsp: req.headers['fspiop-destination'], operation: 'postQuoteRequest', source: req.headers['fspiop-source'], destination: req.headers['fspiop-destination'] }) + return h.response(quotesResponse).code(Enums.Http.ReturnCodes.ACCEPTED.CODE) +} + +exports.putQuotes = function (req, h) { + return h.response().code(Enums.Http.ReturnCodes.ACCEPTED.CODE) +} + +exports.postTransfers = async function (req, h) { + const histTimerEnd = Metrics.getHistogram( + 'sim_request', + 'Histogram for Simulator http operations', + ['success', 'fsp', 'operation', 'source', 'destination'] + ).startTimer() + + Logger.isDebugEnabled && Logger.debug(`[cid=${req.payload.transferId}, fsp=${req.headers['fspiop-source']}, source=${req.headers['fspiop-source']}, dest=${req.headers['fspiop-destination']}] ~ Simulator::api::payee::postTransfers - START`) + + const metadata = `${req.method} ${req.path} ${req.payload.transferId}` + Logger.isInfoEnabled && Logger.info(`IN backend/transfers:: received: ${metadata}.`) + + const quotesResponse = { + completedTimestamp: new Date(new Date().getTime() + 10000), + fulfilment: 'string', + homeTransactionId: req.payload.homeR2PTransactionId, + transferState: 'RECEIVED' + } + + if (!transfersFulfilResponseDisabled) { + // Saving Incoming request + const incomingRequest = { + headers: req.headers, + data: req.payload + } + requestCache.set(req.payload.transferId, incomingRequest) + + histTimerEnd({ + success: true, + fsp: req.headers['fspiop-destination'], + operation: 'postTransfers', + source: req.headers['fspiop-source'], + destination: req.headers['fspiop-destination'] + }) + } else { + // Logger.isPerfEnabled && Logger.perf(`[cid=${req.payload.transferId}, fsp=${req.headers['fspiop-source']}, source=${req.headers['fspiop-source']}, dest=${req.headers['fspiop-destination']}] ~ Simulator::api::payee::postTransfers - END`) + histTimerEnd({ + success: true, + fsp: req.headers['fspiop-destination'], + operation: 'postTransfers', + source: req.headers['fspiop-source'], + destination: req.headers['fspiop-destination'] + }) + } + + return h.response(quotesResponse).code(Enums.Http.ReturnCodes.ACCEPTED.CODE) +} + +exports.putTransfers = function (req, h) { + return h.response().code(Enums.Http.ReturnCodes.ACCEPTED.CODE) +} + +exports.getPartiesByTypeAndId = function (req, h) { + return h.response({ + fspId: 'string' + }).code(Enums.Http.ReturnCodes.ACCEPTED.CODE) +} + +exports.putPartiesByTypeAndId = function (req, h) { + return h.response().code(Enums.Http.ReturnCodes.ACCEPTED.CODE) +} diff --git a/src/backend/routes.js b/src/backend/routes.js new file mode 100644 index 0000000..bc7a2e1 --- /dev/null +++ b/src/backend/routes.js @@ -0,0 +1,89 @@ +/***** + License + -------------- + Copyright © 2017 Bill & Melinda Gates Foundation + 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 + http://www.apache.org/licenses/LICENSE-2.0 + 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. + Contributors + -------------- + This is the official list of the Mojaloop project contributors for this file. + Names of the original copyright holders (individuals or organizations) + should be listed with a '*' in the first column. People who have + contributed from an organization can be listed under the organization + that actually holds the copyright for their contributions (see the + Gates Foundation organization for an example). Those individuals should have + their names indented and be marked with a '-'. Email address can be added + optionally within square brackets . + + * Gates Foundation + - Aaron Reynoza + -------------- + ******/ + +const Handler = require('./handler') +const Enum = require('@mojaloop/central-services-shared').Enum +const tags = ['api', 'metadata', Enum.Tags.RouteTags.SAMPLED] + +module.exports = [ + { + method: 'POST', + path: '/backend/quoterequests', + handler: Handler.postQuoteRequest, + options: { + id: `simulator_${__dirname.split('/').pop()}_postQuoteRequest`, + tags, + description: 'Metadata' + } + }, + { + method: 'PUT', + path: '/backend/quotes/{id}', + handler: Handler.putQuotes, + options: { + id: `simulator_${__dirname.split('/').pop()}_putQuotes`, + tags, + description: 'Metadata' + } + }, + { + method: 'POST', + path: '/backend/transfers', + handler: Handler.postTransfers, + options: { + id: `simulator_${__dirname.split('/').pop()}_postTransfers`, + tags, + description: 'Metadata' + } + }, + { + method: 'PUT', + path: '/backend/transfers/{id}', + handler: Handler.putTransfers, + options: { + id: `simulator_${__dirname.split('/').pop()}_putTransfers`, + tags, + description: 'Metadata' + } + }, + { + method: 'GET', + path: '/backend/parties/{type}/{id}', + handler: Handler.getPartiesByTypeAndId, + options: { + id: `simulator_${__dirname.split('/').pop()}_getParty`, + tags, + description: 'Metadata' + } + }, + { + method: 'PUT', + path: '/backend/parties/{type}/{id}', + handler: Handler.putPartiesByTypeAndId, + options: { + id: `simulator_${__dirname.split('/').pop()}_putParty`, + tags, + description: 'Metadata' + } + } +]