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

expiring tickets #74

Open
nick-potts opened this issue Sep 1, 2023 · 0 comments
Open

expiring tickets #74

nick-potts opened this issue Sep 1, 2023 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@nick-potts
Copy link

nick-potts commented Sep 1, 2023

Not really sure how to explain this.

I want a fully prepaid system where there's no subscribing, simply tickets that can expire.

I tried modifying a few things but got stuck. The first problem was:

? $feature->calculateNextRecurrenceEnd($this->subscription->started_at)

Here it assumes you have a subscription.

I tried adding this:

$hasSubscription = $this->subscription()->exists();

        $consumptionCounter = 0;
        $consumptionRequired = $this->getCurrentConsumption($feature->name) + $consumption;
        $chargableTicket = null;
        foreach (
            $this->loadedTicketFeatures
                     ->find($feature)
                     ->tickets
                     ->sortBy('expired_at') as $ticket
        ) {
            $consumptionCounter += $ticket->charges;
            if ($consumptionCounter >= $consumptionRequired) {
                $chargableTicket = $ticket;
                break;
            }
        }
        
        $consumptionExpiration = match ($hasSubscription) {
            true => $feature->consumable ?
                $feature->calculateNextRecurrenceEnd($this->subscription->started_at)
                : null,
            false => $chargableTicket?->expired_at ?? $this->calculateNextRecurrenceEnd($this->subscription->started_at),
        };

also added ->where('expired_at', '>', now()) to:

This works ok, but for some reason when adding more tickets in the future, it doesn't get added correctly. Or at least loadedTicketFeatures is returning an array of tickets that doesn't contain the newest added tickets.

The test case I was using:

$feature = Feature::factory()->consumable()->createOne();

$subscriber = User::factory()->createOne();

config()->set('soulbscription.feature_tickets', true);

$subscriber->giveTicketFor($feature->name, now()->addDays(2), 2);

$subscriber->giveTicketFor($feature->name, now()->addDay(), 2);

$subscriber->consume($feature->name, 3);

$this->assertEquals(1, $subscriber->getRemainingCharges($feature->name));

$this->travel(2)->days();

$this->assertEquals(0, $subscriber->getRemainingCharges($feature->name));

$subscriber->giveTicketFor($feature->name, now()->addWeek(), 2);

$this->assertEquals(2, $subscriber->getRemainingCharges($feature->name));
@lucasdotvin lucasdotvin added enhancement New feature or request good first issue Good for newcomers labels Sep 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants