-
Notifications
You must be signed in to change notification settings - Fork 0
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
Refactor ProcessBillingBatchService
- first pass
#214
Refactor ProcessBillingBatchService
- first pass
#214
Conversation
We've spotted ways we can refactor `ProcessBillingBatchService` to make it more performant. This is a first pass at this.
We start by moving the billing invoice generation outside of the main loop and updating `_generateInvoiceData` to simply look up the billing invoice instead of generating it.
We refactor our fetching of the invoice accounts into a separate `FetchInvoiceAccountsService`, which seperates out our logic for deduping invoice account ids and also allows us to stub it to test that the fetch error trapping works
Now that `GenerateBillingInvoiceService` no longer hits the db we no longer need to test that we error trap a rejected promise, so we remove the now-unneeded test
We previously put the invoice accounts in an array and used `.find()` to pull the required invoice account from it. We change this so that we now store the invoice accounts in a keyed object which simplifies retrieval; we can simply use `invoiceAccounts[invoiceAccountId]` to get the invoice account we want
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty certain you can lose a comment, hence Request changes.
I'm not so certain about the select()
thing though so happy for that to be ignored if I'm wrong 😁
app/services/supplementary-billing/fetch-invoice-accounts.service.js
Outdated
Show resolved
Hide resolved
app/services/supplementary-billing/process-billing-batch.service.js
Outdated
Show resolved
Hide resolved
Since we only really need to get the invoice account numbers from the db, we rework `FetchInvoiceAccountsService` to only return the numbers along with the ids for later indexing. To avoid confusion we amend the service to be `FetchInvoiceAccountNumbers` which makes it very clear that what it returns isn't an array of `InvoiceAccountModel` instances but simply an array of objects which each contain the account number and id.
test/services/supplementary-billing/fetch-invoice-account-numbers.service.test.js
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://eaflood.atlassian.net/browse/WATER-3996 [After refactoring the `ProcessBillingBatchService` to fetch and store all invoice account numbers prior to the main loop](#214), we found an issue where this could result in multiple billing invoices being generated for a single invoice account due to us generating a billing invoice within the main loop, one for each charge version regardless of whether or not one was previously generated for the invoice account. We fix this by generating our billing invoices (one per invoice account) prior to entering the main loop. We then simply retrieve the pre-generated billing invoice within the loop.
https://eaflood.atlassian.net/browse/WATER-3996
While reviewing
ProcessBillingBatchService
we realised there is scope to make it more performant. This is a first pass which tackles the issue of us fetching invoice accounts one at a time within the main loop. We refactor so that we now fetch all the invoice accounts we will need in a single query prior to entering the main loop and hold them in a keyed object:This means that we can easily retrieve a given invoice account once we're in the main loop: