-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement createSettlementModel admin API#1179 (#578)
* 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
1 parent
94cc321
commit 07d9044
Showing
14 changed files
with
1,212 additions
and
22 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
} | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.