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/1211 Settlement v2 Auto Position Reset #586

Merged
merged 3 commits into from
Feb 11, 2020
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
1 change: 1 addition & 0 deletions migrations/401300_settlementModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ exports.up = async (knex, Promise) => {
t.boolean('requireLiquidityCheck').defaultTo(true).notNullable()
t.integer('ledgerAccountTypeId').unsigned().notNullable()
t.foreign('ledgerAccountTypeId').references('ledgerAccountTypeId').inTable('ledgerAccountType')
t.boolean('autoPositionReset').defaultTo(false).notNullable()
})
}
})
Expand Down
644 changes: 536 additions & 108 deletions package-lock.json

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,54 +79,54 @@
"@hapi/vision": "5.5.4",
"@mojaloop/central-object-store": "8.5.0-snapshot",
"@mojaloop/central-services-database": "8.2.1",
"@mojaloop/central-services-error-handling": "8.6.2",
"@mojaloop/central-services-error-handling": "8.7.0",
"@mojaloop/central-services-health": "8.3.0",
"@mojaloop/central-services-logger": "8.6.0",
"@mojaloop/central-services-metrics": "8.3.0",
"@mojaloop/central-services-shared": "9.1.0",
"@mojaloop/central-services-metrics": "8.8.0",
"@mojaloop/central-services-shared": "9.1.1",
"@mojaloop/central-services-stream": "8.7.2",
"@mojaloop/event-sdk": "8.7.0",
"@mojaloop/forensic-logging-client": "8.3.0",
"@mojaloop/ml-number": "8.2.0",
"@now-ims/hapi-now-auth": "2.0.0",
"@now-ims/hapi-now-auth": "2.0.1",
"awaitify-stream": "1.0.2",
"base64url": "3.0.1",
"blipp": "4.0.1",
"catbox-memory": "4.0.1",
"commander": "4.1.0",
"cron": "1.7.2",
"commander": "4.1.1",
"cron": "1.8.2",
"decimal.js": "10.2.0",
"docdash": "1.1.1",
"docdash": "1.2.0",
"five-bells-condition": "5.0.1",
"glob": "7.1.6",
"hapi-auth-basic": "5.0.0",
"hapi-auth-bearer-token": "6.2.1",
"hapi-swagger": "11.1.0",
"knex": "0.20.7",
"knex": "0.20.9",
"lodash": "4.17.15",
"moment": "2.24.0",
"mongoose": "5.8.7",
"mongoose": "5.8.11",
"npm-run-all": "4.1.5",
"rc": "1.2.8",
"require-glob": "3.2.0",
"uuid4": "1.1.4"
},
"optionalDependencies": {
"mysql": "2.17.1"
"mysql": "2.18.1"
},
"devDependencies": {
"async-retry": "1.3.1",
"faucet": "0.0.1",
"get-port": "5.1.0",
"get-port": "5.1.1",
"jsdoc": "3.6.3",
"jsonpath": "1.0.2",
"nodemon": "2.0.2",
"npm-audit-resolver": "2.1.0",
"npm-audit-resolver": "2.2.0",
"npm-check-updates": "4.0.1",
"nyc": "15.0.0",
"pre-commit": "1.2.2",
"proxyquire": "2.1.3",
"sinon": "8.0.4",
"sinon": "8.1.1",
"standard": "14.3.1",
"tap-xunit": "2.4.1",
"tape": "4.13.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
* Gates Foundation
- Name Surname <name.surname@gatesfoundation.com>

* ModusBox
- Georgi Georgiev <georgi.georgiev@modusbox.com>
- Lazola Lucas <lazola.lucas@modusbox.com>
--------------
******/

'use strict'

const SettlementService = require('../../domain/settlement')
Expand All @@ -38,13 +39,13 @@ const create = async function (request, h) {
const settlementDelay = Enum.SettlementDelay[request.payload.settlementDelay]
const ledgerAccountType = await SettlementService.getLedgerAccountTypeName(request.payload.ledgerAccountType)
if (!ledgerAccountType) {
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.ADD_PARTY_INFO_ERROR, 'Ledger account type was not found.')
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.ADD_PARTY_INFO_ERROR, 'Ledger account type was not found')
}
const settlementModelExist = await SettlementService.getByName(request.payload.name)
if (settlementModelExist) {
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.CLIENT_ERROR, 'This Settlement Model already exists')
} else {
await SettlementService.createSettlementModel(request.payload.name, true, settlementGranularity, settlementInterchange, settlementDelay, request.payload.currency, request.payload.requireLiquidityCheck, ledgerAccountType.ledgerAccountTypeId)
await SettlementService.createSettlementModel(request.payload.name, true, settlementGranularity, settlementInterchange, settlementDelay, request.payload.currency, request.payload.requireLiquidityCheck, ledgerAccountType.ledgerAccountTypeId, request.payload.autoPositionReset)
return h.response().code(201)
}
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
* Gates Foundation
- Name Surname <name.surname@gatesfoundation.com>

* ModusBox
- Georgi Georgiev <georgi.georgiev@modusbox.com>
- Lazola Lucas <lazola.lucas@modusbox.com>
--------------
******/

'use strict'

const Handler = require('./handler')
Expand All @@ -36,7 +37,7 @@ const tags = ['api', 'settlement']
module.exports = [
{
method: 'POST',
path: '/settlementModel',
path: '/settlementModels',
handler: Handler.create,
options: {
tags,
Expand All @@ -52,7 +53,8 @@ module.exports = [
settlementDelay: Joi.string().required().valid(...settlementDelayList).description('Delay type for the settlement model IMMEDIATE or DEFERRED'),
currency: Joi.string().valid(...currencyList).description('Currency code'),
requireLiquidityCheck: Joi.boolean().required().description('Liquidity Check boolean'),
ledgerAccountType: Joi.string().required().valid(...ledgerAccountList).description('Account type for the settlement model POSITION, SETTLEMENT or INTERCHANGE_FEE')
ledgerAccountType: Joi.string().required().valid(...ledgerAccountList).description('Account type for the settlement model POSITION, SETTLEMENT or INTERCHANGE_FEE'),
autoPositionReset: Joi.boolean().required().description('Automatic position reset setting, which determines whether to execute the settlement transfer or not')
})
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/domain/settlement/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,29 @@
* Gates Foundation
- Name Surname <name.surname@gatesfoundation.com>

* ModusBox
- Georgi Georgiev <georgi.georgiev@modusbox.com>
- Lazola Lucas <lazola.lucas@modusbox.com>
--------------
******/

'use strict'

const SettlementModel = require('../../models/settlement/settlement')
const SettlementModelModel = require('../../models/settlement/settlementModel')
const LedgerAccountTypeModel = require('../../models/ledgerAccountType/ledgerAccountType')

const ErrorHandler = require('@mojaloop/central-services-error-handling')

const createSettlementModel = async (name, isActive = true, settlementGranularityId, settlementInterchangeId, settlementDelayId, currencyId = null, requireLiquidityCheck = true, ledgerAccountTypeId) => {
const createSettlementModel = async (name, isActive = true, settlementGranularityId, settlementInterchangeId, settlementDelayId, currencyId = null, requireLiquidityCheck = true, ledgerAccountTypeId, autoPositionReset = false) => {
try {
await SettlementModel.create(name, isActive, settlementGranularityId, settlementInterchangeId, settlementDelayId, currencyId, requireLiquidityCheck, ledgerAccountTypeId)
await SettlementModelModel.create(name, isActive, settlementGranularityId, settlementInterchangeId, settlementDelayId, currencyId, requireLiquidityCheck, ledgerAccountTypeId, autoPositionReset)
return true
} catch (err) {
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}
const getByName = async (name) => {
try {
return await SettlementModel.getByName(name)
return await SettlementModelModel.getByName(name)
} catch (err) {
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
* Gates Foundation
- Name Surname <name.surname@gatesfoundation.com>

* ModusBox
- Georgi Georgiev <georgi.georgiev@modusbox.com>
- Lazola Lucas <lazola.lucas@modusbox.com>

--------------
******/

'use strict'

const Db = require('../../lib/db')
const ErrorHandler = require('@mojaloop/central-services-error-handling')

exports.create = async (name, isActive, settlementGranularityId, settlementInterchangeId, settlementDelayId, currencyId, requireLiquidityCheck, ledgerAccountTypeId) => {
exports.create = async (name, isActive, settlementGranularityId, settlementInterchangeId, settlementDelayId, currencyId, requireLiquidityCheck, ledgerAccountTypeId, autoPositionReset) => {
try {
return await Db.settlementModel.insert({
name,
Expand All @@ -37,7 +38,8 @@ exports.create = async (name, isActive, settlementGranularityId, settlementInter
settlementDelayId,
currencyId,
requireLiquidityCheck,
ledgerAccountTypeId
ledgerAccountTypeId,
autoPositionReset
})
} catch (err) {
throw ErrorHandler.Factory.reformatFSPIOPError(err)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
/*****
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 <email>.
* Gates Foundation
- Name Surname <name.surname@gatesfoundation.com>

* ModusBox
- Georgi Georgiev <georgi.georgiev@modusbox.com>
- Lazola Lucas <lazola.lucas@modusbox.com>
--------------
******/
'use strict'

const Test = require('tapes')(require('tape'))
const Sinon = require('sinon')

const Logger = require('@mojaloop/central-services-logger')
const Handler = require('../../../../src/api/settlement/handler')
const Handler = require('../../../../src/api/settlementModels/handler')
const Sidecar = require('../../../../src/lib/sidecar')
const Settlement = require('../../../../src/domain/settlement')
const SettlementService = require('../../../../src/domain/settlement')

Test('SettlementModel', settlementModelHandlerTest => {
let sandbox
Expand All @@ -22,9 +47,7 @@ Test('SettlementModel', settlementModelHandlerTest => {
sandbox = Sinon.createSandbox()
sandbox.stub(Sidecar)
sandbox.stub(Logger)
sandbox.stub(Settlement)
// sandbox.stub(Cache)
// Cache.getEnums.returns(Promise.resolve({ POSITION: 1, SETTLEMENT: 2, HUB_RECONCILIATION: 3, HUB_MULTILATERAL_SETTLEMENT: 4, HUB_FEE: 5 }))
sandbox.stub(SettlementService)
test.end()
})

Expand All @@ -42,10 +65,11 @@ Test('SettlementModel', settlementModelHandlerTest => {
settlementDelay: 'IMMEDIATE',
settlementCurrency: 'USD',
requireLiquidityCheck: true,
type: 'POSITION'
type: 'POSITION',
autoPositionReset: true
}
Settlement.getLedgerAccountTypeName.returns(Promise.resolve(ledgerAccountType))
Settlement.getByName.returns(Promise.resolve(false))
SettlementService.getLedgerAccountTypeName.returns(Promise.resolve(ledgerAccountType))
SettlementService.getByName.returns(Promise.resolve(false))
const reply = {
response: () => {
return {
Expand All @@ -67,10 +91,11 @@ Test('SettlementModel', settlementModelHandlerTest => {
settlementDelay: 'IMMEDIATE',
settlementCurrency: 'USD',
requireLiquidityCheck: true,
type: 'POSITION'
type: 'POSITION',
autoPositionReset: true
}
Settlement.getLedgerAccountTypeName.returns(Promise.resolve(ledgerAccountType))
Settlement.getByName.returns(Promise.resolve(true))
SettlementService.getLedgerAccountTypeName.returns(Promise.resolve(ledgerAccountType))
SettlementService.getByName.returns(Promise.resolve(true))
try {
await Handler.create({ payload })
test.fail('Error not thrown')
Expand All @@ -89,16 +114,17 @@ Test('SettlementModel', settlementModelHandlerTest => {
settlementDelay: 'DEFERRED',
settlementCurrency: 'USD',
requireLiquidityCheck: true,
type: 'POSITION'
type: 'POSITION',
autoPositionReset: true
}
Settlement.getLedgerAccountTypeName.returns(Promise.resolve(false))
SettlementService.getLedgerAccountTypeName.returns(Promise.resolve(false))

try {
await Handler.create({ payload })
test.fail('Error not thrown')
} catch (e) {
test.ok(e instanceof Error)
test.equal(e.message, 'Ledger account type was not found.')
test.equal(e.message, 'Ledger account type was not found')
test.end()
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@
* Gates Foundation
- Name Surname <name.surname@gatesfoundation.com>

* ModusBox
- Georgi Georgiev <georgi.georgiev@modusbox.com>
- Lazola Lucas <lazola.lucas@modusbox.com>
--------------
******/

'use strict'

const Test = require('tape')
const Base = require('../../base')
const AdminRoutes = require('../../../../src/api/routes')

Test('test settlementModel routes', async function (assert) {
const req = Base.buildRequest({ url: '/settlementModel', method: 'POST' })
Test('test settlementModels routes', async function (assert) {
const req = Base.buildRequest({ url: '/settlementModels', method: 'POST' })
const server = await Base.setup(AdminRoutes)
const res = await server.inject(req)
assert.ok(res)
Expand Down
Loading