-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move SendTransactions to root with refactoring (#745)
https://eaflood.atlassian.net/browse/WATER-4365 > For context this came out of us working on re-implementing the SROC annual bill run using what we've learnt and components from our supplementary billing engine. As part of looking at re-implementing the SROC annual billing engine in this project our spike (WATER-4348 ) confirmed we'd need to reuse some of the services currently sitting in `app/services/bill-runs/supplementary`. We moved most of these in [Move shared billing services to bill-runs root](#720) but left `app/services/bill-runs/supplementary/send-transactions.service.js` out even though we also need to reuse it. When working on the spike we found there is some tidying up we can do in the service. We also spotted we were passing in `BillLicence` purely to set the `billLicenceId`. We now feel this should be done elsewhere. So, we're doing those changes here as they are a little more involved than simply moving the service.
- Loading branch information
1 parent
2e8c94d
commit ab71295
Showing
7 changed files
with
182 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict' | ||
|
||
/** | ||
* Sends transactions to the Charging Module | ||
* @module SendTransactionsService | ||
*/ | ||
|
||
const BillRunError = require('../../errors/bill-run.error.js') | ||
const BillRunModel = require('../../models/bill-run.model.js') | ||
const ChargingModuleCreateTransactionService = require('../charging-module/create-transaction.service.js') | ||
const ChargingModuleCreateTransactionPresenter = require('../../presenters/charging-module/create-transaction.presenter.js') | ||
|
||
/** | ||
* Sends the provided transactions to the Charging Module and returns an array of the sent transactions | ||
* | ||
* Each sent transaction is updated with a status of `charge_created` and the external id returned by the | ||
* Charging Module. | ||
* | ||
* @param {Object[]} transactions - The transactions to be sent to the Charging Module | ||
* @param {string} billRunExternalId - The Charging Module bill run id that the transactions are to be created on | ||
* @param {string} accountNumber - The billing account number for the transactions | ||
* @param {module:LicenceModel} licence - The licence that each transaction is linked to | ||
* | ||
* @returns {Promise<Object[]>} Array of transactions which have been sent to the Charging Module and updated with its | ||
* response | ||
*/ | ||
async function go (transactions, billRunExternalId, accountNumber, licence) { | ||
// NOTE: we purposefully loop through all the transactions to send without awaiting them. This is for performance | ||
// purposes. If for example we have 3 transactions to send we'll send the requests 1 straight after the other. We | ||
// then wait for all 3 to complete. The overall process time will only be that of the one that takes the longest. If | ||
// we await instead the overall time will be the sum of the time to complete each one. | ||
const sendRequests = transactions.map((transaction) => { | ||
return _sendTransactionToChargingModule(transaction, billRunExternalId, accountNumber, licence) | ||
}) | ||
|
||
// We use Promise.all() to ensure we wait for all the send requests to resolve. The service that awaits the call to | ||
// SendTransactionsService.go() will still get the updated transactions as Promise.all() returns what each promise | ||
// resolves to as an array. | ||
return Promise.all(sendRequests) | ||
} | ||
|
||
async function _sendTransactionToChargingModule (transaction, billRunExternalId, accountNumber, licence) { | ||
try { | ||
const chargingModuleRequest = ChargingModuleCreateTransactionPresenter.go(transaction, accountNumber, licence) | ||
|
||
const chargingModuleResponse = await ChargingModuleCreateTransactionService.go( | ||
billRunExternalId, | ||
chargingModuleRequest | ||
) | ||
|
||
transaction.status = 'charge_created' | ||
transaction.externalId = chargingModuleResponse.response.body.transaction.id | ||
|
||
return transaction | ||
} catch (error) { | ||
throw new BillRunError(error, BillRunModel.errorCodes.failedToCreateCharge) | ||
} | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 0 additions & 68 deletions
68
app/services/bill-runs/supplementary/send-transactions.service.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.