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

fix(mojaloop/#3020): add checks for when mongodb is disabled #927

Merged
merged 13 commits into from
Nov 24, 2022
42 changes: 22 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"@mojaloop/central-services-database": "10.7.0",
"@mojaloop/central-services-error-handling": "12.0.5",
"@mojaloop/central-services-health": "14.0.1",
"@mojaloop/central-services-logger": "11.1.0",
"@mojaloop/central-services-logger": "11.2.0",
"@mojaloop/central-services-metrics": "12.0.5",
"@mojaloop/central-services-shared": "17.3.1",
"@mojaloop/central-services-stream": "11.0.0",
Expand Down Expand Up @@ -124,7 +124,7 @@
"jsdoc": "4.0.0",
"jsonpath": "1.1.1",
"nodemon": "2.0.20",
"npm-check-updates": "16.4.1",
"npm-check-updates": "16.4.3",
"nyc": "15.1.0",
"pre-commit": "1.2.2",
"proxyquire": "2.1.3",
Expand Down
9 changes: 7 additions & 2 deletions src/handlers/bulk/fulfil/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ const sendIndividualTransfer = async (message, messageId, kafkaTopic, headers, p
*/
const registerBulkFulfilHandler = async () => {
try {
if (Config.MONGODB_DISABLED) {
throw ErrorHandler.Factory.createFSPIOPError(
ErrorHandler.Enums.FSPIOPErrorCodes.INTERNAL_SERVER_ERROR,
'Cannot register BulkFulfilHandler as Mongo Database is disabled in configuration')
}
const bulkFulfilHandler = {
command: bulkFulfil,
topicName: Kafka.transformGeneralTopicName(Config.KAFKA_CONFIG.TOPIC_TEMPLATES.GENERAL_TOPIC_TEMPLATE.TEMPLATE, Enum.Events.Event.Type.BULK, Enum.Events.Event.Action.FULFIL),
Expand All @@ -317,7 +322,7 @@ const registerBulkFulfilHandler = async () => {
return true
} catch (err) {
Logger.isErrorEnabled && Logger.error(err)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -335,7 +340,7 @@ const registerAllHandlers = async () => {
return true
} catch (err) {
Logger.isErrorEnabled && Logger.error(err)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/handlers/bulk/get/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ const Consumer = require('@mojaloop/central-services-stream').Util.Consumer
const Enum = require('@mojaloop/central-services-shared').Enum
const Metrics = require('@mojaloop/central-services-metrics')
const ErrorHandler = require('@mojaloop/central-services-error-handling')
const Config = require('../../../lib/config')
const BulkTransferService = require('../../../domain/bulkTransfer')
const BulkTransferModel = require('../../../models/bulkTransfer/bulkTransfer')
const Validator = require('../shared/validator')
const Config = require('../../../lib/config')
const { ERROR_HANDLING } = require('../../../lib/config')

const location = { module: 'BulkGetHandler', method: '', path: '' }
Expand Down Expand Up @@ -160,6 +160,11 @@ const getBulkTransfer = async (error, messages) => {
*/
const registerGetBulkTransferHandler = async () => {
try {
if (Config.MONGODB_DISABLED) {
throw ErrorHandler.Factory.createFSPIOPError(
ErrorHandler.Enums.FSPIOPErrorCodes.INTERNAL_SERVER_ERROR,
'Cannot register GetBulkTransferHandler as Mongo Database is disabled in configuration')
}
const bulkGetHandler = {
command: getBulkTransfer,
topicName: Kafka.transformGeneralTopicName(Config.KAFKA_CONFIG.TOPIC_TEMPLATES.GENERAL_TOPIC_TEMPLATE.TEMPLATE, Enum.Events.Event.Type.BULK, Enum.Events.Event.Action.GET),
Expand Down
9 changes: 7 additions & 2 deletions src/handlers/bulk/prepare/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ const bulkPrepare = async (error, messages) => {
*/
const registerBulkPrepareHandler = async () => {
try {
if (Config.MONGODB_DISABLED) {
throw ErrorHandler.Factory.createFSPIOPError(
ErrorHandler.Enums.FSPIOPErrorCodes.INTERNAL_SERVER_ERROR,
'Cannot register BulkPrepareHandler as Mongo Database is disabled in configuration')
}
const bulkPrepareHandler = {
command: bulkPrepare,
topicName: Kafka.transformGeneralTopicName(Config.KAFKA_CONFIG.TOPIC_TEMPLATES.GENERAL_TOPIC_TEMPLATE.TEMPLATE, Enum.Events.Event.Type.BULK, Enum.Events.Event.Action.PREPARE),
Expand All @@ -297,7 +302,7 @@ const registerBulkPrepareHandler = async () => {
return true
} catch (err) {
Logger.isErrorEnabled && Logger.error(err)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -315,7 +320,7 @@ const registerAllHandlers = async () => {
return true
} catch (err) {
Logger.isErrorEnabled && Logger.error(err)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/handlers/bulk/processing/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@ const bulkProcessing = async (error, messages) => {
*/
const registerBulkProcessingHandler = async () => {
try {
if (Config.MONGODB_DISABLED) {
throw ErrorHandler.Factory.createFSPIOPError(
ErrorHandler.Enums.FSPIOPErrorCodes.INTERNAL_SERVER_ERROR,
'Cannot register BulkProcessingHandler as Mongo Database is disabled in configuration')
}
const bulkProcessingHandler = {
command: bulkProcessing,
topicName: Kafka.transformGeneralTopicName(Config.KAFKA_CONFIG.TOPIC_TEMPLATES.GENERAL_TOPIC_TEMPLATE.TEMPLATE, Enum.Events.Event.Type.BULK, Enum.Events.Event.Action.PROCESSING),
Expand Down
2 changes: 1 addition & 1 deletion src/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
MAX_FULFIL_TIMEOUT_DURATION_SECONDS: RC.MAX_FULFIL_TIMEOUT_DURATION_SECONDS || 300,
MONGODB_URI: RC.MONGODB.URI,
MONGODB_DEBUG: (RC.MONGODB.DEBUG === true || RC.MONGODB.DEBUG === 'true'),
MONGODB_DISABLED: RC.MONGODB.DISABLED,
MONGODB_DISABLED: (RC.MONGODB.DISABLED === true || RC.MONGODB.DISABLED === 'true'),
AMOUNT: RC.AMOUNT,
EXPIRES_TIMEOUT: RC.EXPIRES_TIMEOUT,
ERROR_HANDLING: RC.ERROR_HANDLING,
Expand Down
2 changes: 1 addition & 1 deletion src/shared/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const connectMongoose = async () => {
Logger.isWarnEnabled && Logger.warn('Enabling debug for Mongoose...')
ObjStoreDb.Mongoose.set('debug', Config.MONGODB_DEBUG) // enable debug
}
return ObjStoreDb.connect(Config.MONGODB_URI)
return await ObjStoreDb.connect(Config.MONGODB_URI)
} catch (err) {
throw ErrorHandler.Factory.reformatFSPIOPError(err)
// TODO: review as code is being changed from returning null to returning a FSPIOPError
Expand Down
12 changes: 12 additions & 0 deletions test/unit/lib/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,17 @@ Test('Config should', configTest => {
test.end()
})

configTest.test('evaluate MONGODB_DISABLED to a boolean if a string', async function (test) {
console.log(Defaults)
const DefaultsStub = { ...Defaults }
DefaultsStub.MONGODB.DISABLED = 'true'
const Config = Proxyquire('../../../src/lib/config', {
'../../config/default.json': DefaultsStub
})

test.ok(Config.MONGODB_DISABLED === true)
test.end()
})

configTest.end()
})