platform instanceof SocialPlatform) { return; } if (SocialAccount::occupiesNetwork((string) $socialAccount->workspace_id, $socialAccount->platform)) { throw new NetworkAlreadyConnectedException($socialAccount->platform); } } public function created(SocialAccount $socialAccount): void { $this->syncUsageAndOnboarding($socialAccount); } public function deleted(SocialAccount $socialAccount): void { $this->syncUsageAndOnboarding($socialAccount); } public function updated(SocialAccount $socialAccount): void { if (! $socialAccount->wasChanged('status')) { return; } $wasConnected = $socialAccount->getRawOriginal('status') === Status::Connected->value; $isConnected = $socialAccount->status === Status::Connected; if ($wasConnected !== $isConnected) { $this->identifyConnectedPlatforms($socialAccount); $this->notifyOnboarding($socialAccount); } } private function syncUsageAndOnboarding(SocialAccount $socialAccount): void { $this->syncUsage($socialAccount); $this->identifyConnectedPlatforms($socialAccount); if ($socialAccount->status === Status::Connected) { $this->notifyOnboarding($socialAccount); } } private function identifyConnectedPlatforms(SocialAccount $socialAccount): void { if (! PostHogService::isEnabled()) { return; } IdentifyConnectedPlatforms::dispatch((string) $socialAccount->workspace_id); } /** * First usable connect / last disconnect for the account. * Actor-less → syncAndNotify falls back to the account owner. */ private function notifyOnboarding(SocialAccount $socialAccount): void { $socialAccount->loadMissing('workspace.account'); $account = $socialAccount->workspace?->account; if (! $account?->isOnboardingOpen()) { return; } $hasOtherConnections = SocialAccount::query() ->whereIn('workspace_id', $account->workspaces()->select('id')) ->whereKeyNot($socialAccount->id) ->where('status', Status::Connected) ->exists(); if (! $hasOtherConnections) { OnboardingStatusUpdated::dispatchForWorkspace($socialAccount->workspace_id); } } private function syncUsage(SocialAccount $socialAccount): void { if (PostHogService::isEnabled()) { SyncAccountUsage::dispatch( (string) $socialAccount->workspace->account_id, (string) $socialAccount->workspace_id, ); } } }