lucasdotvin / laravel-soulbscription

A straightforward interface to handle subscriptions and feature consumption.
https://soulbscription.lucas.vin
MIT License
601 stars 84 forks source link

Bugfix: The balance of the feature was miscalculated and was considering every subscriber #41

Closed khatriafaz closed 2 years ago

khatriafaz commented 2 years ago

The issue is that when a feature ticket is assigned to the subscriber, and if there are more than 1 subscriber for the same ticket, the package loads the balance as summed up for all the feature tickets.

The whereHas has the scope for who is the subscriber, but the with misses the scope, and because of that, when the feature tickets for a particular feature is loaded, it loaded summed up charges for the tickets assigned to anyone.

Example:

I assign 5 charges as a feature ticket when a user confirms their account:

$user->giveTicketFor('document', null, 5);

So this happens for each user who registered and confirmed their account.

Now when we see how much balance for each user has using:

$user->balance('document')

If the system has only 1 user, it will return 5.

If there are more than one users, say n users, it will return 5 * n.

lucasdotvin commented 2 years ago

Hey, @khatriafaz! Thanks for reporting the bug and bringing a solution! I added a test case to cover this scenario and I'm gonna publish a release right now! 🚀

codecov[bot] commented 2 years ago

Codecov Report

Merging #41 (3f87699) into develop (f01a850) will increase coverage by 0.01%. The diff coverage is 100.00%.

@@              Coverage Diff              @@
##             develop      #41      +/-   ##
=============================================
+ Coverage      98.23%   98.24%   +0.01%     
  Complexity       150      150              
=============================================
  Files             26       26              
  Lines            509      512       +3     
=============================================
+ Hits             500      503       +3     
  Misses             9        9              
Impacted Files Coverage Δ
src/Models/Concerns/HasSubscriptions.php 95.76% <100.00%> (+0.06%) :arrow_up:

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

khatriafaz commented 2 years ago

@lucasdotvin thanks for considering my solution.