trypost/app/Actions/Workspace/CreateWorkspace.php
Paulo Castellano aacc4390a2 refactor(billing): replace MonthlyCreditsLimit Pennant feature with BillingCycle
Credit allotment is now derived directly from BillingCycle::for($account)->creditAllotment()
instead of a cached Pennant feature, removing the dynamic cache-invalidation footgun
(forgetPlanFeatureCache) that had to be called from every subscription/workspace mutation.
2026-06-22 09:55:03 -03:00

49 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Actions\Workspace;
use App\Enums\UserWorkspace\Role;
use App\Models\User;
use App\Models\Workspace;
use Illuminate\Support\Facades\DB;
class CreateWorkspace
{
/**
* @param array<string, mixed> $data
*/
public static function execute(User $user, array $data): Workspace
{
$attributes = array_filter([
'name' => data_get($data, 'name'),
'brand_website' => data_get($data, 'brand_website'),
'brand_description' => data_get($data, 'brand_description'),
'brand_voice_traits' => data_get($data, 'brand_voice_traits'),
'brand_color' => data_get($data, 'brand_color'),
'background_color' => data_get($data, 'background_color'),
'text_color' => data_get($data, 'text_color'),
'content_language' => data_get($data, 'content_language'),
], static fn ($value): bool => $value !== null);
$workspace = DB::transaction(function () use ($user, $attributes): Workspace {
$workspace = Workspace::create([
...$attributes,
'account_id' => $user->account_id,
'user_id' => $user->id,
]);
// Creator becomes Admin of the workspace they made. The Account Owner
// is resolved separately (via account.owner_id) and outranks this role.
$workspace->members()->attach($user->id, ['role' => Role::Admin->value]);
$user->switchWorkspace($workspace);
return $workspace;
});
$user->account?->syncWorkspaceQuantity();
return $workspace;
}
}