diff --git a/app/Jobs/PublishToSocialPlatform.php b/app/Jobs/PublishToSocialPlatform.php index a8cd9925..20fd03d5 100644 --- a/app/Jobs/PublishToSocialPlatform.php +++ b/app/Jobs/PublishToSocialPlatform.php @@ -126,7 +126,7 @@ public function handle(): void 'platform_error_code' => $e->platformErrorCode, 'failed_at' => now()->toIso8601String(), ]); - $this->postPlatform->socialAccount->markAsDisconnected($e->getMessage()); + $this->postPlatform->socialAccount->markAsTokenExpired($e->getMessage()); break; } catch (SocialPublishException $e) { Log::error('Social publish failed: '.$e->userMessage); diff --git a/app/Jobs/VerifyWorkspaceConnections.php b/app/Jobs/VerifyWorkspaceConnections.php index cabca26f..28fa67b5 100644 --- a/app/Jobs/VerifyWorkspaceConnections.php +++ b/app/Jobs/VerifyWorkspaceConnections.php @@ -30,7 +30,7 @@ public function __construct(public Workspace $workspace) {} public function handle(ConnectionVerifier $verifier): void { $connectedAccounts = $this->workspace->socialAccounts() - ->where('status', Status::Connected) + ->whereIn('status', [Status::Connected, Status::TokenExpired]) ->get(); if ($connectedAccounts->isEmpty()) { diff --git a/app/Models/SocialAccount.php b/app/Models/SocialAccount.php index 48b53099..365f7eb3 100644 --- a/app/Models/SocialAccount.php +++ b/app/Models/SocialAccount.php @@ -131,6 +131,42 @@ public function markAsDisconnected(string $errorMessage): void } } + public function markAsTokenExpired(string $errorMessage): void + { + $lock = Cache::lock("social_account_disconnect:{$this->id}", 10); + + if ($lock->get()) { + try { + $this->refresh(); + $wasConnected = $this->status === Status::Connected; + + $this->update([ + 'status' => Status::TokenExpired, + 'error_message' => $errorMessage, + 'disconnected_at' => now(), + ]); + + if ($wasConnected && $this->workspace->owner) { + $platformName = $this->platform->label(); + $accountName = $this->username ?? $this->display_name; + + SendNotification::dispatch( + user: $this->workspace->owner, + workspaceId: $this->workspace_id, + type: Type::AccountDisconnected, + channel: Channel::Both, + title: "{$platformName} token expired", + body: "@{$accountName} needs to be reconnected", + data: ['social_account_id' => $this->id], + mailable: new AccountDisconnected($this), + ); + } + } finally { + $lock->release(); + } + } + } + public function markAsConnected(): void { $this->update([ diff --git a/tests/Feature/Jobs/PublishToSocialPlatformTest.php b/tests/Feature/Jobs/PublishToSocialPlatformTest.php index 72a1492b..4dcb4f98 100644 --- a/tests/Feature/Jobs/PublishToSocialPlatformTest.php +++ b/tests/Feature/Jobs/PublishToSocialPlatformTest.php @@ -91,7 +91,7 @@ expect($this->postPlatform->error_message)->toBe('API Error'); }); -test('publish to social platform disconnects account on token expired', function () { +test('publish to social platform marks account as token expired on auth failure', function () { Event::fake(); Mail::fake(); @@ -106,7 +106,7 @@ $this->socialAccount->refresh(); expect($this->postPlatform->status)->toBe(PlatformStatus::Failed); - expect($this->socialAccount->status)->toBe(AccountStatus::Disconnected); + expect($this->socialAccount->status)->toBe(AccountStatus::TokenExpired); }); test('publish to social platform updates post status when all platforms finished', function () { @@ -294,7 +294,7 @@ expect($this->socialAccount->status)->not->toBe(AccountStatus::Disconnected); }); -test('it disconnects account when token refresh fails during publish retry', function () { +test('it marks account as token expired when refresh fails during publish retry', function () { Event::fake(); $publisher = Mockery::mock(LinkedInPublisher::class); @@ -315,7 +315,7 @@ $this->socialAccount->refresh(); expect($this->postPlatform->status)->toBe(PlatformStatus::Failed); - expect($this->socialAccount->status)->toBe(AccountStatus::Disconnected); + expect($this->socialAccount->status)->toBe(AccountStatus::TokenExpired); }); test('publish to social platform skips if already published (idempotency)', function () {