feat: use TokenExpired state instead of Disconnected on first token failure

This commit is contained in:
Paulo Castellano 2026-04-01 11:46:51 -03:00
parent e034c0b572
commit 8ca846a865
4 changed files with 42 additions and 6 deletions

View file

@ -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);

View file

@ -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()) {

View file

@ -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([

View file

@ -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 () {