Flush cache on subscription changes
This commit is contained in:
parent
508358d020
commit
57c695e976
|
@ -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();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -51,4 +51,18 @@ class License extends Model
|
||||||
3 => null,
|
3 => null,
|
||||||
][$this->meta['tier']];
|
][$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();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,7 @@
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Http\Controllers\SubscriptionController;
|
|
||||||
use App\Models\Forms\Form;
|
use App\Models\Forms\Form;
|
||||||
use App\Models\Template;
|
|
||||||
use App\Notifications\ResetPassword;
|
use App\Notifications\ResetPassword;
|
||||||
use App\Notifications\VerifyEmail;
|
use App\Notifications\VerifyEmail;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
@ -61,7 +59,12 @@ class User extends Authenticatable implements JWTSubject
|
||||||
|
|
||||||
public function ownsForm(Form $form)
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -203,6 +206,16 @@ class User extends Authenticatable implements JWTSubject
|
||||||
})->first()?->onTrial();
|
})->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()
|
public static function boot()
|
||||||
{
|
{
|
||||||
parent::boot();
|
parent::boot();
|
||||||
|
|
|
@ -30,7 +30,7 @@ class FormPolicy
|
||||||
*/
|
*/
|
||||||
public function view(User $user, Form $form)
|
public function view(User $user, Form $form)
|
||||||
{
|
{
|
||||||
return $user->workspaces()->find($form->workspace_id) !== null;
|
return $user->ownsForm($form);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -53,7 +53,7 @@ class FormPolicy
|
||||||
*/
|
*/
|
||||||
public function update(User $user, Form $form)
|
public function update(User $user, Form $form)
|
||||||
{
|
{
|
||||||
return $user->workspaces()->find($form->workspace_id) !== null;
|
return $user->ownsForm($form);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -65,7 +65,7 @@ class FormPolicy
|
||||||
*/
|
*/
|
||||||
public function delete(User $user, Form $form)
|
public function delete(User $user, Form $form)
|
||||||
{
|
{
|
||||||
return $user->workspaces()->find($form->workspace_id) !== null;
|
return $user->ownsForm($form);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -77,7 +77,7 @@ class FormPolicy
|
||||||
*/
|
*/
|
||||||
public function restore(User $user, Form $form)
|
public function restore(User $user, Form $form)
|
||||||
{
|
{
|
||||||
return $user->workspaces()->find($form->workspace_id) !== null;
|
return $user->ownsForm($form);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -89,6 +89,6 @@ class FormPolicy
|
||||||
*/
|
*/
|
||||||
public function forceDelete(User $user, Form $form)
|
public function forceDelete(User $user, Form $form)
|
||||||
{
|
{
|
||||||
return $user->workspaces()->find($form->workspace_id) !== null;
|
return $user->ownsForm($form);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ class WorkspacePolicy
|
||||||
*/
|
*/
|
||||||
public function view(User $user, Workspace $workspace)
|
public function view(User $user, Workspace $workspace)
|
||||||
{
|
{
|
||||||
return $workspace->users()->find($user->id)!==null;
|
return $user->ownsWorkspace($workspace);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
|
use App\Models\Billing\Subscription;
|
||||||
use Illuminate\Http\Resources\Json\JsonResource;
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
@ -31,6 +32,7 @@ class AppServiceProvider extends ServiceProvider
|
||||||
|
|
||||||
JsonResource::withoutWrapping();
|
JsonResource::withoutWrapping();
|
||||||
Cashier::calculateTaxes();
|
Cashier::calculateTaxes();
|
||||||
|
Cashier::useSubscriptionModel(Subscription::class);
|
||||||
|
|
||||||
if ($this->app->runningUnitTests()) {
|
if ($this->app->runningUnitTests()) {
|
||||||
Schema::defaultStringLength(191);
|
Schema::defaultStringLength(191);
|
||||||
|
|
Loading…
Reference in New Issue