added pricing, orgs
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-10-04 20:34:19 -07:00
parent 999d5ad28d
commit 2ca3860557
52 changed files with 3467 additions and 47 deletions
@@ -0,0 +1,59 @@
<?php
namespace Database\Factories;
use App\Models\Organization;
use App\Models\User;
use App\OrganizationRole;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<Organization>
*/
class OrganizationFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => fake()->unique()->company(),
'personal_workspace' => false,
'lifetime_access' => false,
];
}
/**
* Indicate that the organization is a single user's personal workspace.
*/
public function personal(): static
{
return $this->state(fn (array $attributes) => [
'personal_workspace' => true,
]);
}
/**
* Give the organization perpetual access, as granted to accounts that
* predate subscriptions.
*/
public function withLifetimeAccess(): static
{
return $this->state(fn (array $attributes) => [
'lifetime_access' => true,
]);
}
/**
* Add the given user to the organization with the given role.
*/
public function withMember(User $user, OrganizationRole $role = OrganizationRole::Owner): static
{
return $this->afterCreating(function (Organization $organization) use ($user, $role): void {
$organization->users()->attach($user, ['role' => $role->value]);
});
}
}
+25
View File
@@ -0,0 +1,25 @@
<?php
namespace Database\Factories;
use App\Models\Setting;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<Setting>
*/
class SettingFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'key' => 'setting.'.fake()->unique()->word(),
'value' => fake()->word(),
];
}
}
+63
View File
@@ -3,9 +3,13 @@
namespace Database\Factories;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection as SupportCollection;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Laravel\Cashier\Subscription;
/**
* @extends Factory<User>
@@ -17,6 +21,14 @@ class UserFactory extends Factory
*/
protected static ?string $password;
/**
* Whether created users are given an active subscription.
*
* A factory user defaults to being entitled so that tests can focus on the
* behaviour they are covering. Call unsubscribed() to opt out.
*/
protected bool $subscribed = true;
/**
* Define the model's default state.
*
@@ -36,6 +48,43 @@ 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.
*
* The flag is 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.
*
* @param array<string, mixed> $attributes
* @return Collection<int, User>|User
*/
public function create($attributes = [], ?Model $parent = null): Collection|User
{
$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));
return $result;
}
/**
* Indicate that the model does not have an active subscription, as a newly
* registered account that has not been through checkout.
*/
public function unsubscribed(): static
{
$this->subscribed = false;
return $this;
}
/**
* Indicate that the model's email address should be unverified.
*/
@@ -77,4 +126,18 @@ class UserFactory extends Factory
'two_factor_confirmed_at' => now(),
]);
}
/**
* Attach an active subscription to the user's current organization.
*/
protected function subscribe(User $user): void
{
$organization = $user->currentOrganization()->first();
if ($organization === null) {
return;
}
Subscription::factory()->create(['organization_id' => $organization->id]);
}
}
@@ -0,0 +1,41 @@
<?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::create('subscriptions', function (Blueprint $table) {
$table->id();
// Cashier resolves the owner foreign key from the customer model
// (Subscription::owner()), which is App\Models\Organization here
// rather than App\Models\User.
$table->foreignId('organization_id');
$table->string('type');
$table->string('stripe_id')->unique();
$table->string('stripe_status');
$table->string('stripe_price')->nullable();
$table->integer('quantity')->nullable();
$table->timestamp('trial_ends_at')->nullable();
$table->timestamp('ends_at')->nullable();
$table->timestamps();
$table->index(['organization_id', 'stripe_status']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('subscriptions');
}
};
@@ -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::create('subscription_items', function (Blueprint $table) {
$table->id();
$table->foreignId('subscription_id');
$table->string('stripe_id')->unique();
$table->string('stripe_product');
$table->string('stripe_price');
$table->integer('quantity')->nullable();
$table->timestamps();
$table->index(['subscription_id', 'stripe_price']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('subscription_items');
}
};
@@ -0,0 +1,28 @@
<?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('subscription_items', function (Blueprint $table) {
$table->string('meter_id')->nullable()->after('stripe_price');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('subscription_items', function (Blueprint $table) {
$table->dropColumn('meter_id');
});
}
};
@@ -0,0 +1,28 @@
<?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('subscription_items', function (Blueprint $table) {
$table->string('meter_event_name')->nullable()->after('quantity');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('subscription_items', function (Blueprint $table) {
$table->dropColumn('meter_event_name');
});
}
};
@@ -0,0 +1,41 @@
<?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::create('organizations', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->boolean('personal_workspace')->default(true);
$table->boolean('lifetime_access')->default(false);
// The remaining columns replace Cashier's published
// create_customer_columns migration, which targets the users table.
// This model is the Cashier customer, so they belong here. The
// binary collation keeps Stripe's case-sensitive ids intact on MySQL.
$table->string('stripe_id')->nullable()->charset('utf8')->collation('utf8_bin');
$table->string('pm_type')->nullable();
$table->string('pm_last_four', 4)->nullable();
$table->timestamp('trial_ends_at')->nullable();
$table->timestamps();
$table->index('stripe_id');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('organizations');
}
};
@@ -0,0 +1,32 @@
<?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::create('organization_user', function (Blueprint $table) {
$table->id();
$table->foreignId('organization_id')->constrained()->cascadeOnDelete();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('role')->default('owner');
$table->timestamps();
$table->unique(['organization_id', 'user_id']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('organization_user');
}
};
@@ -0,0 +1,32 @@
<?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('users', function (Blueprint $table) {
$table->foreignId('current_organization_id')
->nullable()
->after('id')
->constrained('organizations')
->nullOnDelete();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropConstrainedForeignId('current_organization_id');
});
}
};
@@ -0,0 +1,54 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Give every user who existed before subscriptions existed a personal
* organization with permanent access, so that turning registration into a
* paid wall does not lock the existing accounts out of their own data.
*
* Idempotent: users that already have a current organization are skipped.
*/
public function up(): void
{
DB::table('users')
->select(['id', 'name', 'current_organization_id'])
->whereNull('current_organization_id')
->orderBy('id')
->each(function (object $user): void {
$organizationId = DB::table('organizations')->insertGetId([
'name' => $user->name."'s Workspace",
'personal_workspace' => true,
'lifetime_access' => true,
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('organization_user')->insert([
'organization_id' => $organizationId,
'user_id' => $user->id,
'role' => 'owner',
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('users')
->where('id', $user->id)
->update(['current_organization_id' => $organizationId]);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
if (Schema::hasTable('organizations')) {
DB::table('organizations')->where('personal_workspace', true)->delete();
}
}
};
@@ -0,0 +1,40 @@
<?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('settings', function (Blueprint $table) {
$table->id();
$table->string('key')->unique();
$table->text('value')->nullable();
$table->timestamps();
});
// The Pro plan's advertised amount starts as whole units, matching the
// conventional price, so the pages that show it have something sensible
// to render before an administrator has ever edited it.
DB::table('settings')->insert([
'key' => 'plans.pro.amount',
'value' => '1000',
'created_at' => now(),
'updated_at' => now(),
]);
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('settings');
}
};
+8 -1
View File
@@ -12,7 +12,7 @@ class BaseDatabaseSeeder extends Seeder
*/
public function run(): void
{
User::updateOrCreate(
$admin = User::updateOrCreate(
['email' => config('admin.email')],
[
'name' => config('admin.name'),
@@ -20,5 +20,12 @@ class BaseDatabaseSeeder extends Seeder
'is_admin' => true,
],
);
// Grandfather the admin so a fresh install is usable before any Stripe
// price is configured. The organization itself is created by the
// model's created hook.
$admin->currentOrganization()->first()?->forceFill([
'lifetime_access' => true,
])->save();
}
}