misc(invoice): Bypass aggregation queries when no event in period #2967
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
The current approach for generating a subscription invoice with charge fees is to perform one aggregation request per charge (and possibly per charge filter when defined) no matter the number of events received during the billing period.
For plans counting a lot of charges this could be very slow when many invoices are generated in parallel (on a 1st day of a month for example).
After some research, it appears that in many cases, most of the aggregation result for both charges or filters results in a zero fee as no event were received.
Description
The goal of this PR is to add a pre-check on events before performing the charge billing. It will detect the distinct event code received for a specific subscription during the billing period, and use this list to perform the aggregation only on charge having at least one event.
NOTE: this aggregation byepassing will not apply in in-advance or recurring billing as in these cases the aggregation result relies on more than the event received during the billing period.