Skip to content

Commit

Permalink
Implement createSettlementModel admin API#1179 (#578)
Browse files Browse the repository at this point in the history
* Changes:

* 1179-Create SettlementModel API new code

* Address PR comments:
- Used central-services-shared for the enums
- handled duplicate insert to not return internal server error and sql statement
- Fixed validations on the routes.js
- Changed settlementCurrency to currency on the request
- Changed type to ledgerAccountType on the request

* 1179-Create SettlementModel API

Cleaned up the code

* Triggering build

* Applied standard.JS
  • Loading branch information
lazolalucas authored Feb 7, 2020
1 parent 94cc321 commit 07d9044
Show file tree
Hide file tree
Showing 14 changed files with 1,212 additions and 22 deletions.
524 changes: 511 additions & 13 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@mojaloop/central-ledger",
"version": "8.8.2-snapshot",
"description": "Central ledger hosted by a scheme to record and settle transfers",
"description": "Central ledger hosted by a scheme to record and settle transfers.",
"license": "Apache-2.0",
"author": "ModusBox",
"contributors": [
Expand All @@ -10,7 +10,8 @@
"Lewis Daly <lewisd@crosslaketech.com>",
"Miguel de Barros <miguel.debarros@modusbox.com>",
"Rajiv Mothilal <rajiv.mothilal@modusbox.com>",
"Steven Oderayi <steven.oderayi@modusbox.com>"
"Steven Oderayi <steven.oderayi@modusbox.com>",
"Lazola Lucas <lazola.lucas@modusbox.com>"
],
"repository": {
"type": "git",
Expand Down Expand Up @@ -82,7 +83,7 @@
"@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": "8.8.0",
"@mojaloop/central-services-shared": "9.1.0",
"@mojaloop/central-services-stream": "8.7.2",
"@mojaloop/event-sdk": "8.7.0",
"@mojaloop/forensic-logging-client": "8.3.0",
Expand Down
13 changes: 11 additions & 2 deletions seeds/ledgerAccountType.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Name Surname <name.surname@gatesfoundation.com>
* Georgi Georgiev <georgi.georgiev@modusbox.com>
* Lazola Lucas <lazola.lucas@modusbox.com>
--------------
******/

Expand Down Expand Up @@ -52,8 +53,12 @@ const ledgerAccountTypes = [
isSettleable: 1
}
]

exports.seed = async function (knex) {
const ledgerAccountList = ledgerAccountTypes.filter(currentValue => {
return currentValue.isSettleable
}).map(currentValue => {
return currentValue.name
}).sort()
const seed = async function (knex) {
try {
return await knex('ledgerAccountType').insert(ledgerAccountTypes)
} catch (err) {
Expand All @@ -64,3 +69,7 @@ exports.seed = async function (knex) {
}
}
}
module.exports = {
ledgerAccountList,
seed
}
11 changes: 9 additions & 2 deletions seeds/settlementDelay.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Name Surname <name.surname@gatesfoundation.com>
* Georgi Georgiev <georgi.georgiev@modusbox.com>
* Lazola Lucas <lazola.lucas@modusbox.com>
--------------
******/

Expand All @@ -34,8 +35,10 @@ const settlementDelayTypes = [
description: null
}
]

exports.seed = async function (knex) {
const settlementDelayList = settlementDelayTypes.map(currentValue => {
return currentValue.name
}).sort()
const seed = async function (knex) {
try {
return await knex('settlementDelay').insert(settlementDelayTypes)
} catch (err) {
Expand All @@ -46,3 +49,7 @@ exports.seed = async function (knex) {
}
}
}
module.exports = {
settlementDelayList,
seed
}
11 changes: 10 additions & 1 deletion seeds/settlementGranularity.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Name Surname <name.surname@gatesfoundation.com>
* Georgi Georgiev <georgi.georgiev@modusbox.com>
* Lazola Lucas <lazola.lucas@modusbox.com>
--------------
******/

Expand All @@ -34,8 +35,11 @@ const settlementGranularityTypes = [
description: null
}
]
const settlementGranularityList = settlementGranularityTypes.map(currentValue => {
return currentValue.name
}).sort()

exports.seed = async function (knex) {
const seed = async function (knex) {
try {
return await knex('settlementGranularity').insert(settlementGranularityTypes)
} catch (err) {
Expand All @@ -46,3 +50,8 @@ exports.seed = async function (knex) {
}
}
}

module.exports = {
settlementGranularityList,
seed
}
10 changes: 9 additions & 1 deletion seeds/settlementInterchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Name Surname <name.surname@gatesfoundation.com>
* Georgi Georgiev <georgi.georgiev@modusbox.com>
* Lazola Lucas <lazola.lucas@modusbox.com>
--------------
******/

Expand All @@ -34,8 +35,11 @@ const settlementInterchangeTypes = [
description: null
}
]
const settlementInterchangeList = settlementInterchangeTypes.map(currentValue => {
return currentValue.name
}).sort()

exports.seed = async function (knex) {
const seed = async function (knex) {
try {
return await knex('settlementInterchange').insert(settlementInterchangeTypes)
} catch (err) {
Expand All @@ -46,3 +50,7 @@ exports.seed = async function (knex) {
}
}
}
module.exports = {
settlementInterchangeList,
seed
}
57 changes: 57 additions & 0 deletions src/api/settlement/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*****
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>
- Lazola Lucas <lazola.lucas@modusbox.com>
--------------
******/

'use strict'

const SettlementService = require('../../domain/settlement')
const Sidecar = require('../../lib/sidecar')
const ErrorHandler = require('@mojaloop/central-services-error-handling')

const Enum = require('@mojaloop/central-services-shared').Enum.Settlements

const create = async function (request, h) {
Sidecar.logRequest(request)
try {
const settlementGranularity = Enum.SettlementGranularity[request.payload.settlementGranularity]
const settlementInterchange = Enum.SettlementInterchange[request.payload.settlementInterchange]
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.')
}
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)
return h.response().code(201)
}
} catch (err) {
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

module.exports = {
create
}
60 changes: 60 additions & 0 deletions src/api/settlement/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*****
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>
- Lazola Lucas <lazola.lucas@modusbox.com>
--------------
******/

'use strict'

const Handler = require('./handler')
const Joi = require('@hapi/joi')
const currencyList = require('../../../seeds/currency.js').currencyList
const settlementGranularityList = require('../../../seeds/settlementGranularity.js').settlementGranularityList
const settlementInterchangeList = require('../../../seeds/settlementInterchange.js').settlementInterchangeList
const settlementDelayList = require('../../../seeds/settlementDelay.js').settlementDelayList
const ledgerAccountList = require('../../../seeds/ledgerAccountType.js').ledgerAccountList
const tags = ['api', 'settlement']

module.exports = [
{
method: 'POST',
path: '/settlementModel',
handler: Handler.create,
options: {
tags,
payload: {
allow: ['application/json'],
failAction: 'error'
},
validate: {
payload: Joi.object({
name: Joi.string().alphanum().min(2).max(30).required().description('Name of the settlement model'),
settlementGranularity: Joi.string().required().valid(...settlementGranularityList).description('Granularity type for the settlement model GROSS or NET'),
settlementInterchange: Joi.string().required().valid(...settlementInterchangeList).description('Interchange type for the settlement model BILATERAL or MULTILATERAL'),
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')
})
}
}
}
]
60 changes: 60 additions & 0 deletions src/domain/settlement/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*****
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>
- Lazola Lucas <lazola.lucas@modusbox.com>
--------------
******/

'use strict'

const SettlementModel = require('../../models/settlement/settlement')
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) => {
try {
await SettlementModel.create(name, isActive, settlementGranularityId, settlementInterchangeId, settlementDelayId, currencyId, requireLiquidityCheck, ledgerAccountTypeId)
return true
} catch (err) {
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}
const getByName = async (name) => {
try {
return await SettlementModel.getByName(name)
} catch (err) {
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

const getLedgerAccountTypeName = async (name) => {
try {
return await LedgerAccountTypeModel.getLedgerAccountByName(name)
} catch (err) {
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

module.exports = {
createSettlementModel,
getLedgerAccountTypeName,
getByName
}
53 changes: 53 additions & 0 deletions src/models/settlement/settlement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*****
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>
- 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) => {
try {
return await Db.settlementModel.insert({
name,
isActive,
settlementGranularityId,
settlementInterchangeId,
settlementDelayId,
currencyId,
requireLiquidityCheck,
ledgerAccountTypeId
})
} catch (err) {
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}
exports.getByName = async (name) => {
try {
const result = await Db.settlementModel.find({ name: name })
return result[0]
} catch (err) {
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}
Loading

0 comments on commit 07d9044

Please sign in to comment.