Skip to content

Commit

Permalink
Flush cache on subscription changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JhumanJ committed Dec 3, 2023
1 parent 508358d commit 57c695e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 9 deletions.
21 changes: 21 additions & 0 deletions app/Models/Billing/Subscription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Models\Billing;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Laravel\Cashier\Subscription as CashierSubscription;

class Subscription extends CashierSubscription
{
use HasFactory;

public static function booted(): void
{
static::saved(function (Subscription $sub) {
$sub->user->flushCache();
});
static::deleted(function (Subscription $sub) {
$sub->user->flushCache();
});
}
}
14 changes: 14 additions & 0 deletions app/Models/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,18 @@ public function getCustomDomainLimitCountAttribute()
3 => null,
][$this->meta['tier']];
}

public static function booted(): void
{
static::saved(function (License $license) {
if ($license->user) {
$license->user->flushCache();
}
});
static::deleted(function (License $license) {
if ($license->user) {
$license->user->flushCache();
}
});
}
}
19 changes: 16 additions & 3 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace App\Models;

use App\Http\Controllers\SubscriptionController;
use App\Models\Forms\Form;
use App\Models\Template;
use App\Notifications\ResetPassword;
use App\Notifications\VerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down Expand Up @@ -61,7 +59,12 @@ class User extends Authenticatable implements JWTSubject

public function ownsForm(Form $form)
{
return $this->workspaces()->find($form->workspace_id) !== null;
return $this->workspaces()->whereUserId($form->workspace_id)->exists();
}

public function ownsWorkspace(Workspace $workspace)
{
return $this->workspaces()->whereUserId($workspace->id)->exists();
}

/**
Expand Down Expand Up @@ -203,6 +206,16 @@ public function getIsRiskyAttribute()
})->first()?->onTrial();
}

public function flushCache()
{
$this->workspaces()->with('forms')->get()->each(function (Workspace $workspace) {
$workspace->flush();
$workspace->forms->each(function (Form $form) {
$form->flush();
});
});
}

public static function boot()
{
parent::boot();
Expand Down
10 changes: 5 additions & 5 deletions app/Policies/FormPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function viewAny(User $user)
*/
public function view(User $user, Form $form)
{
return $user->workspaces()->find($form->workspace_id) !== null;
return $user->ownsForm($form);
}

/**
Expand All @@ -53,7 +53,7 @@ public function create(User $user)
*/
public function update(User $user, Form $form)
{
return $user->workspaces()->find($form->workspace_id) !== null;
return $user->ownsForm($form);
}

/**
Expand All @@ -65,7 +65,7 @@ public function update(User $user, Form $form)
*/
public function delete(User $user, Form $form)
{
return $user->workspaces()->find($form->workspace_id) !== null;
return $user->ownsForm($form);
}

/**
Expand All @@ -77,7 +77,7 @@ public function delete(User $user, Form $form)
*/
public function restore(User $user, Form $form)
{
return $user->workspaces()->find($form->workspace_id) !== null;
return $user->ownsForm($form);
}

/**
Expand All @@ -89,6 +89,6 @@ public function restore(User $user, Form $form)
*/
public function forceDelete(User $user, Form $form)
{
return $user->workspaces()->find($form->workspace_id) !== null;
return $user->ownsForm($form);
}
}
2 changes: 1 addition & 1 deletion app/Policies/WorkspacePolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function viewAny(User $user)
*/
public function view(User $user, Workspace $workspace)
{
return $workspace->users()->find($user->id)!==null;
return $user->ownsWorkspace($workspace);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use App\Models\Billing\Subscription;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
Expand Down Expand Up @@ -31,6 +32,7 @@ public function boot()

JsonResource::withoutWrapping();
Cashier::calculateTaxes();
Cashier::useSubscriptionModel(Subscription::class);

if ($this->app->runningUnitTests()) {
Schema::defaultStringLength(191);
Expand Down

0 comments on commit 57c695e

Please sign in to comment.