Skip to content

Commit

Permalink
feat: add post consentRequests and put consentsRequests/{ID} handler (#…
Browse files Browse the repository at this point in the history
…31)

* feat: add post consentrequests handler

* chore: add docuemntation

* feat: add consentRequests/{ID} handler

* chore: update snippets

* chore: edit description

* chore: add put consentRequests/id to handler

* chore: init test files

* chore: add initial tests

* chore: fix imports

* chore: remove comment and update package-lock

* chore: change files

* chore: change import

* chore: remove comment

* chore: add back comments

* chore: add bdd tests

* chore: update interface

* chore: update tests

* chore: update types
  • Loading branch information
kleyow authored Sep 28, 2020
1 parent e877733 commit fde0076
Show file tree
Hide file tree
Showing 18 changed files with 2,636 additions and 507 deletions.
951 changes: 553 additions & 398 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"@hapi/hapi": "^19.1.1",
"@hapi/inert": "^6.0.1",
"@hapi/vision": "^6.0.0",
"@mojaloop/api-snippets": "^2.0.0",
"@mojaloop/api-snippets": "11.0.15",
"@mojaloop/central-services-error-handling": "10.6.0",
"@mojaloop/central-services-health": "10.6.0",
"@mojaloop/central-services-logger": "10.6.0",
Expand Down
120 changes: 120 additions & 0 deletions src/domain/consentRequests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*****
License
--------------
Copyright © 2020 Mojaloop Foundation The Mojaloop files are made available by the Mojaloop 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 <email>.
* Gates Foundation
- Name Surname <name.surname@gatesfoundation.com>
- Kevin Leyow <kevin.leyow@modusbox.com>
--------------
******/
import { Util as HapiUtil } from '@hapi/hapi'
import Mustache from 'mustache'
import Logger from '@mojaloop/central-services-logger'

import {
Enum,
Util,
FspEndpointTypesEnum,
RestMethodsEnum
} from '@mojaloop/central-services-shared'
// eslint is complaining about these imports. not sure why.
// eslint-disable-next-line import/no-unresolved
import Config from '~/shared/config'
import { inspect } from 'util'
import {
FSPIOPError,
ReformatFSPIOPError
} from '@mojaloop/central-services-error-handling'
// eslint-disable-next-line import/no-unresolved
import { finishChildSpan } from '~/shared/util'
// eslint-disable-next-line import/no-unresolved
import * as types from '~/interface/types'
// eslint-disable-next-line import/no-unresolved
import { forwardConsentRequestsIdRequestError } from '~/domain/consentRequests/{ID}'

/**
* @function forwardConsentRequestsRequest
* @description Forwards a /consentRequests request
* @param {string} path Callback endpoint path
* @param {FspEndpointTypesEnum} path Callback endpoint template
* @param {HapiUtil.Dictionary<string>} headers Headers object of the request
* @param {RestMethodsEnum} method The http method POST or PUT
* @param {object} payload Body of the request
* @param {object} span optional request span
* @throws {FSPIOPError} Will throw an error if no endpoint to forward the consentRequests requests is
* found, if there are network errors or if there is a bad response
* @returns {Promise<void>}
*/
export async function forwardConsentRequestsRequest (
path: string,
endpointType: FspEndpointTypesEnum,
headers: HapiUtil.Dictionary<string>,
method: RestMethodsEnum,
payload: types.ConsentRequestsPayload,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
span?: any): Promise<void> {
const childSpan = span?.getChild('forwardConsentRequestsRequest')
const sourceDfspId = headers[Enum.Http.Headers.FSPIOP.SOURCE]
const destinationDfspId = headers[Enum.Http.Headers.FSPIOP.DESTINATION]
try {
const endpoint = await Util.Endpoints.getEndpoint(
Config.ENDPOINT_SERVICE_URL,
destinationDfspId,
endpointType
)
Logger.info(`consentRequest::forwardConsentRequestsRequest - Resolved destination party ${endpointType} endpoint to: ${inspect(endpoint)}`)
const url: string = Mustache.render(endpoint + path, {})
Logger.info(`consentRequest::forwardConsentRequestsRequest - Forwarding consentRequest to endpoint: ${url}`)

await Util.Request.sendRequest(
url,
headers,
sourceDfspId,
destinationDfspId,
method,
payload,
Enum.Http.ResponseTypes.JSON,
childSpan
)

Logger.info(`consentRequest::forwardConsentRequestsRequest - Forwarded consentRequest: from ${sourceDfspId} to ${destinationDfspId}`)
if (childSpan && !childSpan.isFinished) {
childSpan.finish()
}
} catch (err) {
Logger.error(`consentRequest::forwardConsentRequestsRequest - Error forwarding consentRequest to endpoint: ${inspect(err)}`)
const errorHeaders = {
...headers,
'fspiop-source': Enum.Http.Headers.FSPIOP.SWITCH.value,
'fspiop-destination': sourceDfspId
}
const fspiopError: FSPIOPError = ReformatFSPIOPError(err)
await forwardConsentRequestsIdRequestError(
Enum.EndPoints.FspEndpointTemplates.TP_CONSENT_REQUEST_PUT_ERROR,
payload.id,
errorHeaders,
fspiopError.toApiErrorObject(Config.ERROR_HANDLING.includeCauseExtension, Config.ERROR_HANDLING.truncateExtensions),
childSpan
)

if (childSpan && !childSpan.isFinished) {
await finishChildSpan(fspiopError, childSpan)
}
throw fspiopError
}
}
25 changes: 25 additions & 0 deletions src/domain/consentRequests/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*****
License
--------------
Copyright © 2020 Mojaloop Foundation The Mojaloop files are made available by the Mojaloop 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 <email>.
* Gates Foundation
- Name Surname <name.surname@gatesfoundation.com>
- Kevin Leyow <kevin.leyow@modusbox.com>
--------------
******/
export * as ConsentRequestsId from './{ID}'
177 changes: 177 additions & 0 deletions src/domain/consentRequests/{ID}.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*****
License
--------------
Copyright © 2020 Mojaloop Foundation The Mojaloop files are made available by the Mojaloop 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 <email>.
* Gates Foundation
- Name Surname <name.surname@gatesfoundation.com>
- Kevin Leyow <kevin.leyow@modusbox.com>
--------------
******/
import { Util as HapiUtil } from '@hapi/hapi'
import Mustache from 'mustache'
import Logger from '@mojaloop/central-services-logger'

import {
Enum,
Util,
FspEndpointTypesEnum,
RestMethodsEnum
} from '@mojaloop/central-services-shared'
import Config from '~/shared/config'
import { inspect } from 'util'
import {
APIErrorObject,
FSPIOPError,
ReformatFSPIOPError
} from '@mojaloop/central-services-error-handling'
import { finishChildSpan } from '~/shared/util'
import * as types from '~/interface/types'

/**
* @function forwardConsentRequestsIdRequest
* @description Forwards a /consentRequests/{ID} request
* @param {string} consentRequestsRequestId ConsentRequest ID
* @param {string} path Callback endpoint path
* @param {FspEndpointTypesEnum} path Callback endpoint template
* @param {HapiUtil.Dictionary<string>} headers Headers object of the request
* @param {RestMethodsEnum} method The http method POST or PUT
* @param {object} payload Body of the request
* @param {object} span optional request span
* @throws {FSPIOPError} Will throw an error if no endpoint to forward the consentRequests requests is
* found, if there are network errors or if there is a bad response
* @returns {Promise<void>}
*/
export async function forwardConsentRequestsIdRequest(
consentRequestsRequestId: string,
path: string,
endpointType: FspEndpointTypesEnum,
headers: HapiUtil.Dictionary<string>,
method: RestMethodsEnum,
payload: types.ConsentRequestsIDPayload,
span?: any): Promise<void> {

const childSpan = span?.getChild('forwardConsentRequestsIDRequest')
const sourceDfspId = headers[Enum.Http.Headers.FSPIOP.SOURCE]
const destinationDfspId = headers[Enum.Http.Headers.FSPIOP.DESTINATION]

try {
const endpoint = await Util.Endpoints.getEndpoint(
Config.ENDPOINT_SERVICE_URL,
destinationDfspId,
endpointType
)
Logger.info(`consentRequest::forwardConsentRequestsIdRequest - Resolved destination party ${endpointType} for consentRequest ${consentRequestsRequestId} endpoint to: ${inspect(endpoint)}`)
const url: string = Mustache.render(endpoint + path, { ID: consentRequestsRequestId })
Logger.info(`consentRequest::forwardConsentRequestsIdRequest - Forwarding consentRequest to endpoint: ${url}`)

await Util.Request.sendRequest(
url,
headers,
sourceDfspId,
destinationDfspId,
method,
payload,
Enum.Http.ResponseTypes.JSON,
childSpan
)

Logger.info(`consentRequest::forwardConsentRequestsIdRequest - Forwarded consentRequest: ${consentRequestsRequestId} from ${sourceDfspId} to ${destinationDfspId}`)
if (childSpan && !childSpan.isFinished) {
childSpan.finish()
}
} catch (err) {
Logger.error(`consentRequest::forwardConsentRequestsIdRequest - Error forwarding consentRequest to endpoint: ${inspect(err)}`)
const errorHeaders = {
...headers,
'fspiop-source': Enum.Http.Headers.FSPIOP.SWITCH.value,
'fspiop-destination': sourceDfspId
}
const fspiopError: FSPIOPError = ReformatFSPIOPError(err)
await forwardConsentRequestsIdRequestError(
Enum.EndPoints.FspEndpointTemplates.TP_CONSENT_REQUEST_PUT_ERROR,
consentRequestsRequestId,
errorHeaders,
fspiopError.toApiErrorObject(Config.ERROR_HANDLING.includeCauseExtension, Config.ERROR_HANDLING.truncateExtensions),
childSpan
)

if (childSpan && !childSpan.isFinished) {
await finishChildSpan(fspiopError, childSpan)
}
throw fspiopError
}
}

/**
* @function forwardConsentRequestsIDRequestError
* @description Generic function to handle sending `PUT .../consentRequests/error` back to the FSPIOP-Source
* @param {string} path Callback endpoint path
* @param {string} consentRequestsRequestId the ID of the consentRequest
* @param {HapiUtil.Dictionary<string>} headers Headers object of the request
* @param {APIErrorObject} error Error details
* @param {object} span optional request span
* @throws {FSPIOPError} Will throw an error if no endpoint to forward the transactions requests is
* found, if there are network errors or if there is a bad response
* @returns {Promise<void>}
*/
export async function forwardConsentRequestsIdRequestError(
path: string,
consentRequestsRequestId: string,
headers: HapiUtil.Dictionary<string>,
error: APIErrorObject,
span?: any): Promise<void> {

const childSpan = span?.getChild('forwardConsentRequestsRequestError')
const sourceDfspId = headers[Enum.Http.Headers.FSPIOP.SOURCE]
const destinationDfspId = headers[Enum.Http.Headers.FSPIOP.DESTINATION]
const endpointType = Enum.EndPoints.FspEndpointTypes.TP_CB_URL_CONSENT_REQUEST_PUT_ERROR

try {
const endpoint = await Util.Endpoints.getEndpoint(
Config.ENDPOINT_SERVICE_URL,
destinationDfspId,
endpointType)
Logger.info(`consentRequest::forwardConsentRequestsRequest - Resolved destinationDfsp endpoint: ${endpointType}to: ${inspect(endpoint)}`)
const url: string = Mustache.render(endpoint + path, { ID: consentRequestsRequestId })
Logger.info(`consentRequest::forwardConsentRequestsRequest - Forwarding consentRequest error callback to endpoint: ${url}`)

await Util.Request.sendRequest(
url,
headers,
sourceDfspId,
destinationDfspId,
Enum.Http.RestMethods.PUT,
error,
Enum.Http.ResponseTypes.JSON,
childSpan
)

Logger.info(`consentRequest::forwardConsentRequestsRequest - Forwarded consentRequest error callback: from ${sourceDfspId} to ${destinationDfspId}`)
if (childSpan && !childSpan.isFinished) {
childSpan.finish()
}

} catch (err) {
Logger.error(`consentRequest::forwardConsentRequestsRequest - Error forwarding consentRequest error to endpoint: ${inspect(err)}`)
const fspiopError: FSPIOPError = ReformatFSPIOPError(err)
if (childSpan && !childSpan.isFinished) {
await finishChildSpan(fspiopError, childSpan)
}
throw fspiopError
}
}
Loading

0 comments on commit fde0076

Please sign in to comment.