Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 2060 remove notifyThirdpartyAboutTransfer #64

Merged
merged 9 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20,802 changes: 20,779 additions & 23 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"@mojaloop/central-services-health": "^11.0.0",
"@mojaloop/central-services-metrics": "^9.5.0",
"@mojaloop/central-services-shared": "^11.5.9",
"@mojaloop/sdk-standard-components": "^15.2.1",
"@mojaloop/sdk-standard-components": "^15.4.0",
"@types/axios": "^0.14.0",
"@types/convict": "^5.2.2",
"@types/hapi": "^18.0.5",
Expand Down Expand Up @@ -142,11 +142,11 @@
"nodemon": "^2.0.7",
"npm-audit-resolver": "^2.2.1",
"npm-check-updates": "^11.1.4",
"openapi-response-validator": "^7.2.3",
"openapi-typescript": "^2.4.2",
"redis-mock": "0.52.0",
"source-map-support": "0.5.19",
"standard-version": "^9.1.1",
"openapi-response-validator": "^7.2.3",
"openapi-typescript": "^2.4.2",
"swagger-cli": "^4.0.4",
"ts-jest": "^26.5.1"
}
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/inbound/thirdpartyRequests/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import { InboundThirdpartyTransactionPostRequest } from '~/models/thirdparty.tra
*/
async function post (_context: Context, request: Request, h: StateResponseToolkit): Promise<ResponseObject> {
const transactionRequest = request.payload as InboundThirdpartyTransactionPostRequest

const pispId = request.headers['FSPIOP-Source']
// input validation - mocked
if (!(await verifyConsentId(transactionRequest.consentId)) ||
!(await verifyPispId(request.headers['FSPIOP-Source'])) ||
Expand All @@ -60,14 +60,14 @@ async function post (_context: Context, request: Request, h: StateResponseToolki

const modelConfig: InboundThridpartyTransactionsModelConfig = {
logger: h.getLogger(),
sdkRequests: h.getSDKRequests(),
sdkOutgoingRequests: h.getSDKOutgoingRequests(),
mojaloopRequests: h.getMojaloopRequests(),
thirdpartyRequests: h.getThirdpartyRequests()
}
const model = new InboundThridpartyTransactionsModel(modelConfig)
// don't await on promise to be resolved
setImmediate(async () => {
const result = await model.requestToPayTransfer(transactionRequest)
const result = await model.requestToPayTransfer(transactionRequest, pispId)
modelConfig.logger.push({ result }).info('requestToPayTransfer done')

// TODO: should we process result somewhere ? PUT ?
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/outbound/thirdpartyTransaction/partyLookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async function post (_context: any, request: Request, h: StateResponseToolkit):
logger: h.getLogger(),
thirdpartyRequests: h.getThirdpartyRequests(),
mojaloopRequests: h.getMojaloopRequests(),
sdkRequests: h.getSDKRequests()
sdkOutgoingRequests: h.getSDKOutgoingRequests()
}
const exists = await existsInKVS(modelConfig)
if (exists) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function post (_context: any, request: Request, h: StateResponseToolkit):
logger: h.getLogger(),
thirdpartyRequests: h.getThirdpartyRequests(),
mojaloopRequests: h.getMojaloopRequests(),
sdkRequests: h.getSDKRequests()
sdkOutgoingRequests: h.getSDKOutgoingRequests()
}

// load model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async function post (_context: any, request: Request, h: StateResponseToolkit):
logger: h.getLogger(),
thirdpartyRequests: h.getThirdpartyRequests(),
mojaloopRequests: h.getMojaloopRequests(),
sdkRequests: h.getSDKRequests()
sdkOutgoingRequests: h.getSDKOutgoingRequests()
}

// load model
Expand Down
35 changes: 21 additions & 14 deletions src/models/inbound/thirdparty.transactions.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
--------------
******/

import { SDKRequests } from '~/shared/sdk-requests'
import { SDKOutgoingRequests } from '~/shared/sdk-outgoing-requests'
import {
Logger as SDKLogger,
MojaloopRequests,
Expand All @@ -36,12 +36,11 @@ import {
OutboundRequestToPayTransferPostRequest,
OutboundRequestToPayTransferPostResponse
} from '../thirdparty.transactions.interface'
import config from '~/shared/config'
import { ThirdpartyTransactionStatus } from '../pispTransaction.interface'

export interface InboundThridpartyTransactionsModelConfig {
logger: SDKLogger.Logger
sdkRequests: SDKRequests
sdkOutgoingRequests: SDKOutgoingRequests
mojaloopRequests: MojaloopRequests
thirdpartyRequests: ThirdpartyRequests
}
Expand All @@ -58,18 +57,23 @@ export class InboundThridpartyTransactionsModel {
return this.config.logger
}

protected get sdkRequests (): SDKRequests {
return this.config.sdkRequests
protected get sdkOutgoingRequests (): SDKOutgoingRequests {
return this.config.sdkOutgoingRequests
}

protected get mojaloopRequests (): MojaloopRequests {
return this.config.mojaloopRequests
}

protected get thirdpartyRequests (): ThirdpartyRequests {
return this.config.thirdpartyRequests
}

// RequestToPay endpoint was used by POC PISPTransaction code
// TODO: use it when PISPMerchantTransaction flow will be implemented in future
async requestToPayTransfer (
inRequest: InboundThirdpartyTransactionPostRequest
inRequest: InboundThirdpartyTransactionPostRequest,
pispId: string
): Promise<OutboundRequestToPayTransferPostResponse> {
this.logger.push({ inRequest }).info('requestToPayTransfer: inRequest')
// propagate make the requestToPayTransfer on outbound sdk-scheme-adapter
Expand Down Expand Up @@ -101,20 +105,23 @@ export class InboundThridpartyTransactionsModel {
}
this.logger.push({ requestToPayTransfer }).info('requestToPayTransfer: requestToPayTransfer')

const response = await this.sdkRequests.requestToPayTransfer(
const response = await this.sdkOutgoingRequests.requestToPayTransfer(
requestToPayTransfer
) as OutboundRequestToPayTransferPostResponse

this.logger.push({ response }).info('requestToPayTransfer: response')

// optionally notify via PATCH
if (config.SHARED.SDK_NOTIFY_ABOUT_TRANSFER_URI) {
const transactionStatus: ThirdpartyTransactionStatus = {
transactionId: inRequest.transactionRequestId,
transactionRequestState: 'ACCEPTED'
}
await this.sdkRequests.notifyThirdpartyAboutTransfer(transactionStatus, inRequest.transactionRequestId)
// notifyThirdpartyAboutTransfer via PATCH
const transactionStatus: ThirdpartyTransactionStatus = {
transactionId: inRequest.transactionRequestId,
transactionRequestState: 'ACCEPTED',
transactionState: 'COMPLETED'
}
await this.thirdpartyRequests.patchThirdpartyRequestsTransactions(
transactionStatus,
inRequest.transactionRequestId,
pispId
)

return response
}
Expand Down
7 changes: 4 additions & 3 deletions src/models/pispTransaction.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { Method } from 'javascript-state-machine'
import { ErrorInformation } from '~/interface/types'
import { ControlledStateMachine, StateData, PersistentModelConfig } from '~/models/persistent.model'
import { PubSub } from '~/shared/pub-sub'
import { SDKRequests } from '~/shared/sdk-requests'
import { SDKOutgoingRequests } from '~/shared/sdk-outgoing-requests'

export enum RequestPartiesInformationState {
COMPLETED = 'COMPLETED',
Expand Down Expand Up @@ -82,7 +82,7 @@ export interface PISPTransactionModelConfig extends PersistentModelConfig {
pubSub: PubSub
thirdpartyRequests: ThirdpartyRequests
mojaloopRequests: MojaloopRequests
sdkRequests: SDKRequests
sdkOutgoingRequests: SDKOutgoingRequests
}

// derived from request body specification
Expand Down Expand Up @@ -123,7 +123,8 @@ export interface ThirdpartyTransactionInitiateResponse {

export interface ThirdpartyTransactionStatus {
transactionId: string
transactionRequestState: 'RECEIVED' | 'PENDING' | 'ACCEPTED' | 'REJECTED'
transactionRequestState: 'RECEIVED' | 'PENDING' | 'ACCEPTED' | 'REJECTED',
transactionState: 'RECEIVED' | 'PENDING' | 'COMPLETED' | 'REJECTED'
}

export interface ThirdpartyTransactionApproveResponse {
Expand Down
8 changes: 4 additions & 4 deletions src/models/pispTransaction.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import {
ThirdpartyTransactionStatus
} from './pispTransaction.interface'
import inspect from '~/shared/inspect'
import { SDKRequests } from '~/shared/sdk-requests'
import { SDKOutgoingRequests } from '~/shared/sdk-outgoing-requests'
import { HTTPResponseError } from '~/shared/http-response-error'

export class InvalidPISPTransactionDataError extends Error {
Expand Down Expand Up @@ -119,8 +119,8 @@ export class PISPTransactionModel
return this.config.mojaloopRequests
}

get sdkRequests (): SDKRequests {
return this.config.sdkRequests
get sdkOutgoingRequests (): SDKOutgoingRequests {
return this.config.sdkOutgoingRequests
}

static notificationChannel (phase: PISPTransactionPhase, transactionRequestId: string): string {
Expand All @@ -139,7 +139,7 @@ export class PISPTransactionModel

try {
// call GET /parties on sdk-scheme-adapter Outbound service
const response = this.data.payeeResolved = await this.sdkRequests.requestPartiesInformation(
const response = this.data.payeeResolved = await this.sdkOutgoingRequests.requestPartiesInformation(
partyIdType, partyIdentifier, partySubIdOrType
) as RequestPartiesInformationResponse

Expand Down
8 changes: 4 additions & 4 deletions src/server/plugins/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { logger } from '~/shared/logger'
import config from '~/shared/config'
import { PISPBackendRequests } from '~/shared/pisp-backend-requests'
import { DFSPBackendRequests } from '~/shared/dfsp-backend-requests'
import { SDKRequests } from '~/shared/sdk-requests'
import { SDKOutgoingRequests } from '~/shared/sdk-outgoing-requests'
import { Scheme } from '~/shared/http-scheme'

export interface StateResponseToolkit extends ResponseToolkit {
Expand All @@ -47,7 +47,7 @@ export interface StateResponseToolkit extends ResponseToolkit {
getWSO2Auth: () => SDK.WSO2Auth
getPISPBackendRequests: () => PISPBackendRequests
getDFSPBackendRequests: () => DFSPBackendRequests
getSDKRequests: () => SDKRequests
getSDKOutgoingRequests: () => SDKOutgoingRequests
}

export const StatePlugin = {
Expand Down Expand Up @@ -132,7 +132,7 @@ export const StatePlugin = {
getScopesPath: config.SHARED.DFSP_BACKEND_GET_SCOPES_PATH
})

const sdkRequests = new SDKRequests({
const sdkOutgoingRequests = new SDKOutgoingRequests({
logger,
dfspId: config.SHARED.DFSP_ID,
uri: config.SHARED.SDK_OUTGOING_URI,
Expand All @@ -156,7 +156,7 @@ export const StatePlugin = {
server.decorate('toolkit', 'getWSO2Auth', (): SDK.WSO2Auth => wso2Auth)
server.decorate('toolkit', 'getPISPBackendRequests', (): PISPBackendRequests => pispBackendRequests)
server.decorate('toolkit', 'getDFSPBackendRequests', (): DFSPBackendRequests => dfspBackendRequests)
server.decorate('toolkit', 'getSDKRequests', (): SDKRequests => sdkRequests)
server.decorate('toolkit', 'getSDKOutgoingRequests', (): SDKOutgoingRequests => sdkOutgoingRequests)

// disconnect from redis when server is stopped
server.events.on('stop', async () => {
Expand Down
30 changes: 6 additions & 24 deletions src/shared/sdk-requests.ts → src/shared/sdk-outgoing-requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,18 @@ import {
RequestPartiesInformationResponse
} from '~/models/pispTransaction.interface'
export interface SDKRequestConfig extends HttpRequestsConfig {
// TODO: SDK Requests Interfaces: MojaloopRequests & ThridpartyRequests
dfspId: string
requestPartiesInformationPath: string
requestToPayTransferPath: string
notifyAboutTransferPath: string
}

/**
* @class SDKRequest
* @class SDKOutgoingRequest
* @description tiny wrapper dedicated to make requests to SDK scheme adapter Outgoing
* and SDK MojaloopRequest/ThirdpartyRequest interfaces
*
*/
export class SDKRequests extends HttpRequests {
export class SDKOutgoingRequests extends HttpRequests {
// we want this constructor for better code support
// eslint-disable-next-line no-useless-constructor
constructor (config: SDKRequestConfig) {
Expand Down Expand Up @@ -86,34 +85,17 @@ export class SDKRequests extends HttpRequests {
// REQUESTS
/**
* TODO: these requests will be used by DFSPTransactionModel
* // these two will be done by calling ThirdpartyRequests interface, so not implemented here
* notifyThirdpartyAboutRejectedAuthorization
* notifyThirdpartyAboutTransfer
* // syncronous calls to SDKOutgoing§
kleyow marked this conversation as resolved.
Show resolved Hide resolved
* requestAuthorization
* requestQuote,
* requestThirdpartyTransaction
* requestTransfer,
* requestVerifyAuthorization
*
* */
async notifyThirdpartyAboutTransfer (
request: ThirdpartyTransactionStatus,
id: string
): Promise<void> {
// TODO: replace by thirdpartyRequests.patchThridpartyRequestTransaction
return this.loggedRequest<void>({
// uri: this.prependScheme(config.SHARED.NOTIFY_ABOUT_TRANSFER_URI.replace('{ID}', id)),
uri: this.prependScheme(this.notifyAboutTransferPath.replace('{ID}', id)),
method: 'PATCH',
body: requests.common.bodyStringifier(request),
headers: {
...this.headers,
// 'fspiop-source': this.config.SHARED.DFSP_ID
'fspiop-source': this.dfspId
},

agent: this.agent
})
}

async requestPartiesInformation (
type: string, id: string, subId?: string
Expand Down Expand Up @@ -141,7 +123,7 @@ export class SDKRequests extends HttpRequests {
})
}

// TODO drop it and replace by requestTransfer
// TODO: drop it and replace by requestTransfer
async requestToPayTransfer (
request: OutboundRequestToPayTransferPostRequest
): Promise<OutboundRequestToPayTransferPostResponse | void> {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/handlers/outbound-pispTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('Outbound PISP transaction handlers', () => {
getLogger: jest.fn(() => mockLogger()),
getThirdpartyRequests: jest.fn(),
getMojaloopRequests: jest.fn(),
getSDKRequests: jest.fn(),
getSDKOutgoingRequests: jest.fn(),
response: jest.fn(() => ({
code: jest.fn((code: number) => ({
statusCode: code
Expand Down
15 changes: 8 additions & 7 deletions test/unit/models/pispTransaction.model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import {
thirdparty as tpAPI
} from '@mojaloop/api-snippets'
import { HTTPResponseError } from '~/shared/http-response-error'
import { SDKRequests } from '~/shared/sdk-requests'
import { SDKOutgoingRequests } from '~/shared/sdk-outgoing-requests'

// mock KVS default exported class
jest.mock('~/shared/kvs')
Expand Down Expand Up @@ -89,12 +89,12 @@ describe('pipsTransactionModel', () => {
getParties: jest.fn(() => Promise.resolve({ statusCode: 202 })),
putAuthorizations: jest.fn(() => Promise.resolve({ statusCode: 202 }))
} as unknown as MojaloopRequests,
sdkRequests: {
sdkOutgoingRequests: {
requestPartiesInformation: jest.fn(() => Promise.resolve({
party: { Iam: 'mocked-party' },
currentStatus: 'COMPLETED'
}))
} as unknown as SDKRequests
} as unknown as SDKOutgoingRequests
}
mocked(modelConfig.pubSub.subscribe).mockImplementationOnce(
(_channel: string, cb: NotificationCallback) => {
Expand Down Expand Up @@ -191,7 +191,7 @@ describe('pipsTransactionModel', () => {

it('should give response properly populated from backendRequests.requestPartiesInformation', async () => {
const model = await create(lookupData, modelConfig)
mocked(modelConfig.sdkRequests.requestPartiesInformation).mockImplementationOnce(() => Promise.resolve({
mocked(modelConfig.sdkOutgoingRequests.requestPartiesInformation).mockImplementationOnce(() => Promise.resolve({
party,
currentState: RequestPartiesInformationState.COMPLETED
}))
Expand Down Expand Up @@ -219,14 +219,14 @@ describe('pipsTransactionModel', () => {
})

// check we made a call to mojaloopRequest.getParties
expect(modelConfig.sdkRequests.requestPartiesInformation).toBeCalledWith(
expect(modelConfig.sdkOutgoingRequests.requestPartiesInformation).toBeCalledWith(
'party-id-type', 'party-identifier', undefined
)
})

it('should handle error', async () => {
mocked(
modelConfig.sdkRequests.requestPartiesInformation
modelConfig.sdkOutgoingRequests.requestPartiesInformation
).mockImplementationOnce(
() => {
const err = new HTTPResponseError({
Expand Down Expand Up @@ -415,7 +415,8 @@ describe('pipsTransactionModel', () => {

const transactionStatus: ThirdpartyTransactionStatus = {
transactionId: '5678-5678',
transactionRequestState: 'ACCEPTED'
transactionRequestState: 'ACCEPTED',
transactionState: 'COMPLETED'
}

beforeEach(async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/server/plugins/state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('StatePlugin', () => {
expect(ServerMock.decorate.mock.calls[7][0]).toEqual('toolkit')
expect(ServerMock.decorate.mock.calls[7][1]).toEqual('getDFSPBackendRequests')
expect(ServerMock.decorate.mock.calls[8][0]).toEqual('toolkit')
expect(ServerMock.decorate.mock.calls[8][1]).toEqual('getSDKRequests')
expect(ServerMock.decorate.mock.calls[8][1]).toEqual('getSDKOutgoingRequests')

// check listener registration on 'stop' event
expect(ServerMock.events.on).toBeCalledTimes(1)
Expand Down
Loading