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: avoid 2 extra db requests #1054

Draft
wants to merge 1 commit into
base: feat/fx-impl
Choose a base branch
from
Draft
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
37 changes: 0 additions & 37 deletions src/domain/fx/cyril.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,42 +117,6 @@ const getParticipantAndCurrencyForFxTransferMessage = async (payload) => {
}
}

const processFxFulfilMessage = async (commitRequestId, payload) => {
const histTimerGetParticipantAndCurrencyForFxTransferMessage = Metrics.getHistogram(
'fx_domain_cyril_processFxFulfilMessage',
'fx_domain_cyril_processFxFulfilMessage - Metrics for fx cyril',
['success']
).startTimer()
// Does this commitRequestId appear on the watch list?
const watchListRecord = await watchList.getItemInWatchListByCommitRequestId(commitRequestId)
if (!watchListRecord) {
throw new Error(`Commit request ID ${commitRequestId} not found in watch list`)
}
const fxTransferRecord = await fxTransfer.getAllDetailsByCommitRequestId(commitRequestId)
const {
initiatingFspParticipantCurrencyId,
initiatingFspParticipantId,
initiatingFspName,
counterPartyFspSourceParticipantCurrencyId,
counterPartyFspTargetParticipantCurrencyId,
counterPartyFspParticipantId,
counterPartyFspName
} = fxTransferRecord

// TODO: May need to update the watchList record to indicate that the fxTransfer has been fulfilled

histTimerGetParticipantAndCurrencyForFxTransferMessage({ success: true })
return {
initiatingFspParticipantCurrencyId,
initiatingFspParticipantId,
initiatingFspName,
counterPartyFspSourceParticipantCurrencyId,
counterPartyFspTargetParticipantCurrencyId,
counterPartyFspParticipantId,
counterPartyFspName
}
}

const processFulfilMessage = async (transferId, payload, transfer) => {
const histTimerGetParticipantAndCurrencyForFxTransferMessage = Metrics.getHistogram(
'fx_domain_cyril_processFulfilMessage',
Expand Down Expand Up @@ -260,6 +224,5 @@ const processFulfilMessage = async (transferId, payload, transfer) => {
module.exports = {
getParticipantAndCurrencyForTransferMessage,
getParticipantAndCurrencyForFxTransferMessage,
processFxFulfilMessage,
processFulfilMessage
}
8 changes: 5 additions & 3 deletions src/handlers/transfers/FxFulfilService.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,17 +307,19 @@ class FxFulfilService {

async processFxFulfil({ transfer, payload, action }) {
await this.FxTransferModel.fxTransfer.saveFxFulfilResponse(transfer.commitRequestId, payload, action)
const cyrilOutput = await this.cyril.processFxFulfilMessage(transfer.commitRequestId, payload)
if (!transfer.fxWatchListId) {
throw new Error(`Commit request ID ${transfer.commitRequestId} not found in watch list`)
}
Comment on lines -310 to +312
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we still have a function in cyril 'processFxFulfilMessage' for this check? I mean. you can remove the call 'getAllDetailsByCommitRequestId' in that function, but I would recommend the queries (getItemInWatchListByCommitRequestId) on watchlist in the cyril functions instead of having a join in other place. Because the watchlist logic may change and I wouldn't suggest merging watchList in the places other than cyril. I know it's additional query but I hope it will not impact performance that much compared to left join.

const eventDetail = {
functionality: Type.POSITION,
action
}
this.log.info('handle fxFulfilResponse', { eventDetail, cyrilOutput })
this.log.info('handle fxFulfilResponse', { eventDetail })

await this.kafkaProceed({
consumerCommit,
eventDetail,
messageKey: cyrilOutput.counterPartyFspSourceParticipantCurrencyId.toString(),
messageKey: transfer.counterPartyFspSourceParticipantCurrencyId.toString(),
topicNameOverride: this.Config.KAFKA_CONFIG.EVENT_TYPE_ACTION_TOPIC_MAP?.POSITION?.COMMIT
})
return true
Expand Down
2 changes: 2 additions & 0 deletions src/models/fxTransfer/fxTransfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ const getAllDetailsByCommitRequestId = async (commitRequestId) => {
.leftJoin('fxTransferStateChange AS tsc', 'tsc.commitRequestId', 'fxTransfer.commitRequestId')
.leftJoin('transferState AS ts', 'ts.transferStateId', 'tsc.transferStateId')
.leftJoin('fxTransferFulfilment AS tf', 'tf.commitRequestId', 'fxTransfer.commitRequestId')
.leftJoin('fxWatchList AS wl', 'wl.commitRequestId', 'fxTransfer.commitRequestId')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see my above comment.

// .leftJoin('transferError as te', 'te.commitRequestId', 'transfer.commitRequestId') // currently transferError.transferId is PK ensuring one error per transferId
.select(
'fxTransfer.*',
'wl.fxWatchListId',
'pc1.participantCurrencyId AS initiatingFspParticipantCurrencyId',
'tp1.amount AS initiatingFspAmount',
'da.participantId AS initiatingFspParticipantId',
Expand Down
6 changes: 0 additions & 6 deletions src/models/fxTransfer/watchList.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ const Db = require('../../lib/db')
const { TABLE_NAMES } = require('../../shared/constants')
const { logger } = require('../../shared/logger')

const getItemInWatchListByCommitRequestId = async (commitRequestId) => {
logger.debug(`get item in watch list (commitRequestId=${commitRequestId})`)
return Db.from(TABLE_NAMES.fxWatchList).findOne({ commitRequestId })
}

const getItemsInWatchListByDeterminingTransferId = async (determiningTransferId) => {
logger.debug(`get item in watch list (determiningTransferId=${determiningTransferId})`)
return Db.from(TABLE_NAMES.fxWatchList).find({ determiningTransferId })
Expand All @@ -43,7 +38,6 @@ const addToWatchList = async (record) => {
}

module.exports = {
getItemInWatchListByCommitRequestId,
getItemsInWatchListByDeterminingTransferId,
addToWatchList
}
1 change: 1 addition & 0 deletions test/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ const fxtGetAllDetailsByCommitRequestIdDto = ({
} = fxTransferDto()) => ({
commitRequestId,
determiningTransferId,
fxWatchListId: 100,
sourceAmount: sourceAmount.amount,
sourceCurrency: sourceAmount.currency,
targetAmount: targetAmount.amount,
Expand Down
47 changes: 0 additions & 47 deletions test/unit/domain/fx/cyril.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,53 +178,6 @@ Test('Cyril', cyrilTest => {
getParticipantAndCurrencyForFxTransferMessageTest.end()
})

cyrilTest.test('processFxFulfilMessage should', processFxFulfilMessageTest => {
processFxFulfilMessageTest.test('throws error when commitRequestId not in watchlist', async (test) => {
try {
watchList.getItemInWatchListByCommitRequestId.returns(Promise.resolve(null))
await Cyril.processFxFulfilMessage(fxPayload.commitRequestId)
test.ok(watchList.getItemInWatchListByCommitRequestId.calledWith(fxPayload.commitRequestId))
test.fail('Error not thrown')
test.end()
} catch (e) {
test.pass('Error Thrown')
test.end()
}
})

processFxFulfilMessageTest.test('should return fxTransferRecord when commitRequestId is in watchlist', async (test) => {
try {
const fxTransferRecordDetails = {
initiatingFspParticipantCurrencyId: 1,
initiatingFspParticipantId: 1,
initiatingFspName: 'fx_dfsp1',
counterPartyFspSourceParticipantCurrencyId: 1,
counterPartyFspTargetParticipantCurrencyId: 2,
counterPartyFspParticipantId: 2,
counterPartyFspName: 'fx_dfsp2'
}
watchList.getItemInWatchListByCommitRequestId.returns(Promise.resolve({
commitRequestId: fxPayload.commitRequestId,
determiningTransferId: fxPayload.determiningTransferId,
fxTransferTypeId: Enum.Fx.FxTransferType.PAYER_CONVERSION,
createdDate: new Date()
}))
fxTransfer.getAllDetailsByCommitRequestId.returns(Promise.resolve(fxTransferRecordDetails))
const result = await Cyril.processFxFulfilMessage(fxPayload.commitRequestId)
test.ok(watchList.getItemInWatchListByCommitRequestId.calledWith(fxPayload.commitRequestId))
test.ok(fxTransfer.getAllDetailsByCommitRequestId.calledWith(fxPayload.commitRequestId))
test.deepEqual(result, fxTransferRecordDetails)
test.pass('Error not thrown')
test.end()
} catch (e) {
test.fail('Error Thrown')
test.end()
}
})

processFxFulfilMessageTest.end()
})

cyrilTest.test('processFulfilMessage should', processFulfilMessageTest => {
processFulfilMessageTest.test('return false if transferId is not in watchlist', async (test) => {
try {
Expand Down
4 changes: 1 addition & 3 deletions test/unit/handlers/transfers/fxFuflilHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ Test('FX Transfer Fulfil handler -->', fxFulfilTest => {
fxFulfilTest.test('should process fxFulfil callback - just skip message if no commitRequestId in watchList', async (t) => {
// todo: clarify this behaviuor
const fxTransferDetails = fixtures.fxtGetAllDetailsByCommitRequestIdDto()
sandbox.stub(FxFulfilService.prototype, 'getFxTransferDetails').resolves(fxTransferDetails)
sandbox.stub(FxFulfilService.prototype, 'getFxTransferDetails').resolves({ ...fxTransferDetails, fxWatchListId: null })
sandbox.stub(FxFulfilService.prototype, 'validateHeaders').resolves()
sandbox.stub(FxFulfilService.prototype, 'validateEventType').resolves()
sandbox.stub(FxFulfilService.prototype, 'validateFulfilment').resolves()
Expand All @@ -350,7 +350,6 @@ Test('FX Transfer Fulfil handler -->', fxFulfilTest => {
hasDuplicateHash: false
})
Validator.validateFulfilCondition.returns(true)
fxTransferModel.watchList.getItemInWatchListByCommitRequestId.resolves(null)
const metadata = fixtures.fulfilMetadataDto({ action: Action.FX_COMMIT })
const kafkaMessage = fixtures.fxFulfilKafkaMessageDto({ metadata })

Expand All @@ -375,7 +374,6 @@ Test('FX Transfer Fulfil handler -->', fxFulfilTest => {
})
Validator.validateFulfilCondition.returns(true)
fxTransferModel.fxTransfer.getAllDetailsByCommitRequestId.resolves(fxTransferDetails)
fxTransferModel.watchList.getItemInWatchListByCommitRequestId.resolves(fixtures.watchListItemDto())

const action = Action.FX_COMMIT
const metadata = fixtures.fulfilMetadataDto({ action })
Expand Down