Skip to content

Commit

Permalink
Admin API changes (mojaloop#5)
Browse files Browse the repository at this point in the history
* moved requests to models

* spelling mistake

* cleaning up unused code

* removal of hardcoded value to enum

* fixes for aligning ALS with correct flow for sending HTTP requests as well as refactoring and reworking code to be more maintainable. also removed broken code for batch participants. moved endpoint cache to models folders. tried to comment more

* version update

* Admin API functionality added with full CRUD

* refactor

* fix for broken test
  • Loading branch information
rmothilal authored May 10, 2019
1 parent 4d58d2b commit 1599ce3
Show file tree
Hide file tree
Showing 20 changed files with 417 additions and 40 deletions.
5 changes: 4 additions & 1 deletion src/domain/oracle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@

const oracle = require('./oracle')

exports.postOracle = oracle.postOracle
exports.createOracle = oracle.createOracle
exports.getOracle = oracle.getOracle
exports.deleteOracle = oracle.deleteOracle
exports.updateOracle = oracle.updateOracle
116 changes: 112 additions & 4 deletions src/domain/oracle/oracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ const Logger = require('@mojaloop/central-services-shared').Logger
const oracleEndpoint = require('../../models/oracle')
const partyIdType = require('../../models/partyIdType')
const endpointType = require('../../models/endpointType')
const currency = require('../../models/currency')

/**
* @function postOracle
* @function createOracle
*
* @description This creates and entry in the oracleEndpoint table
*
* @param {object} req The request object from the Hapi server
*/
const postOracle = async (req) => {
exports.createOracle = async (req) => {
try {
let oracleEntity = {}
const payload = req.payload
Expand All @@ -62,6 +63,113 @@ const postOracle = async (req) => {
}
}

module.exports = {
postOracle
/**
* @function getOracle
*
* @description Retrieves list of oracles may accept query parameters
*
* @param {object} req The request object from the Hapi server
*/
exports.getOracle = async (req) => {
try {
let oracleEndpointModelList
let isCurrency, isType = false
let oracleList = []
if (req.query.currency) {
isCurrency = true
}
if (req.query.type) {
isType = true
}
if (isCurrency && isType){
oracleEndpointModelList = await oracleEndpoint.getOracleEndpointByTypeAndCurrency(req.query.type, req.query.currency)
} else if (isCurrency && !isType){
oracleEndpointModelList = await oracleEndpoint.getOracleEndpointByCurrency(req.query.currency)
} else if (isType && !isCurrency) {
oracleEndpointModelList = await oracleEndpoint.getOracleEndpointByType(req.query.type)
} else {
oracleEndpointModelList = await oracleEndpoint.getAllOracleEndpoint()
}
for (let oracleEndpointModel of oracleEndpointModelList) {
let oracle = {
oracleId: oracleEndpointModel.oracleEndpointId,
oracleIdType: oracleEndpointModel.idType,
endpoint: {
value: oracleEndpointModel.value,
endpointType: oracleEndpointModel.endpointType
},
currency: oracleEndpointModel.currency,
isDefault: oracleEndpointModel.isDefault
}
oracleList.push(oracle)
}
return oracleList
} catch (e) {
Logger.error(e)
throw e
}
}

/**
* @function updateOracle
*
* @description This process updates properties of oracle entries
*
* @param {object} req The request object from the Hapi server
*/
exports.updateOracle = async (req) => {
try {
const payload = req.payload
const currentOracleEndpointList = await oracleEndpoint.getOracleEndpointById(req.params.ID)
if (currentOracleEndpointList.length > 0) {
let currentOracleEndpoint = currentOracleEndpointList[0]
let newOracleEntry = {}
if (payload.oracleIdType && payload.oracleIdType !== currentOracleEndpoint.idType) {
let partyTypeModel = await partyIdType.getPartyIdTypeByName(payload.oracleIdType)
newOracleEntry.partyIdTypeId = partyTypeModel.partyIdTypeId
}
if (payload.endpoint && payload.endpoint.value && payload.endpoint.value !== currentOracleEndpoint.value) {
newOracleEntry.value = payload.endpoint.value
}
if (payload.endpoint && payload.endpoint.endpointType && payload.endpoint.endpointType !== currentOracleEndpoint.endpointType) {
let endpointTypeModel = await endpointType.getEndpointTypeByType(payload.endpoint.endpointType)
newOracleEntry.endpointTypeId = endpointTypeModel.endpointTypeId
}
if (payload.currency && payload.currency !== currentOracleEndpoint.currency) {
let currencyModel = await currency.getCurrencyById(payload.currency)
if (currencyModel) {
newOracleEntry.currencyId = payload.currency
} else {
throw new Error('Invalid currency code')
}
}
if (payload.isDefault && payload.isDefault !== currentOracleEndpoint.isDefault) {
newOracleEntry.isDefault = payload.isDefault
}
await oracleEndpoint.updateOracleEndpointById(req.params.ID, newOracleEntry)
return true
} else {
return false
}
} catch (e) {
Logger.error(e)
throw e
}
}

/**
* @function deleteOracle
*
* @description This process deletes an oracle endpoint by setting
*
* @param {object} req The request object from the Hapi server
*/
exports.deleteOracle = async (req) => {
try {
await oracleEndpoint.destroyOracleEndpointById(req.params.ID)
return true
} catch (e) {
Logger.error(e)
throw e
}
}
4 changes: 2 additions & 2 deletions src/domain/participants/participants.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const Logger = require('@mojaloop/central-services-shared').Logger
const Enums = require('../../lib/enum')
const util = require('../../lib/util')
const Errors = require('../../lib/error')
const oracle = require('../../models/oracle/oracle')
const oracle = require('../../models/oracle/facade')
const participant = require('../../models/participantEndpoint/facade')

/**
Expand Down Expand Up @@ -217,4 +217,4 @@ module.exports = {
putParticipantsErrorByTypeAndID,
postParticipants,
postParticipantsBatch
}
}
4 changes: 2 additions & 2 deletions src/domain/parties/parties.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const Enums = require('../../lib/enum')
const participant = require('../../models/participantEndpoint/facade')
const util = require('../../lib/util')
const Errors = require('../../lib/error')
const oracle = require('../../models/oracle/oracle')
const oracle = require('../../models/oracle/facade')

/**
* @function getPartiesByTypeAndID
Expand Down Expand Up @@ -131,4 +131,4 @@ module.exports = {
getPartiesByTypeAndID,
putPartiesByTypeAndID,
putPartiesErrorByTypeAndID
}
}
12 changes: 8 additions & 4 deletions src/handlers/oracles.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

'use strict'

const Boom = require('boom')
const oracle = require('../domain/oracle')

/**
Expand All @@ -38,8 +37,13 @@ module.exports = {
* produces: application/json
* responses: 200, 400, 401, 403, 404, 405, 406, 501, 503
*/
get: function OracleGet(request, h) {
return Boom.notImplemented()
get: async (request, h) => {
try {
const response = await oracle.getOracle(request)
return h.response(response).code(200)
} catch (e) {
return h.response(e).code(400)
}
},
/**
* summary: Create Oracles
Expand All @@ -50,7 +54,7 @@ module.exports = {
*/
post: async (request, h) => {
try {
await oracle.postOracle(request)
await oracle.createOracle(request)
return h.response().code(201)
} catch (e) {
return h.response(e).code(400)
Expand Down
20 changes: 15 additions & 5 deletions src/handlers/oracles/{ID}.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

'use strict'

const Boom = require('boom')
const oracle = require('../../domain/oracle')

/**
* Operations on /oracles/{ID}
Expand All @@ -37,8 +37,13 @@ module.exports = {
* produces: application/json
* responses: 204, 400, 401, 403, 404, 405, 406, 501, 503
*/
put: function OraclePut(request, h) {
return Boom.notImplemented()
put: async (request, h) => {
try {
await oracle.updateOracle(request)
return h.response().code(204)
} catch (e) {
return h.response(e).code(400)
}
},
/**
* summary: Delete Oracle
Expand All @@ -47,7 +52,12 @@ module.exports = {
* produces: application/json
* responses: 204, 400, 401, 403, 404, 405, 406, 501, 503
*/
delete: function OracleDelete(request, h) {
return Boom.notImplemented()
delete: async (request, h) => {
try {
await oracle.deleteOracle(request)
return h.response().code(204)
} catch (e) {
return h.response(e).code(400)
}
}
}
26 changes: 26 additions & 0 deletions src/lib/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*****
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>
* Rajiv Mothilal <rajiv.mothilal@modusbox.com>
--------------
******/
'use strict'

module.exports = require('@mojaloop/central-services-database').Db
47 changes: 47 additions & 0 deletions src/models/currency/currency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*****
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>
* Rajiv Mothilal <rajiv.mothilal@modusbox.com>
--------------
******/

'use strict'

const Db = require('../../lib/db')

/**
* @function getCurrencyById
*
* @description Retrieve a valid currency entry
*
* @param {object} currencyId The 3 character currency code
*/
const getCurrencyById = async (currencyId) => {
try {
return Db.currency.findOne({currencyId, isActive: true})
} catch (err) {
throw new Error(err.message)
}
}

module.exports = {
getCurrencyById
}
32 changes: 32 additions & 0 deletions src/models/currency/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*****
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>
- Rajiv Mothilal <rajiv.mothilal@modusbox.com>
--------------
******/

'use strict'

const currency = require('./currency')

module.exports = {
getCurrencyById: currency.getCurrencyById
}
9 changes: 8 additions & 1 deletion src/models/endpointType/endpointType.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@

'use strict'

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

/**
* @function getEndpointTypeByType
*
* @description Retrieve a valid currency entry
*
* @param {object} type The type of the endpoint i.e. URL
*/
const getEndpointTypeByType = async (type) => {
try {
return Db.endpointType.findOne({type, isActive: true})
Expand Down
Loading

0 comments on commit 1599ce3

Please sign in to comment.