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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('plans', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('key')->unique();
|
||||
$table->string('name');
|
||||
|
||||
// Which kind of workspace the plan provisions. Free and personal
|
||||
// plans map to a single-user personal workspace, family to a shared
|
||||
// family workspace, and organization to a shared organization.
|
||||
$table->string('workspace_type')->default('personal');
|
||||
|
||||
// The Stripe price id the plan is sold against. Plans without one
|
||||
// are not purchasable and are hidden from the pricing page.
|
||||
$table->string('stripe_price')->nullable();
|
||||
$table->string('interval')->default('month');
|
||||
|
||||
// The advertised price, in minor units, so a bad edit cannot
|
||||
// silently advertise a free plan.
|
||||
$table->unsignedInteger('amount')->default(0);
|
||||
|
||||
// Limits. A null limit means unlimited.
|
||||
$table->unsignedInteger('seat_limit')->nullable();
|
||||
$table->unsignedInteger('max_folders')->nullable();
|
||||
$table->unsignedInteger('max_folder_depth')->nullable();
|
||||
$table->unsignedInteger('max_notes_per_folder')->nullable();
|
||||
$table->unsignedInteger('note_chars')->nullable();
|
||||
|
||||
$table->unsignedInteger('sort_order')->default(0);
|
||||
$table->boolean('is_active')->default(true);
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
// The plans that ship with the application. The paid personal plan
|
||||
// reuses the configured Pro price, so it stays in step with the Stripe
|
||||
// product created in the dashboard.
|
||||
$personalPrice = config('plans.plans.pro.price');
|
||||
$personalPrice = is_string($personalPrice) && $personalPrice !== '' ? $personalPrice : null;
|
||||
|
||||
$plans = [
|
||||
[
|
||||
'key' => 'free',
|
||||
'name' => 'Free',
|
||||
'workspace_type' => 'personal',
|
||||
'stripe_price' => null,
|
||||
'amount' => 0,
|
||||
'seat_limit' => 1,
|
||||
'max_folders' => 3,
|
||||
'max_folder_depth' => 1,
|
||||
'max_notes_per_folder' => 50,
|
||||
'note_chars' => 1000,
|
||||
'sort_order' => 0,
|
||||
],
|
||||
[
|
||||
'key' => 'personal',
|
||||
'name' => 'Personal',
|
||||
'workspace_type' => 'personal',
|
||||
'stripe_price' => $personalPrice,
|
||||
'amount' => 1000,
|
||||
'seat_limit' => 1,
|
||||
'max_folders' => null,
|
||||
'max_folder_depth' => null,
|
||||
'max_notes_per_folder' => null,
|
||||
'note_chars' => null,
|
||||
'sort_order' => 1,
|
||||
],
|
||||
[
|
||||
'key' => 'family',
|
||||
'name' => 'Family',
|
||||
'workspace_type' => 'family',
|
||||
'stripe_price' => null,
|
||||
'amount' => 2500,
|
||||
'seat_limit' => 6,
|
||||
'max_folders' => null,
|
||||
'max_folder_depth' => null,
|
||||
'max_notes_per_folder' => null,
|
||||
'note_chars' => null,
|
||||
'sort_order' => 2,
|
||||
],
|
||||
[
|
||||
'key' => 'organization',
|
||||
'name' => 'Organization',
|
||||
'workspace_type' => 'organization',
|
||||
'stripe_price' => null,
|
||||
'amount' => 9900,
|
||||
'seat_limit' => null,
|
||||
'max_folders' => null,
|
||||
'max_folder_depth' => null,
|
||||
'max_notes_per_folder' => null,
|
||||
'note_chars' => null,
|
||||
'sort_order' => 3,
|
||||
],
|
||||
[
|
||||
'key' => 'unlimited',
|
||||
'name' => 'Unlimited',
|
||||
'workspace_type' => 'personal',
|
||||
'stripe_price' => null,
|
||||
'amount' => 0,
|
||||
'seat_limit' => 1,
|
||||
'max_folders' => null,
|
||||
'max_folder_depth' => null,
|
||||
'max_notes_per_folder' => null,
|
||||
'note_chars' => null,
|
||||
'sort_order' => 4,
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($plans as $plan) {
|
||||
DB::table('plans')->insert($plan + [
|
||||
'interval' => 'month',
|
||||
'is_active' => true,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('plans');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('organizations', function (Blueprint $table) {
|
||||
// Nullable while the backfill migration assigns every existing
|
||||
// workspace a plan; new workspaces are given one when created.
|
||||
$table->foreignId('plan_id')
|
||||
->nullable()
|
||||
->after('lifetime_access')
|
||||
->constrained()
|
||||
->nullOnDelete();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('organizations', function (Blueprint $table) {
|
||||
$table->dropConstrainedForeignId('plan_id');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* A null notes limit means the user has no per-user override and the limit
|
||||
* comes from their workspace's plan instead.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->unsignedInteger('notes_limit')->nullable()->default(null)->change();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
DB::table('users')->whereNull('notes_limit')->update(['notes_limit' => 1000]);
|
||||
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->unsignedInteger('notes_limit')->nullable(false)->default(1000)->change();
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* Give every workspace that predates plans one, so that entitlement and
|
||||
* limits can be read from a plan instead of the legacy lifetime flag and
|
||||
* subscription rows:
|
||||
*
|
||||
* - Grandfathered accounts stay entitled on the unlimited plan.
|
||||
* - Existing subscribers move to the plan their Stripe price maps to.
|
||||
* - Everyone else starts on the free plan.
|
||||
*
|
||||
* Idempotent: workspaces that already have a plan are left alone.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
$unlimited = $this->planId('unlimited');
|
||||
$personal = $this->planId('personal');
|
||||
$free = $this->planId('free');
|
||||
|
||||
if ($unlimited === null || $personal === null || $free === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
DB::table('organizations')
|
||||
->where('lifetime_access', true)
|
||||
->whereNull('plan_id')
|
||||
->update(['plan_id' => $unlimited]);
|
||||
|
||||
$proPrice = config('plans.plans.pro.price');
|
||||
|
||||
if (is_string($proPrice) && $proPrice !== '') {
|
||||
$subscribed = DB::table('subscriptions')
|
||||
->where('stripe_price', $proPrice)
|
||||
->distinct()
|
||||
->pluck('organization_id');
|
||||
|
||||
DB::table('organizations')
|
||||
->whereNull('plan_id')
|
||||
->whereIn('id', $subscribed)
|
||||
->update(['plan_id' => $personal]);
|
||||
}
|
||||
|
||||
DB::table('organizations')
|
||||
->whereNull('plan_id')
|
||||
->update(['plan_id' => $free]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
DB::table('organizations')->update(['plan_id' => null]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the id of the plan with the given key, if it exists.
|
||||
*/
|
||||
private function planId(string $key): ?int
|
||||
{
|
||||
$id = DB::table('plans')->where('key', $key)->value('id');
|
||||
|
||||
return $id === null ? null : (int) $id;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user