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: fix for database insert failures #410

Merged
merged 46 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
7428740
ci: disbale int test for snapshots until fixed
oderayi Jun 27, 2024
cf5c0ac
chore(snapshot): 17.0.0-snapshot.0
oderayi Jun 27, 2024
2f7795c
chore(snapshot): 17.0.0-snapshot.1
oderayi Jun 27, 2024
8a1dee5
chore(snapshot): 17.0.0-snapshot.2
oderayi Jun 27, 2024
905abbc
ci: update config
oderayi Jun 27, 2024
81edd84
chore(snapshot): 17.0.0-snapshot.3
oderayi Jun 27, 2024
c633370
ci: update config
oderayi Jun 27, 2024
ec9b3c4
chore(snapshot): 17.0.0-snapshot.4
oderayi Jun 27, 2024
679b0e4
fix: ensure central ledger knex is initialized on startup
oderayi Sep 12, 2024
870b4e7
chore: fix audit
oderayi Sep 12, 2024
3485068
chore(snapshot): 17.0.0-snapshot.5
oderayi Sep 12, 2024
68a1235
test: update unit tests
oderayi Sep 12, 2024
2790a1d
chore(snapshot): 17.0.0-snapshot.6
oderayi Sep 12, 2024
362e675
chore: update deps
oderayi Sep 12, 2024
9bb63a2
chore(snapshot): 17.0.0-snapshot.7
oderayi Sep 12, 2024
417dee3
fix: fix transferParticipant query
oderayi Sep 12, 2024
17c19cf
chore(snapshot): 17.0.0-snapshot.8
oderayi Sep 12, 2024
13d5b92
fix: fix insert for participantPositionChange, add participantCurrenc…
oderayi Sep 13, 2024
c4f7a94
chore(snapshot): 17.0.0-snapshot.9
oderayi Sep 13, 2024
c4e54d6
chore(snapshot): 17.0.0-snapshot.10
oderayi Sep 13, 2024
a9a004a
chore(snapshot): 17.0.0-snapshot.11
oderayi Sep 13, 2024
4b6dbb9
fix: more fixes for participantPositionChange insert of participantCu…
oderayi Sep 13, 2024
a859b4e
chore(snapshot): 17.0.0-snapshot.12
oderayi Sep 13, 2024
625259e
fix: fix settlementContentAggregagion query in line with fx db changes
oderayi Sep 18, 2024
b751337
chore: update deps
oderayi Sep 18, 2024
f9dcb74
chore(snapshot): 17.0.0-snapshot.13
oderayi Sep 18, 2024
61f0598
test: temp disable coverage for faced
oderayi Sep 18, 2024
3761324
chore(snapshot): 17.0.0-snapshot.14
oderayi Sep 18, 2024
b4dce52
doc: update comment
oderayi Sep 19, 2024
12d7837
chore: remove comment
oderayi Sep 19, 2024
7710b17
fix: use ppc.change
oderayi Sep 19, 2024
e560172
chore(snapshot): 17.0.0-snapshot.15
oderayi Sep 19, 2024
3da5de5
ci: upgrade cl version
oderayi Sep 19, 2024
96d92e8
fix: fixes for participantPositionChange
oderayi Sep 19, 2024
3ba3c0d
chore(snapshot): 17.0.0-snapshot.16
oderayi Sep 19, 2024
44345c5
fix: add fx query and fix(?) db commit/rollback (#411)
kleyow Sep 20, 2024
52b2008
fix: refactor queries
vijayg10 Sep 20, 2024
85b9853
chore(snapshot): 17.0.0-snapshot.17
kleyow Sep 21, 2024
87d9be8
chore: lint dep
kleyow Sep 21, 2024
f306812
chore(snapshot): 17.0.0-snapshot.18
kleyow Sep 21, 2024
c98c6a8
chore: addressed pr comments
vijayg10 Sep 23, 2024
dc0e99b
chore: udpate deps
vijayg10 Sep 23, 2024
3e77dd1
chore: re-enable test coverage check for ignored file, some cleanups
oderayi Sep 23, 2024
16fc26a
Merge branch 'fix/csi-640-database-failures' of github.com:mojaloop/c…
oderayi Sep 23, 2024
727faf2
test: update tests
oderayi Sep 24, 2024
8924e89
ci: upgrade vesions in docker-compose
oderayi Sep 24, 2024
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
2 changes: 0 additions & 2 deletions src/handlers/grossSettlement/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ async function processTransferSettlement (error, messages) {
await knex.transaction(async trx => {
try {
await transferSettlementService.processMsgFulfil(transferEventId, transferEventStateStatus, trx)
await trx.commit
} catch (err) {
await trx.rollback
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
})
Expand Down
2 changes: 0 additions & 2 deletions src/handlers/rules/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ async function processRules (error, messages) {
await knex.transaction(async trx => {
try {
await RulesService.insertLedgerEntries(ledgerEntries, transferEventId, trx)
await trx.commit
} catch (err) {
await trx.rollback
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
})
Expand Down
37 changes: 0 additions & 37 deletions src/models/settlement/facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,8 @@ const settlementTransfersPrepare = async function (settlementId, transactionTime
createdDate: transactionTimestamp
})
.transacting(trx)

if (doCommit) {
await trx.commit
}
} catch (err) {
Logger.isErrorEnabled && Logger.error(err)
if (doCommit) {
await trx.rollback
}
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}
Expand Down Expand Up @@ -246,7 +239,6 @@ const settlementTransfersPrepare = async function (settlementId, transactionTime
const settlementTransfersReserve = async function (settlementId, transactionTimestamp, requireLiquidityCheck, enums, trx = null) {
const knex = await Db.getKnex()
let isLimitExceeded, transferStateChangeId

// Retrieve list of PS_TRANSFERS_RESERVED, but not RESERVED
const settlementTransferList = await knex('settlementParticipantCurrency AS spc')
.join('settlementParticipantCurrencyStateChange AS spcsc', function () {
Expand Down Expand Up @@ -389,16 +381,9 @@ const settlementTransfersReserve = async function (settlementId, transactionTime
})
.transacting(trx)
}

if (doCommit) {
await trx.commit
}
}
} catch (err) {
Logger.isErrorEnabled && Logger.error(err)
if (doCommit) {
await trx.rollback
}
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}
Expand Down Expand Up @@ -550,16 +535,9 @@ const settlementTransfersAbort = async function (settlementId, transactionTimest
})
.transacting(trx)
}

if (doCommit) {
await trx.commit
}
}
} catch (err) {
Logger.isErrorEnabled && Logger.error(err)
if (doCommit) {
await trx.rollback
}
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}
Expand Down Expand Up @@ -727,16 +705,9 @@ const settlementTransfersCommit = async function (settlementId, transactionTimes
const message = Facade.getNotificationMessage(action, destination, payload)
await Utility.produceGeneralMessage(Utility.ENUMS.NOTIFICATION, Utility.ENUMS.EVENT, message, Utility.ENUMS.STATE.SUCCESS)
}

if (doCommit) {
await trx.commit
}
}
} catch (err) {
Logger.isErrorEnabled && Logger.error(err)
if (doCommit) {
await trx.rollback
}
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}
Expand Down Expand Up @@ -1211,9 +1182,6 @@ const Facade = {
.update({ currentStateChangeId: settlementStateChangeId })
.transacting(trx)
}

await trx.commit

return {
id: settlementId,
state: settlementData.settlementStateId,
Expand All @@ -1224,7 +1192,6 @@ const Facade = {
}
} catch (err) {
Logger.isErrorEnabled && Logger.error(err)
await trx.rollback
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
})
Expand Down Expand Up @@ -1329,15 +1296,13 @@ const Facade = {
.update({ currentStateChangeId: settlementStateChangeId })
.transacting(trx)

await trx.commit
return {
id: settlementId,
state: payload.state,
reason: payload.reason
}
} catch (err) {
Logger.isErrorEnabled && Logger.error(err)
await trx.rollback
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
})
Expand Down Expand Up @@ -1546,11 +1511,9 @@ const Facade = {
await knex('settlement').transacting(trx)
.where('settlementId', settlementId)
.update({ currentStateChangeId: settlementStateChangeId })
await trx.commit
return settlementId
} catch (err) {
Logger.isErrorEnabled && Logger.error(err)
await trx.rollback
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
})
Expand Down
43 changes: 32 additions & 11 deletions src/models/settlementWindow/facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const Facade = {
},

getTransfersCount: async function ({ settlementWindowId }) {
// Get fxTransferFulfilment count aswell?
oderayi marked this conversation as resolved.
Show resolved Hide resolved
return Db.from('transferFulfilment').query(builder => {
return builder
.count('* as cnt')
Expand Down Expand Up @@ -80,6 +81,7 @@ const Facade = {

getByParams: async function ({ query }) {
const { participantId, state, fromDateTime, toDateTime, currency } = query
// Get from fxTransferFulfilment as well?
return Db.from('settlementWindow').query(builder => {
if (!participantId) {
const b = builder
Expand Down Expand Up @@ -160,11 +162,9 @@ const Facade = {
await knex('settlementWindow').transacting(trx)
.where({ settlementWindowId: newSettlementWindowId })
.update({ currentStateChangeId: newSettlementWindowStateChangeId })
await trx.commit
return newSettlementWindowId[0]
} catch (err) {
Logger.isErrorEnabled && Logger.error(err)
await trx.rollback
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
})
Expand Down Expand Up @@ -247,10 +247,6 @@ const Facade = {
.join('participantPositionChange AS ppc', 'ppc.transferStateChangeId', 'tsc.transferStateChangeId')
.join('participantCurrency AS pc', 'pc.participantCurrencyId', 'ppc.participantCurrencyId')
.join('participant AS p', 'p.participantId', 'pc.participantId')
.leftJoin('transferParticipant AS tp', function () {
this.on('tp.participantId', 'p.participantId')
.andOn('tp.transferId', 'tf.transferId')
})
.join('settlementWindowContent AS swc', function () {
this.on('swc.settlementWindowId', 'tf.settlementWindowId')
.on('swc.ledgerAccountTypeId', 'pc.ledgerAccountTypeId')
Expand All @@ -259,17 +255,44 @@ const Facade = {
.join('settlementModel AS m', 'm.settlementModelId', 'swc.settlementModelId')
.where('tf.settlementWindowId', settlementWindowId)
.andWhere('m.settlementGranularityId', Enum.Settlements.SettlementGranularity.NET)
.groupBy('swc.settlementWindowContentId', 'pc.participantCurrencyId', 'tp.transferParticipantRoleTypeId', 'tp.ledgerEntryTypeId')
.groupBy('swc.settlementWindowContentId', 'pc.participantCurrencyId', 'transferParticipantRoleTypeId', 'ledgerEntryTypeId')
.select('swc.settlementWindowContentId', 'pc.participantCurrencyId',
knex.raw('CASE WHEN ?? = NULL THEN ? ELSE ?? END AS transferParticipantRoleTypeId', ['tp.transferParticipantRoleTypeId', 7, 'tp.transferParticipantRoleTypeId']), // @todo 7 = Enum.Accounts.TransferParticipantRoleType.COUNTER_PARTY_FSP requires upgrade of cs-shared
knex.raw('CASE WHEN ?? = NULL THEN ? ELSE ?? END AS ledgerEntryTypeId', ['tp.ledgerEntryTypeId', Enum.Accounts.LedgerEntryType.PRINCIPLE_VALUE, 'tp.ledgerEntryTypeId']),
knex.raw('CASE WHEN ppc.change > 0 THEN ? ELSE ?? END AS transferParticipantRoleTypeId', [ Enum.Accounts.TransferParticipantRoleType.PAYER_DFSP, Enum.Accounts.TransferParticipantRoleType.PAYEE_DFSP]),
knex.raw('? AS ??', [Enum.Accounts.LedgerEntryType.PRINCIPLE_VALUE, 'ledgerEntryTypeId']),
oderayi marked this conversation as resolved.
Show resolved Hide resolved
knex.raw('? AS ??', [Enum.Settlements.SettlementWindowState.CLOSED, 'settlementWindowStateId']),
knex.raw('? AS ??', [transactionTimestamp, 'createdDate']))
.sum('ppc.change AS amount')
})
.transacting(trx)
await builder

const fxBuilder = knex
.from(knex.raw('settlementContentAggregation (settlementWindowContentId, participantCurrencyId, transferParticipantRoleTypeId, ledgerEntryTypeId, currentStateId, createdDate, amount)'))
.insert(function () {
this.from('fxTransferFulfilment AS fxtf')
.join('fxTransferStateChange AS fxtsc', 'fxtsc.commitRequestId', 'fxtf.commitRequestId')
.join('participantPositionChange AS ppc', 'ppc.fxTransferStateChangeId', 'fxtsc.fxTransferStateChangeId')
.join('participantCurrency AS pc', 'pc.participantCurrencyId', 'ppc.participantCurrencyId')
.join('participant AS p', 'p.participantId', 'pc.participantId')
.join('settlementWindowContent AS swc', function () {
this.on('swc.settlementWindowId', 'fxtf.settlementWindowId')
.on('swc.ledgerAccountTypeId', 'pc.ledgerAccountTypeId')
.on('swc.currencyId', 'pc.currencyId')
})
.join('settlementModel AS m', 'm.settlementModelId', 'swc.settlementModelId')
.where('fxtf.settlementWindowId', settlementWindowId)
.andWhere('m.settlementGranularityId', Enum.Settlements.SettlementGranularity.NET)
.groupBy('swc.settlementWindowContentId', 'pc.participantCurrencyId', 'transferParticipantRoleTypeId', 'ledgerEntryTypeId')
.select('swc.settlementWindowContentId', 'pc.participantCurrencyId',
knex.raw('CASE WHEN ppc.change > 0 THEN ? ELSE ?? END AS transferParticipantRoleTypeId', [ Enum.Accounts.TransferParticipantRoleType.PAYER_DFSP, Enum.Accounts.TransferParticipantRoleType.PAYEE_DFSP]),
knex.raw('? AS ??', [Enum.Accounts.LedgerEntryType.PRINCIPLE_VALUE, 'ledgerEntryTypeId']),
knex.raw('? AS ??', [Enum.Settlements.SettlementWindowState.CLOSED, 'settlementWindowStateId']),
knex.raw('? AS ??', [transactionTimestamp, 'createdDate']))
.sum('ppc.change AS amount')
})
.transacting(trx)
await fxBuilder

vijayg10 marked this conversation as resolved.
Show resolved Hide resolved
// Insert settlementWindowContentStateChange
builder = knex
.from(knex.raw('settlementWindowContentStateChange (settlementWindowContentId, settlementWindowStateId, reason, createdDate)'))
Expand Down Expand Up @@ -313,11 +336,9 @@ const Facade = {
.where({ settlementWindowId })
.update({ currentStateChangeId: settlementWindowStateChangeId })

await trx.commit
return true
} catch (err) {
Logger.isErrorEnabled && Logger.error(err)
await trx.rollback
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
})
Expand Down
Loading