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

Refactor ProcessBillingBatchService - first pass #214

Merged
merged 18 commits into from
May 11, 2023

Conversation

StuAA78
Copy link
Contributor

@StuAA78 StuAA78 commented May 9, 2023

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:

{
  '610ca07a-3bd3-4ea2-8cb1-82f6977cdc71': { invoiceAccountId: '610ca07a-3bd3-4ea2-8cb1-82f6977cdc71', ... },
  '03cec07f-9ad3-4c9a-bad8-1958cec0d61f': { invoiceAccountId: '03cec07f-9ad3-4c9a-bad8-1958cec0d61f', ... }
}

This means that we can easily retrieve a given invoice account once we're in the main loop:

const invoiceAccountId = invoiceAccounts['610ca07a-3bd3-4ea2-8cb1-82f6977cdc71']

We've spotted ways we can refactor `ProcessBillingBatchService` to make it more performant. This is a first pass at this.
@StuAA78 StuAA78 added the housekeeping Refactoring, tidying up or other work which supports the project label May 9, 2023
@StuAA78 StuAA78 self-assigned this May 9, 2023
StuAA78 added 12 commits May 9, 2023 13:31
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
@StuAA78 StuAA78 marked this pull request as ready for review May 10, 2023 15:58
Copy link
Member

@Cruikshanks Cruikshanks left a 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 😁

StuAA78 added 3 commits May 11, 2023 14:43
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.
Cruikshanks
Cruikshanks previously approved these changes May 11, 2023
@StuAA78 StuAA78 requested a review from Jozzey May 11, 2023 15:32
Copy link
Contributor

@Jozzey Jozzey left a comment

Choose a reason for hiding this comment

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

@StuAA78 StuAA78 merged commit 1f1b0aa into main May 11, 2023
@StuAA78 StuAA78 deleted the refactor-process-billing-batch-service-first-pass branch May 11, 2023 15:39
StuAA78 added a commit that referenced this pull request May 12, 2023
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
housekeeping Refactoring, tidying up or other work which supports the project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants