trypost/app/Actions/User/CreateUser.php
Paulo Castellano 2da15df96c feat: introduce Account entity as billing owner and refactor architecture
- Create Account model as Cashier Billable entity (stripe, plan, subscription)
- Account owns workspaces and has an owner_id (User)
- User belongs to one Account via account_id
- Workspace belongs to Account via account_id, no longer has billing fields
- Remove Brand model entirely (workspaces serve as grouping)
- Rename brand_limit to workspace_limit in plans
- Workspace roles simplified: admin/member/viewer (owner via Account)
- Invites now belong to Account with workspaces JSON array
- Pennant features scope changed from Workspace to Account
- EnsureSubscribed middleware checks Account subscription
- All controllers updated: BillingController, OnboardingController,
  WorkspaceInviteController, SocialController, StripeEventListener
- Frontend: extract GoogleAuthButton component, create WorkspaceRole
  enum for type-safe role checks, fix all views for new architecture
- All 1101 tests passing
2026-04-14 22:22:04 -03:00

53 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Actions\User;
use App\Enums\User\Setup;
use App\Enums\UserWorkspace\Role;
use App\Models\Account;
use App\Models\User;
use App\Models\Workspace;
use Illuminate\Support\Facades\DB;
class CreateUser
{
/**
* @param array{name: string, email: string, password?: string, timezone?: string, setup?: Setup, email_verified_at?: \DateTimeInterface|null} $data
*/
public static function execute(array $data): User
{
return DB::transaction(function () use ($data): User {
$isInviteRegistration = data_get($data, 'is_invite', false);
$account = Account::create([
'name' => data_get($data, 'name')."'s Account",
]);
$user = User::create([
'name' => data_get($data, 'name'),
'email' => data_get($data, 'email'),
'password' => data_get($data, 'password'),
'setup' => data_get($data, 'setup', $isInviteRegistration ? Setup::Completed : Setup::Role),
'email_verified_at' => data_get($data, 'email_verified_at', $isInviteRegistration ? now() : null),
'account_id' => $account->id,
]);
$account->update(['owner_id' => $user->id]);
$workspace = Workspace::create([
'account_id' => $account->id,
'user_id' => $user->id,
'name' => $user->name."'s Workspace",
'timezone' => data_get($data, 'timezone', 'UTC'),
]);
$workspace->members()->attach($user->id, ['role' => Role::Member->value]);
$user->update(['current_workspace_id' => $workspace->id]);
return $user;
});
}
}