Skip to content

Commit

Permalink
Merge pull request #3 from gibaros/888-UseTransformHeadersFromTheShar…
Browse files Browse the repository at this point in the history
…edHttpLibrary

888-UseEnumFromCentralServicesSharedLib
  • Loading branch information
gibaros authored Aug 28, 2019
2 parents 77b900b + 2682b4d commit 859adc5
Show file tree
Hide file tree
Showing 14 changed files with 695 additions and 1,141 deletions.
932 changes: 647 additions & 285 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "transaction-requests-service",
"description": "An asynchronous pass through transaction request API for merchant payment initiated requests.",
"license": "Apache-2.0",
"version": "7.3.0",
"version": "7.4.1",
"author": "ModusBox",
"contributors": [
"Rajiv Mothilal <rajiv.mothilal@modusbox.com>"
Expand Down Expand Up @@ -44,34 +44,34 @@
]
},
"dependencies": {
"@hapi/good": "8.2.0",
"@hapi/hapi": "18.3.1",
"@hapi/good": "8.2.1",
"@hapi/hapi": "18.3.2",
"@hapi/inert": "5.2.1",
"@hapi/vision": "5.5.2",
"@mojaloop/central-services-health": "7.0.0",
"@mojaloop/central-services-metrics": "5.2.0",
"@mojaloop/central-services-shared": "7.2.0",
"@mojaloop/central-services-error-handling": "7.3.0",
"@mojaloop/central-services-shared": "7.4.5",
"@mojaloop/central-services-error-handling": "7.4.0",
"axios": "0.19.0",
"blipp": "4.0.0",
"catbox": "10.0.6",
"catbox-memory": "4.0.1",
"docdash": "1.1.1",
"glob": "7.1.4",
"hapi-openapi": "1.2.2",
"hapi-swagger": "10.0.2",
"hapi-swagger": "10.1.0",
"immutable": "3.8.2",
"jsdoc": "3.6.3",
"mustache": "3.0.1",
"mustache": "3.0.2",
"uuid4": "1.1.4"
},
"devDependencies": {
"ava": "2.2.0",
"eslint": "6.1.0",
"ava": "2.3.0",
"eslint": "6.2.2",
"nyc": "14.1.1",
"proxyquire": "2.1.3",
"sinon": "7.4.1",
"standard": "13.1.0",
"standard": "14.0.2",
"swagmock": "1.0.0",
"tap-xunit": "2.4.1"
},
Expand Down
56 changes: 24 additions & 32 deletions src/domain/transactionRequests/transactionRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,59 +23,54 @@
******/
'use strict'

const Enum = require('../../lib/enum')
const participantEndpointModel = require('../../models/participantEndpoint/participantEndpoint')
const Errors = require('../../lib/errors')
const Enum = require('@mojaloop/central-services-shared').Enum
const Endpoint = require('@mojaloop/central-services-shared').Util.Endpoints
const Logger = require('@mojaloop/central-services-shared').Logger
const util = require('util')
const Mustache = require('mustache')
const Utils = require('../../lib/util')
const requests = require('../../lib/request')
const requests = require('@mojaloop/central-services-shared').Util.Request
const ErrorHandler = require('@mojaloop/central-services-error-handling')
const Config = require('../../lib/config.js')

/**
* Forwards transactionRequests endpoint requests to destination FSP for processing
*
* @returns {undefined}
*/
const forwardTransactionRequest = async (request, path) => {
const forwardTransactionRequest = async (path, headers, method, params, payload) => {
let endpoint
const fspiopSource = request.headers['fspiop-source']
const fspiopDest = request.headers['fspiop-destination']
const payload = request.payload || { transactionRequestId: request.params.ID }
const fspiopSource = headers[Enum.Http.Headers.FSPIOP.SOURCE]
const fspiopDest = headers[Enum.Http.Headers.FSPIOP.DESTINATION]
const payloadLocal = payload || { transactionRequestId: params.ID }
try {
endpoint = await participantEndpointModel.getEndpoint(fspiopDest, Enum.endpointTypes.FSPIOP_CALLBACK_URL_TRANSACTION)
Logger.info(`Resolved PAYER party ${Enum.endpointTypes.FSPIOP_CALLBACK_URL_TRANSACTION} endpoint for transactionRequest ${payload.transactionRequestId || 'error.test.js'} to: ${util.inspect(endpoint)}`)
endpoint = await Endpoint.getEndpoint(Config.SWITCH_ENDPOINT, fspiopDest, Enum.EndPoints.FspEndpointTypes.FSPIOP_CALLBACK_URL_TRANSACTION)
Logger.info(`Resolved PAYER party ${Enum.EndPoints.FspEndpointTypes.FSPIOP_CALLBACK_URL_TRANSACTION} endpoint for transactionRequest ${payload.transactionRequestId || 'error.test.js'} to: ${util.inspect(endpoint)}`)
if (!endpoint) {
// we didnt get an endpoint for the payee dfsp!
// make an error callback to the initiator
throw new Errors.FSPIOPError(request.method.toUpperCase() !== Enum.restMethods.GET ? payload : undefined,
`No ${Enum.endpointTypes.FSPIOP_CALLBACK_URL_TRANSACTION} endpoint found for transactionRequest ${payload.transactionRequestId} for ${fspiopDest}`, fspiopSource,
Errors.ApiErrorCodes.DESTINATION_FSP_ERROR)
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.DESTINATION_FSP_ERROR, `No ${Enum.EndPoints.FspEndpointTypes.FSPIOP_CALLBACK_URL_TRANSACTION} endpoint found for transactionRequest ${payload.transactionRequestId} for ${Enum.Http.Headers.FSPIOP.DESTINATION}`, request.method.toUpperCase() !== Enum.Http.RestMethods.GET ? payload : undefined, fspiopSource)
}
const fullUrl = Mustache.render(endpoint + path, {
ID: payload.transactionRequestId || request.params.ID
ID: payloadLocal.transactionRequestId || params.ID
})
Logger.info(`Forwarding transaction request to endpoint: ${fullUrl}`)
// Network errors lob an exception. Bare in mind 3xx 4xx and 5xx are not network errors
// so we need to wrap the request below in a `try catch` to handle network errors
let res
try {
res = await requests.sendRequest(fullUrl, Utils.generateRequestHeaders(request.headers), request.method, request.method.toUpperCase() !== Enum.restMethods.GET ? payload : undefined)
res = await requests.sendRequest(fullUrl, headers, fspiopSource, fspiopDest, method, method.toUpperCase() !== Enum.Http.RestMethods.GET ? payloadLocal : undefined)
} catch (e) {
throw new Errors.FSPIOPError('Network error', `Network error forwarding quote request: ${e.stack || util.inspect(e)}`,
fspiopSource, Errors.ApiErrorCodes.DESTINATION_COMMUNICATION_ERROR)
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.DESTINATION_COMMUNICATION_ERROR, `Network error forwarding quote request: ${e.stack || util.inspect(e)}`, 'Network error', fspiopSource)
}
Logger.info(`Forwarding transaction request ${payload.transactionRequestId} from ${fspiopSource} to ${fspiopDest} got response ${res.status} ${res.statusText}`)
// handle non network related errors below
if (!res.ok) {
throw new Errors.FSPIOPError(res.statusText, 'Got non-success response forwarding transaction request',
fspiopSource, Errors.ApiErrorCodes.DESTINATION_COMMUNICATION_ERROR)
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.DESTINATION_COMMUNICATION_ERROR, 'Got non-success response forwarding transaction request', res.statusText, fspiopSource)
}
return true
} catch (err) {
Logger.info(`Error forwarding transaction request to endpoint ${endpoint}: ${err.stack || util.inspect(err)}`)
forwardTransactionRequestError(request.headers, fspiopSource, Enum.endpoints.TRANSACTION_REQUEST_PUT_ERROR, Enum.restMethods.PUT, request.params.ID, err)
forwardTransactionRequestError(headers, Enum.Http.Headers.FSPIOP.SOURCE, Enum.EndPoints.FspEndpointTemplates.TRANSACTION_REQUEST_PUT_ERROR, Enum.Http.RestMethods.PUT, params.ID, err)
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}
Expand All @@ -87,17 +82,16 @@ const forwardTransactionRequest = async (request, path) => {
*/
const forwardTransactionRequestError = async (headers, to, path, method, transactionRequestId, payload) => {
let endpoint
const fspiopSource = headers['fspiop-source']
const fspiopSource = headers[Enum.Http.Headers.FSPIOP.SOURCE]
const fspiopDestination = headers[Enum.Http.Headers.FSPIOP.DESTINATION]
try {
endpoint = await participantEndpointModel.getEndpoint(to, Enum.endpointTypes.FSPIOP_CALLBACK_URL_TRANSACTION)
Logger.info(`Resolved PAYER party ${Enum.endpointTypes.FSPIOP_CALLBACK_URL_TRANSACTION} endpoint for transactionRequest ${transactionRequestId || 'error.test.js'} to: ${util.inspect(endpoint)}`)
endpoint = await Endpoint.getEndpoint(Config.SWITCH_ENDPOINT, to, Enum.EndPoints.FspEndpointTypes.FSPIOP_CALLBACK_URL_TRANSACTION)
Logger.info(`Resolved PAYER party ${Enum.EndPoints.FspEndpointTypes.FSPIOP_CALLBACK_URL_TRANSACTION} endpoint for transactionRequest ${transactionRequestId || 'error.test.js'} to: ${util.inspect(endpoint)}`)

if (!endpoint) {
// we didnt get an endpoint for the payee dfsp!
// make an error callback to the initiator
throw new Errors.FSPIOPError(payload,
`No ${Enum.endpointTypes.FSPIOP_CALLBACK_URL_TRANSACTION} endpoint found for transactionRequest ${transactionRequestId} for ${to}`, fspiopSource,
Errors.ApiErrorCodes.DESTINATION_FSP_ERROR)
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.DESTINATION_FSP_ERROR, `No ${Enum.EndPoints.FspEndpointTypes.FSPIOP_CALLBACK_URL_TRANSACTION} endpoint found for transactionRequest ${transactionRequestId} for ${to}`, payload, fspiopSource)
}
const fullUrl = Mustache.render(endpoint + path, {
ID: transactionRequestId
Expand All @@ -108,17 +102,15 @@ const forwardTransactionRequestError = async (headers, to, path, method, transac
// so we need to wrap the request below in a `try catch` to handle network errors
let res
try {
res = await requests.sendRequest(fullUrl, Utils.generateRequestHeaders(headers), method, payload || undefined)
res = await requests.sendRequest(fullUrl, headers, fspiopSource, fspiopDestination, method, payload || undefined)
} catch (e) {
throw new Errors.FSPIOPError('Network error', `Network error forwarding quote request: ${e.stack || util.inspect(e)}`,
fspiopSource, Errors.ApiErrorCodes.DESTINATION_COMMUNICATION_ERROR)
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.DESTINATION_FSP_ERROR, `Network error forwarding quote request: ${e.stack || util.inspect(e)}`, 'Network error', fspiopSource)
}
Logger.info(`Forwarding transaction request ${transactionRequestId} from ${fspiopSource} to ${to} got response ${res.status} ${res.statusText}`)

// handle non network related errors below
if (!res.ok) {
throw new Errors.FSPIOPError(res.statusText, 'Got non-success response forwarding transaction request',
fspiopSource, Errors.ApiErrorCodes.DESTINATION_COMMUNICATION_ERROR)
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.DESTINATION_COMMUNICATION_ERROR, 'Got non-success response forwarding transaction request', res.statusText, fspiopSource)
}
return true
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/health.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ module.exports = {
* responses: 200, 400, 401, 403, 404, 405, 406, 501, 503
*/
get: defaultHealthHandler(healthCheck)
}
}
6 changes: 3 additions & 3 deletions src/handlers/transactionRequests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const Enum = require('../lib/enum')
const Enum = require('@mojaloop/central-services-shared').Enum
const transactionRequest = require('../domain/transactionRequests/transactionRequests')
/**
* Operations on /transactionRequests
Expand All @@ -14,7 +14,7 @@ module.exports = {
* responses: 202, 400, 401, 403, 404, 405, 406, 501, 503
*/
post: function (request, h) {
transactionRequest.forwardTransactionRequest(request, Enum.endpoints.TRANSACTION_REQUEST_POST)
return h.response().code(202)
transactionRequest.forwardTransactionRequest(Enum.EndPoints.FspEndpointTemplates.TRANSACTION_REQUEST_POST, request.headers, request.method, request.params, request.payload)
return h.response().code(Enum.Http.ReturnCodes.ACCEPTED.CODE)
}
}
10 changes: 5 additions & 5 deletions src/handlers/transactionRequests/{ID}.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const Enum = require('../../lib/enum')
const Enum = require('@mojaloop/central-services-shared').Enum
const transactionRequest = require('../../domain/transactionRequests/transactionRequests')

/**
Expand All @@ -15,8 +15,8 @@ module.exports = {
* responses: 202, 400, 401, 403, 404, 405, 406, 501, 503
*/
get: function (request, h) {
transactionRequest.forwardTransactionRequest(request, Enum.endpoints.TRANSACTION_REQUEST_GET)
return h.response().code(202)
transactionRequest.forwardTransactionRequest(request, Enum.EndPoints.FspEndpointTemplates.TRANSACTION_REQUEST_GET)
return h.response().code(Enum.Http.ReturnCodes.ACCEPTED.CODE)
},
/**
* summary: TransactionRequestsByID
Expand All @@ -26,7 +26,7 @@ module.exports = {
* responses: 200, 400, 401, 403, 404, 405, 406, 501, 503
*/
put: function (request, h) {
transactionRequest.forwardTransactionRequest(request, Enum.endpoints.TRANSACTION_REQUEST_PUT)
return h.response().code(200)
transactionRequest.forwardTransactionRequest(request, Enum.EndPoints.FspEndpointTemplates.TRANSACTION_REQUEST_PUT)
return h.response().code(Enum.Http.ReturnCodes.OK.CODE)
}
}
6 changes: 3 additions & 3 deletions src/handlers/transactionRequests/{ID}/error.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const Enum = require('../../../lib/enum')
const Enum = require('@mojaloop/central-services-shared').Enum
const transactionRequest = require('../../../domain/transactionRequests/transactionRequests')

/**
Expand All @@ -15,7 +15,7 @@ module.exports = {
* responses: 200, 400, 401, 403, 404, 405, 406, 501, 503
*/
put: function (request, h) {
transactionRequest.forwardTransactionRequestError(request.headers, request.headers['fspiop-destination'], Enum.endpoints.TRANSACTION_REQUEST_PUT_ERROR, Enum.restMethods.PUT, request.params.ID, request.payload)
return h.response().code(200)
transactionRequest.forwardTransactionRequestError(request.headers, request.headers['fspiop-destination'], Enum.EndPoints.FspEndpointTemplates.TRANSACTION_REQUEST_PUT_ERROR, Enum.Http.RestMethods.PUT, request.params.ID, request.payload)
return h.response().code(Enum.Http.ReturnCodes.OK.CODE)
}
}
113 changes: 0 additions & 113 deletions src/lib/enum.js

This file was deleted.

Loading

0 comments on commit 859adc5

Please sign in to comment.