trypost/app/Actions/Workspace/DeleteWorkspace.php
Paulo Castellano 12a1baa757 refactor(social): address PR review follow-ups
- DeleteWorkspace now reassigns affected users to another workspace they
  belong to (instead of nulling current_workspace_id), so deleting the
  current workspace while owning others no longer bounces to "create".
- Remove the dead LINKEDIN_PAGE_CLIENT_REDIRECT env (the unified flow uses
  a single callback; config no longer reads a page redirect).
- useOAuthPopup uses the Wayfinder connect helpers per platform instead of
  hardcoding /connect/{platform}.
- Unify the Inertia\Response alias in Mastodon/Bluesky controllers to
  InertiaResponse, matching the other connect controllers.
- Add tests: workspace reassignment on delete, and independent refresh of
  a linkedin row vs its linkedin-page sibling.
2026-06-25 15:01:23 -03:00

37 lines
979 B
PHP

<?php
declare(strict_types=1);
namespace App\Actions\Workspace;
use App\Jobs\PostHog\SyncAccountUsage;
use App\Models\User;
use App\Models\Workspace;
use App\Services\PostHogService;
class DeleteWorkspace
{
public static function execute(User $user, Workspace $workspace): void
{
User::where('current_workspace_id', $workspace->id)
->get()
->each(function (User $affected) use ($workspace): void {
$fallback = $affected->workspaces()
->where('workspaces.id', '!=', $workspace->id)
->first();
$affected->update(['current_workspace_id' => $fallback?->id]);
});
$account = $workspace->account;
$accountId = (string) $workspace->account_id;
$workspace->delete();
$account?->syncWorkspaceQuantity();
if (PostHogService::isEnabled()) {
SyncAccountUsage::dispatch($accountId, null);
}
}
}