This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\Organization;
|
||||
use App\Models\Plan;
|
||||
use App\Models\User;
|
||||
use App\OrganizationRole;
|
||||
use App\WorkspaceType;
|
||||
@@ -61,6 +62,20 @@ class OrganizationFactory extends Factory
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Give the organization the plan with the given key.
|
||||
*/
|
||||
public function withPlan(string $key): static
|
||||
{
|
||||
return $this->afterCreating(function (Organization $organization) use ($key): void {
|
||||
$planId = Plan::query()->where('key', $key)->value('id');
|
||||
|
||||
if ($planId !== null) {
|
||||
$organization->forceFill(['plan_id' => $planId])->save();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given user to the organization with the given role.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\Plan;
|
||||
use App\WorkspaceType;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* @extends Factory<Plan>
|
||||
*/
|
||||
class PlanFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* A default plan is unlimited and not purchasable, matching the most
|
||||
* permissive tier so tests can opt into limits explicitly.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'key' => fake()->unique()->slug(2),
|
||||
'name' => fake()->words(2, true),
|
||||
'workspace_type' => WorkspaceType::Personal,
|
||||
'stripe_price' => null,
|
||||
'interval' => 'month',
|
||||
'amount' => 0,
|
||||
'seat_limit' => null,
|
||||
'max_folders' => null,
|
||||
'max_folder_depth' => null,
|
||||
'max_notes_per_folder' => null,
|
||||
'note_chars' => null,
|
||||
'sort_order' => 0,
|
||||
'is_active' => true,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the plan is sold through Stripe at the given price.
|
||||
*/
|
||||
public function purchasable(?string $price = null): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'stripe_price' => $price ?? 'price_'.Str::random(12),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the plan caps the given limits.
|
||||
*/
|
||||
public function limited(int $maxFolders, int $maxFolderDepth, int $maxNotesPerFolder, int $noteChars): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'max_folders' => $maxFolders,
|
||||
'max_folder_depth' => $maxFolderDepth,
|
||||
'max_notes_per_folder' => $maxNotesPerFolder,
|
||||
'note_chars' => $noteChars,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\Plan;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
@@ -29,6 +30,14 @@ class UserFactory extends Factory
|
||||
*/
|
||||
protected bool $subscribed = true;
|
||||
|
||||
/**
|
||||
* The plan given to the user's personal organization. Following the plan
|
||||
* of record, a factory user starts on the unlimited plan so tests are not
|
||||
* constrained by folder or note limits. Call withPlan() or withFreePlan()
|
||||
* to opt into a specific tier.
|
||||
*/
|
||||
protected ?string $planKey = 'unlimited';
|
||||
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
@@ -49,10 +58,10 @@ class UserFactory extends Factory
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the model and, unless opted out, give it an active subscription on
|
||||
* the personal organization the model's created hook just made.
|
||||
* Create the model and give its personal organization a plan. Unless opted
|
||||
* out, the model also gets an active subscription on that organization.
|
||||
*
|
||||
* The flag is read here rather than in an afterCreating callback because
|
||||
* The flags are read here rather than in an afterCreating callback because
|
||||
* afterCreating() hands back a new factory instance, so a closure registered
|
||||
* there would close over a copy that unsubscribed() never reaches.
|
||||
*
|
||||
@@ -63,13 +72,15 @@ class UserFactory extends Factory
|
||||
{
|
||||
$result = parent::create($attributes, $parent);
|
||||
|
||||
if (! $this->subscribed) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$users = $result instanceof SupportCollection ? $result : collect([$result]);
|
||||
|
||||
$users->each(fn (User $user) => $this->subscribe($user));
|
||||
$users->each(function (User $user): void {
|
||||
if ($this->subscribed) {
|
||||
$this->subscribe($user);
|
||||
}
|
||||
|
||||
$this->assignPlan($user, $this->planKey);
|
||||
});
|
||||
|
||||
return $result;
|
||||
}
|
||||
@@ -85,6 +96,24 @@ class UserFactory extends Factory
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Give the user's personal organization the plan with the given key.
|
||||
*/
|
||||
public function withPlan(string $key): static
|
||||
{
|
||||
$this->planKey = $key;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Give the user's personal organization the free plan.
|
||||
*/
|
||||
public function withFreePlan(): static
|
||||
{
|
||||
return $this->withPlan('free');
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the model's email address should be unverified.
|
||||
*/
|
||||
@@ -140,4 +169,28 @@ class UserFactory extends Factory
|
||||
|
||||
Subscription::factory()->create(['organization_id' => $organization->id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign a plan to the user's current organization.
|
||||
*/
|
||||
protected function assignPlan(User $user, ?string $planKey): void
|
||||
{
|
||||
if ($planKey === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$organization = $user->currentOrganization()->first();
|
||||
|
||||
if ($organization === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$planId = Plan::query()->where('key', $planKey)->value('id');
|
||||
|
||||
if ($planId === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$organization->forceFill(['plan_id' => $planId])->save();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user