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: remove trx.rollback() #1051

Merged
merged 1 commit into from
Jun 13, 2024
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
485 changes: 218 additions & 267 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@
}
},
"dependencies": {
"@hapi/catbox-memory": "6.0.1",
"@hapi/catbox-memory": "6.0.2",
"@hapi/good": "9.0.1",
"@hapi/hapi": "21.3.9",
"@hapi/hapi": "21.3.10",
"@hapi/inert": "7.1.0",
"@hapi/joi": "17.1.1",
"@hapi/vision": "7.0.3",
Expand All @@ -104,7 +104,7 @@
"@mojaloop/central-services-shared": "18.4.0-snapshot.17",
"@mojaloop/central-services-stream": "11.3.0",
"@mojaloop/database-lib": "11.0.5",
"@mojaloop/event-sdk": "14.1.0",
"@mojaloop/event-sdk": "14.1.1",
"@mojaloop/ml-number": "11.2.4",
"@mojaloop/object-store-lib": "12.0.3",
"@now-ims/hapi-now-auth": "2.1.0",
Expand Down Expand Up @@ -141,7 +141,7 @@
"jsonpath": "1.1.1",
"nodemon": "3.1.3",
"npm-check-updates": "16.14.20",
"nyc": "15.1.0",
"nyc": "17.0.0",
"pre-commit": "1.2.2",
"proxyquire": "2.1.3",
"replace": "^1.2.2",
Expand Down
3 changes: 0 additions & 3 deletions src/models/fxTransfer/fxTransfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,8 @@ const savePreparedRequest = async (payload, stateReason, hasPassedValidation) =>
counterPartyParticipantRecord2.name = payload.counterPartyFsp

await knex(TABLE_NAMES.fxTransferStateChange).transacting(trx).insert(fxTransferStateChangeRecord)
await trx.commit()
histTimerSaveTranferTransactionValidationPassedEnd({ success: true, queryName: 'facade_saveFxTransferPrepared_transaction' })
} catch (err) {
await trx.rollback()
histTimerSaveTranferTransactionValidationPassedEnd({ success: false, queryName: 'facade_saveFxTransferPrepared_transaction' })
throw err
}
Expand Down Expand Up @@ -398,7 +396,6 @@ const saveFxFulfilResponse = async (commitRequestId, payload, action, fspiopErro
result.savePayeeTransferResponseExecuted = true
logger.debug('saveFxFulfilResponse::success')
} catch (err) {
await trx.rollback()
histTFxFulfilResponseValidationPassedEnd({ success: false, queryName: 'facade_saveFxFulfilResponse_transaction' })
logger.error('saveFxFulfilResponse::failure')
throw err
Expand Down
6 changes: 0 additions & 6 deletions src/models/ledgerAccountType/ledgerAccountType.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,8 @@ exports.create = async (name, description, isActive, isSettleable, trx = null) =
.from('ledgerAccountType')
.where('name', name)
.transacting(trx)
if (doCommit) {
await trx.commit
}
return createdId[0].ledgerAccountTypeId
} catch (err) {
if (doCommit) {
await trx.rollback()
}
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/models/position/facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,9 @@ const prepareChangeParticipantPositionTransaction = async (transferList) => {
}
batchParticipantPositionChange.length && await knex.batchInsert('participantPositionChange', batchParticipantPositionChange).transacting(trx)
histTimerPersistTransferStateChangeEnd({ success: true, queryName: 'facade_prepareChangeParticipantPositionTransaction_transaction_PersistTransferState' })
await trx.commit()
histTimerChangeParticipantPositionTransEnd({ success: true, queryName: 'facade_prepareChangeParticipantPositionTransaction_transaction' })
} catch (err) {
Logger.isErrorEnabled && Logger.error(err)
await trx.rollback()
histTimerChangeParticipantPositionTransEnd({ success: false, queryName: 'facade_prepareChangeParticipantPositionTransaction_transaction' })
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
Expand Down Expand Up @@ -298,10 +296,8 @@ const changeParticipantPositionTransaction = async (participantCurrencyId, isRev
createdDate: transactionTimestamp
}
await knex('participantPositionChange').transacting(trx).insert(participantPositionChange)
await trx.commit()
histTimerChangeParticipantPositionTransactionEnd({ success: true, queryName: 'facade_changeParticipantPositionTransaction' })
} catch (err) {
await trx.rollback()
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}).catch((err) => {
Expand Down
3 changes: 0 additions & 3 deletions src/models/transfer/facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@ const savePayeeTransferResponse = async (transferId, payload, action, fspiopErro
result.savePayeeTransferResponseExecuted = true
Logger.isDebugEnabled && Logger.debug('savePayeeTransferResponse::success')
} catch (err) {
await trx.rollback()
histTPayeeResponseValidationPassedEnd({ success: false, queryName: 'facade_saveTransferPrepared_transaction' })
Logger.isErrorEnabled && Logger.error('savePayeeTransferResponse::failure')
throw err
Expand Down Expand Up @@ -486,10 +485,8 @@ const saveTransferPrepared = async (payload, stateReason = null, hasPassedValida
}
await knex('ilpPacket').transacting(trx).insert(ilpPacketRecord)
await knex('transferStateChange').transacting(trx).insert(transferStateChangeRecord)
await trx.commit()
histTimerSaveTranferTransactionValidationPassedEnd({ success: true, queryName: 'facade_saveTransferPrepared_transaction' })
} catch (err) {
await trx.rollback()
histTimerSaveTranferTransactionValidationPassedEnd({ success: false, queryName: 'facade_saveTransferPrepared_transaction' })
throw err
}
Expand Down
53 changes: 4 additions & 49 deletions test/unit/models/ledgerAccountType/ledgerAccountType.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,14 @@ Test('ledgerAccountType model', async (ledgerAccountTypeTest) => {
sandbox.stub(Db, 'getKnex')
const knexStub = sandbox.stub()
const trxStub = {
get commit () {
commit () {

},
get rollback () {
rollback () {

}
}
const trxSpyCommit = sandbox.spy(trxStub, 'commit', ['get'])
sandbox.spy(trxStub, 'commit')

knexStub.transaction = sandbox.stub().callsArgWith(0, trxStub)
Db.getKnex.returns(knexStub)
Expand All @@ -219,58 +219,13 @@ Test('ledgerAccountType model', async (ledgerAccountTypeTest) => {
knexStub.select = selectStub

await Model.create(ledgerAccountType.name, ledgerAccountType.description, ledgerAccountType.isActive, ledgerAccountType.isSettleable)
test.equal(trxSpyCommit.get.calledOnce, true, 'commit the transaction if no transaction is passed')
test.equal(knexStub.transaction.calledOnce, true, 'call knex.transaction() no transaction is passed')
test.end()
} catch (err) {
test.fail(`should not have thrown an error ${err}`)
test.end()
}
})
await ledgerAccountTypeTest.test('create should', async (test) => {
let trxStub
let trxSpyRollBack
const ledgerAccountType = {
name: 'POSITION',
description: 'A single account for each currency with which the hub operates. The account is "held" by the Participant representing the hub in the switch',
isActive: 1,
isSettleable: true
}
try {
sandbox.stub(Db, 'getKnex')
const knexStub = sandbox.stub()
trxStub = {
get commit () {

},
get rollback () {

}
}
trxSpyRollBack = sandbox.spy(trxStub, 'rollback', ['get'])

knexStub.transaction = sandbox.stub().callsArgWith(0, trxStub)
Db.getKnex.returns(knexStub)
const transactingStub = sandbox.stub()
const insertStub = sandbox.stub()
transactingStub.resolves()
knexStub.insert = insertStub.returns({ transacting: transactingStub })
const selectStub = sandbox.stub()
const fromStub = sandbox.stub()
const whereStub = sandbox.stub()
transactingStub.rejects(new Error())
whereStub.returns({ transacting: transactingStub })
fromStub.returns({ whereStub })
knexStub.select = selectStub.returns({ from: fromStub })

await Model.create(ledgerAccountType.name, ledgerAccountType.description, ledgerAccountType.isActive, ledgerAccountType.isSettleable)
test.fail('have thrown an error')
test.end()
} catch (err) {
test.pass('throw an error')
test.equal(trxSpyRollBack.get.calledOnce, true, 'rollback the transaction if no transaction is passed')
test.end()
}
})

await ledgerAccountTypeTest.test('create should', async (test) => {
let trxStub
Expand Down