mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-05 18:23:48 +00:00
handle SubscriptionWasCreated, SubscriptionWasExpired, SubscriptionWasUpdated on VueFileManager backend
This commit is contained in:
30
database/factories/UserLimitationFactory.php
Normal file
30
database/factories/UserLimitationFactory.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Users\Models\UserLimitation;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
class UserLimitationFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* The name of the factory's corresponding model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $model = UserLimitation::class;
|
||||
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function definition()
|
||||
{
|
||||
return [
|
||||
'user_id' => $this->faker->uuid,
|
||||
'max_storage_amount' => $this->faker->randomElement([100, 200, 300]),
|
||||
'max_team_members' => $this->faker->randomElement([10, 20, 30]),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ namespace App\Users\Actions;
|
||||
|
||||
use App\Users\Models\User;
|
||||
use Illuminate\Http\Response;
|
||||
use App\Users\Models\UserSettings;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use App\Users\Requests\RegisterUserRequest;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
|
||||
namespace App\Users\Models;
|
||||
|
||||
use ByteUnits\Metric;
|
||||
@@ -93,7 +92,7 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
{
|
||||
$is_storage_limit = get_settings('storage_limitation') ?? 1;
|
||||
|
||||
if (!$is_storage_limit) {
|
||||
if (! $is_storage_limit) {
|
||||
return [
|
||||
'used' => $this->usedCapacity,
|
||||
'used_formatted' => Metric::bytes($this->usedCapacity)->format(),
|
||||
@@ -101,7 +100,7 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
}
|
||||
|
||||
return [
|
||||
'used' => (float)get_storage_fill_percentage($this->usedCapacity, $this->limitations->max_storage_amount),
|
||||
'used' => (float) get_storage_fill_percentage($this->usedCapacity, $this->limitations->max_storage_amount),
|
||||
'used_formatted' => get_storage_fill_percentage($this->usedCapacity, $this->limitations->max_storage_amount) . '%',
|
||||
'capacity' => $this->limitations->max_storage_amount,
|
||||
'capacity_formatted' => format_gigabytes($this->limitations->max_storage_amount),
|
||||
@@ -114,7 +113,7 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
public function getUsedCapacityAttribute(): int
|
||||
{
|
||||
return $this->filesWithTrashed
|
||||
->map(fn($item) => $item->getRawOriginal())->sum('filesize');
|
||||
->map(fn ($item) => $item->getRawOriginal())->sum('filesize');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,9 +2,13 @@
|
||||
namespace App\Users\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Database\Factories\UserLimitationFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
|
||||
class UserLimitation extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $guarded = [];
|
||||
@@ -16,4 +20,9 @@ class UserLimitation extends Model
|
||||
public $incrementing = false;
|
||||
|
||||
protected $keyType = 'string';
|
||||
|
||||
protected static function newFactory(): UserLimitationFactory
|
||||
{
|
||||
return UserLimitationFactory::new();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,4 @@ class SettingsResource extends JsonResource
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
|
||||
namespace App\Users\Resources;
|
||||
|
||||
use Domain\Folders\Resources\FolderCollection;
|
||||
@@ -35,17 +34,15 @@ class UserResource extends JsonResource
|
||||
'limitations' => [
|
||||
'id' => $this->id,
|
||||
'type' => 'limitations',
|
||||
'data' => [
|
||||
'data' => [
|
||||
'attributes' => $this->limitations,
|
||||
],
|
||||
],
|
||||
$this->mergeWhen($this->hasSubscription(), fn() => [
|
||||
$this->mergeWhen($this->hasSubscription(), fn () => [
|
||||
'subscription' => new SubscriptionResource($this->subscription),
|
||||
]),
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ namespace Domain\Admin\Controllers\Users;
|
||||
|
||||
use App\Users\Models\User;
|
||||
use Illuminate\Http\Response;
|
||||
use App\Users\Models\UserSettings;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Users\Resources\UserResource;
|
||||
use App\Users\Resources\UsersCollection;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
|
||||
namespace Support\Listeners;
|
||||
|
||||
use Illuminate\Events\Dispatcher;
|
||||
@@ -9,26 +8,26 @@ use VueFileManager\Subscription\Support\Events\SubscriptionWasUpdated;
|
||||
|
||||
class SubscriptionEventSubscriber
|
||||
{
|
||||
public function handleSubscriptionWasCreated($subscription)
|
||||
public function handleSubscriptionWasCreated($event)
|
||||
{
|
||||
$subscription->user->limitations()->update([
|
||||
'max_storage_amount' => $subscription->feature('max_storage_amount'),
|
||||
'max_team_members' => $subscription->feature('max_team_members'),
|
||||
$event->subscription->user->limitations()->update([
|
||||
'max_storage_amount' => $event->subscription->feature('max_storage_amount'),
|
||||
'max_team_members' => $event->subscription->feature('max_team_members'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function handleSubscriptionWasUpdated($subscription)
|
||||
public function handleSubscriptionWasUpdated($event)
|
||||
{
|
||||
$subscription->user->limitations()->update([
|
||||
'max_storage_amount' => $subscription->feature('max_storage_amount'),
|
||||
'max_team_members' => $subscription->feature('max_team_members'),
|
||||
$event->subscription->user->limitations()->update([
|
||||
'max_storage_amount' => $event->subscription->feature('max_storage_amount'),
|
||||
'max_team_members' => $event->subscription->feature('max_team_members'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function handleSubscriptionWasExpired($subscription)
|
||||
public function handleSubscriptionWasExpired($event)
|
||||
{
|
||||
$subscription->user->limitations()->update([
|
||||
'max_storage_amount' => get_settings('default_storage_amount'),
|
||||
$event->subscription->user->limitations()->update([
|
||||
'max_storage_amount' => get_settings('default_storage_amount') ?? 1,
|
||||
'max_team_members' => 5,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\App\Users;
|
||||
|
||||
use Storage;
|
||||
|
||||
@@ -177,7 +177,7 @@ class UserAccountTest extends TestCase
|
||||
'limitations' => [
|
||||
'id' => $user->id,
|
||||
'type' => 'limitations',
|
||||
'data' => [
|
||||
'data' => [
|
||||
'attributes' => $user->limitations,
|
||||
],
|
||||
],
|
||||
|
||||
102
tests/App/Users/UserSubscriptionTest.php
Normal file
102
tests/App/Users/UserSubscriptionTest.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
namespace Tests\App\Users;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Users\Models\User;
|
||||
use VueFileManager\Subscription\Domain\Plans\Models\Plan;
|
||||
use VueFileManager\Subscription\Domain\Plans\Models\PlanFeature;
|
||||
use VueFileManager\Subscription\Support\Events\SubscriptionWasCreated;
|
||||
use VueFileManager\Subscription\Support\Events\SubscriptionWasExpired;
|
||||
use VueFileManager\Subscription\Support\Events\SubscriptionWasUpdated;
|
||||
|
||||
class UserSubscriptionTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_set_user_limitations_for_new_subscription()
|
||||
{
|
||||
$plan = Plan::factory()
|
||||
->has(PlanFeature::factory()
|
||||
->count(2)
|
||||
->sequence(
|
||||
[
|
||||
'key' => 'max_storage_amount',
|
||||
'value' => 200,
|
||||
],
|
||||
[
|
||||
'key' => 'max_team_members',
|
||||
'value' => 20,
|
||||
],
|
||||
), 'features')
|
||||
->create();
|
||||
|
||||
$user = User::factory()
|
||||
->hasSubscription([
|
||||
'plan_id' => $plan->id,
|
||||
])
|
||||
->create();
|
||||
|
||||
SubscriptionWasCreated::dispatch($user->subscription);
|
||||
|
||||
$this->assertDatabaseHas('user_limitations', [
|
||||
'max_storage_amount' => 200,
|
||||
'max_team_members' => 20,
|
||||
]);
|
||||
}
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_set_user_limitations_for_updated_subscription()
|
||||
{
|
||||
$plan = Plan::factory()
|
||||
->has(PlanFeature::factory()
|
||||
->count(2)
|
||||
->sequence(
|
||||
[
|
||||
'key' => 'max_storage_amount',
|
||||
'value' => 200,
|
||||
],
|
||||
[
|
||||
'key' => 'max_team_members',
|
||||
'value' => 20,
|
||||
],
|
||||
), 'features')
|
||||
->create();
|
||||
|
||||
$user = User::factory()
|
||||
->hasSubscription([
|
||||
'plan_id' => $plan->id,
|
||||
])
|
||||
->create();
|
||||
|
||||
SubscriptionWasUpdated::dispatch($user->subscription);
|
||||
|
||||
$this->assertDatabaseHas('user_limitations', [
|
||||
'max_storage_amount' => 200,
|
||||
'max_team_members' => 20,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_set_user_limitations_for_expired_subscription()
|
||||
{
|
||||
$user = User::factory()
|
||||
->hasSubscription()
|
||||
->create();
|
||||
|
||||
$user->limitations()->update([
|
||||
'max_storage_amount' => 200,
|
||||
'max_team_members' => 20,
|
||||
]);
|
||||
|
||||
SubscriptionWasExpired::dispatch($user->subscription);
|
||||
|
||||
$this->assertDatabaseHas('user_limitations', [
|
||||
'max_storage_amount' => 1,
|
||||
'max_team_members' => 5,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user