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

Feature/926 enhanced get settlement windows by params #254

Merged
Show file tree
Hide file tree
Changes from all 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
400 changes: 208 additions & 192 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "central-settlement",
"description": "Central settlements hosted by a scheme to record and make settlements",
"description": "Central settlements hosted by a scheme to record and make settlements.",
"version": "9.2.2-snapshot",
"license": "Apache-2.0",
"private": false,
Expand Down Expand Up @@ -37,6 +37,7 @@
"@mojaloop/central-services-stream": "9.1.0",
"@mojaloop/ml-number": "8.2.0",
"@now-ims/hapi-now-auth": "2.0.1",
"async": "3.2.0",
"async-retry": "1.3.1",
"blipp": "4.0.1",
"hapi-auth-bearer-token": "6.2.1",
Expand Down
13 changes: 11 additions & 2 deletions src/domain/settlementWindow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- Miguel de Barros <miguel.debarros@modusbox.com>
- Rajiv Mothilal <rajiv.mothilal@modusbox.com>
- Valentin Genev <valentin.genev@modusbox.com>
- Lazola Lucas <lazola.lucas@modusbox.com>
--------------
******/

Expand Down Expand Up @@ -58,15 +59,23 @@ module.exports = {

getByParams: async function (params, enums) {
// 4 filters - at least one should be used
if (hasFilters(params.query) && Object.keys(params.query).length < 5) {
if (hasFilters(params.query) && Object.keys(params.query).length < 6) {
lazolalucas marked this conversation as resolved.
Show resolved Hide resolved
const settlementWindows = await SettlementWindowModel.getByParams(params, enums)
if (settlementWindows && settlementWindows.length > 0) {
for (const settlementWindow of settlementWindows) {
const settlementWindowContent = await SettlementWindowContentModel.getBySettlementWindowId(settlementWindow.settlementWindowId)
if (settlementWindowContent) {
settlementWindow.content = settlementWindowContent
} else {
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.INTERNAL_SERVER_ERROR, `No records for settlementWidowContentId : ${settlementWindow.settlementWindowId} found`)
}
}
return settlementWindows
} else {
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, `settlementWindow by filters: ${JSON.stringify(params.query).replace(/"/g, '')} not found`)
}
} else {
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, 'Use at least one parameter: participantId, state, fromDateTime, toDateTime')
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, 'Use at least one parameter: participantId, state, fromDateTime, toDateTime, currency')
}
},

Expand Down
8 changes: 8 additions & 0 deletions src/interface/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,15 @@
"format": "date-time",
"description": "The end date for query (relates to central-ledger.settlementWindow.createdDate). Can be used together with `fromDateTime'. eg 2017-07-21T17:32:28Z\n",
"required": false
},
{
"in": "query",
"name": "currency",
"type": "string",
"description": "A valid currency to filter on.\n",
"required": false
}

],
"tags": [
"getSettlementWindowsByParams"
Expand Down
7 changes: 6 additions & 1 deletion src/models/settlementWindow/facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,14 @@ const Facade = {
},

getByParams: async function ({ query }) {
const { participantId, state, fromDateTime, toDateTime } = query
const { participantId, state, fromDateTime, toDateTime, currency } = query
return Db.settlementWindow.query(builder => {
if (!participantId) {
const b = builder
.leftJoin('settlementWindowStateChange AS swsc', 'swsc.settlementWindowStateChangeId', 'settlementWindow.currentStateChangeId')
.leftJoin('transferFulfilment AS tf', 'tf.settlementWindowId', 'settlementWindow.settlementWindowId')
.leftJoin('transferParticipant AS tp', 'tp.transferId', 'tf.transferId')
.leftJoin('participantCurrency AS pc', 'pc.participantCurrencyId', 'tp.participantCurrencyId')
.select(
'settlementWindow.settlementWindowId',
'swsc.settlementWindowStateId as state',
Expand All @@ -93,6 +96,7 @@ const Facade = {
if (state) { b.where('swsc.settlementWindowStateId', state) }
if (fromDateTime) { b.where('settlementWindow.createdDate', '>=', fromDateTime) }
if (toDateTime) { b.where('settlementWindow.createdDate', '<=', toDateTime) }
if (currency) { b.where('pc.currencyId', currency) }
return b
} else {
const b = builder
Expand All @@ -112,6 +116,7 @@ const Facade = {
if (state) { b.where('swsc.settlementWindowStateId', state) }
if (fromDateTime) { b.where('settlementWindow.createdDate', '>=', fromDateTime) }
if (toDateTime) { b.where('settlementWindow.createdDate', '<=', toDateTime) }
if (currency) { b.where('pc.currencyId', currency) }
return b
}
})
Expand Down
57 changes: 57 additions & 0 deletions test/unit/domain/settlementWindow/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

* Georgi Georgiev <georgi.georgiev@modusbox.com>
* Valentin Genev <valentin.genev@modusbox.com>
* Lazola Lucas <lazola.lucas@modusbox.com>
--------------
******/

Expand Down Expand Up @@ -158,9 +159,13 @@ Test('SettlementWindowService', async (settlementWindowServiceTest) => {
const enums = {}
const options = { logger: Logger }
const settlementWindowsMock = [{ settlementWindowId: 1 }, { settlementWindowId: 2 }]
const settlementWindowMock = { settlementWindowId: 1, content: { id: 11 } }
const settlementWindowContentMock = { id: 11 }

await getByParamsTest.test('return settlement windows', async test => {
try {
SettlementWindowContentModel.getBySettlementWindowId = sandbox.stub().returns(settlementWindowContentMock)
SettlementWindowModel.getById = sandbox.stub().returns(settlementWindowMock)
SettlementWindowModel.getByParams = sandbox.stub().returns(settlementWindowsMock)
const result = await SettlementWindowService.getByParams(params, enums, options)
test.ok(result, 'Result returned')
Expand Down Expand Up @@ -199,6 +204,58 @@ Test('SettlementWindowService', async (settlementWindowServiceTest) => {
}
})

await settlementWindowServiceTest.test('getByParams should fail when no content is found', async getByParamsTest => {
try {
let params = { query: { participantId: 1, state: 'PENDING_SETTLEMENT' } }
const enums = {}
const options = { logger: Logger }
const settlementWindowsMock = [{ settlementWindowId: 1 }, { settlementWindowId: 2 }]
const settlementWindowMock = { settlementWindowId: 1, content: { id: 11 } }

await getByParamsTest.test('return settlement windows', async test => {
try {
SettlementWindowContentModel.getBySettlementWindowId = sandbox.stub().returns(undefined)
SettlementWindowModel.getById = sandbox.stub().returns(settlementWindowMock)
SettlementWindowModel.getByParams = sandbox.stub().returns(settlementWindowsMock)
try {
await SettlementWindowService.getByParams(params, enums, options)
test.fail('Error expected, but not thrown!')
} catch (err) {
test.equal(err.message, 'No records for settlementWidowContentId : 1 found')
}
SettlementWindowModel.getByParams = sandbox.stub().returns()
try {
await SettlementWindowService.getByParams(params, enums)
test.fail('Error expected, but not thrown!')
} catch (err) {
test.ok(err instanceof Error, `Error "${err.message}" thrown as expected`)
test.ok(SettlementWindowModel.getByParams.withArgs(params, enums).calledOnce, 'SettlementWindowModel.getByParams with args ... called once')
}

params = { query: {} }
try {
await SettlementWindowService.getByParams(params, enums)
test.fail('Error expected, but not thrown!')
} catch (err) {
test.pass(`Error "${err.message.substr(0, 50)} ..." thrown as expected`)
}

test.end()
} catch (err) {
Logger.error(`getByParamsTest failed with error - ${err}`)
test.fail()
test.end()
}
})

await getByParamsTest.end()
} catch (err) {
Logger.error(`settlementWindowServiceTest failed with error - ${err}`)
getByParamsTest.fail()
getByParamsTest.end()
}
})

await settlementWindowServiceTest.test('process should', async processTest => {
try {
const params = { id: 1 }
Expand Down
10 changes: 8 additions & 2 deletions test/unit/models/settlementWindow/facade.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ Test('Settlement Window facade', async (settlementWindowFacadeTest) => {
const state = 'PENDING_SETTLEMENT'
const fromDateTime = new Date('01-01-1970').toISOString()
const toDateTime = new Date().toISOString()
let query = { participantId, state, fromDateTime, toDateTime }
const currency = 'USD'
let query = { participantId, state, fromDateTime, toDateTime, currency }
const settlementWindowResultStub = [{
settlementWindowId: 1,
state: 'PENDING_SETTLEMENT'
Expand Down Expand Up @@ -288,6 +289,7 @@ Test('Settlement Window facade', async (settlementWindowFacadeTest) => {
test.ok(whereStub.withArgs('swsc.settlementWindowStateId', state).calledOnce)
test.ok(whereStub.withArgs('settlementWindow.createdDate', '>=', fromDateTime).calledOnce)
test.ok(whereStub.withArgs('settlementWindow.createdDate', '<=', toDateTime).calledOnce)
test.ok(whereStub.withArgs('pc.currencyId', currency).calledOnce)
test.end()
} catch (err) {
Logger.error(`getByParams failed with error - ${err}`)
Expand Down Expand Up @@ -330,10 +332,13 @@ Test('Settlement Window facade', async (settlementWindowFacadeTest) => {
try {
Db.settlementWindow.query.returns(Promise.resolve(settlementWindowResultStub))

query = { state, fromDateTime, toDateTime }
query = { state, fromDateTime, toDateTime, currency }
const result = await SettlementWindowFacade.getByParams({ query }, enums)
test.ok(result, 'Result returned')
test.ok(builderStub.leftJoin.withArgs('settlementWindowStateChange AS swsc', 'swsc.settlementWindowStateChangeId', 'settlementWindow.currentStateChangeId').calledOnce)
test.ok(leftJoin2Stub.withArgs('transferFulfilment AS tf', 'tf.settlementWindowId', 'settlementWindow.settlementWindowId').calledOnce)
test.ok(leftJoin3Stub.withArgs('transferParticipant AS tp', 'tp.transferId', 'tf.transferId').calledOnce)
test.ok(leftJoin4Stub.withArgs('participantCurrency AS pc', 'pc.participantCurrencyId', 'tp.participantCurrencyId').calledOnce)
test.ok(selectStub.withArgs('settlementWindow.settlementWindowId',
'swsc.settlementWindowStateId as state',
'swsc.reason as reason',
Expand All @@ -344,6 +349,7 @@ Test('Settlement Window facade', async (settlementWindowFacadeTest) => {
test.ok(whereStub.withArgs('swsc.settlementWindowStateId', state).calledOnce)
test.ok(whereStub.withArgs('settlementWindow.createdDate', '>=', fromDateTime).calledOnce)
test.ok(whereStub.withArgs('settlementWindow.createdDate', '<=', toDateTime).calledOnce)
test.ok(whereStub.withArgs('pc.currencyId', currency).calledOnce)
test.end()
} catch (err) {
Logger.error(`getByParams failed with error - ${err}`)
Expand Down