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

Feature Usage seems to be wrong #30

Open
jimiero opened this issue Dec 6, 2024 · 8 comments
Open

Feature Usage seems to be wrong #30

jimiero opened this issue Dec 6, 2024 · 8 comments

Comments

@jimiero
Copy link

jimiero commented Dec 6, 2024

Hello @mckenziearts ,

I think this method here may be wrong

public function canUseFeature(string $featureSlug): bool
    {

        $featureValue = $this->getFeatureValue($featureSlug);

        $usage = $this->usage()->byFeatureSlug($featureSlug)->first();

        if ($featureValue === 'true') {
            return true;
        }

        // If the feature value is zero, let's return false since
        // there's no uses available. (useful to disable countable features)
        if (! $usage || $usage->expired() || $featureValue === null || $featureValue === '0' || $featureValue === 'false') {
            return false;
        }

        // Check for available uses
        return $this->getFeatureRemainings($featureSlug) > 0;
    }

why will it fail if there's not usages done already ?

lets say user purchase a plan with feature "contacts" with limit 30

Then if you do

$user->subscription->canUseFeature('contacts')

it will return false, because he never made use of that feature...

@JELmzabi
Copy link

+1

@jimiero Any solution? I have the same issue and am looking for a workaround or fix. If you've found a solution, could you please share the steps or code changes you used? Thanks in advance! 🙏 .

@jimiero
Copy link
Author

jimiero commented Dec 18, 2024

@JELmzabi unfortunately, I haven't because I'm also expecting to see if this package is maintained if not I'd rather using different one @mckenziearts do you have any input

@JELmzabi
Copy link

I think we should initialize usage. I have created a function that does that, but I’m not sure if it’s the best solution.

public function initializeSubscriptionUsage(Subscription $subscription) {
        $features = $subscription->plan->features;

        $usages = [];

        foreach ($features as $feature) {
            $featureValue = $feature->value;

            if(is_numeric($featureValue) && $featureValue != '0') {
                $usages[]= [
                    'subscription_id' => $subscription->getKey(),
                    'feature_id' => $feature->getKey(),
                    'used' => 0,
                ];   
            }
        }

        return $subscription->usage()->createMany($usages);
    }

@jimiero
Copy link
Author

jimiero commented Dec 23, 2024

@JELmzabi where did you put that method?

@JELmzabi
Copy link

@jimiero in a service

@jimiero
Copy link
Author

jimiero commented Dec 23, 2024

@JELmzabi a bit more details? when exactly do you initialize it ?

@JELmzabi
Copy link

JELmzabi commented Dec 23, 2024

@jimiero When a user subscribes to a plan, I initialize their usage

@jimiero
Copy link
Author

jimiero commented Dec 23, 2024

@JELmzabi do you have discord or something where we can discuss?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants