trypost/app/Jobs/VerifyWorkspaceConnections.php
Paulo Castellano 09e37f2879 feat: notification system with SendNotification job, dialog UI, tests
Backend:
- Create notifications table (user_id, workspace_id, type, channel,
  title, body, data JSON, read_at, archived_at)
- Create Notification model with Type enum (post_failed,
  account_disconnected, invite_received, member_joined, member_removed)
  and Channel enum (email, in_app, both)
- Create SendNotification job: isolated from publish flow, handles
  saving in-app notification and sending email independently
- NotificationController: index (excludes archived, scoped to workspace),
  markAsRead, markAllAsRead, archiveAll
- Integrate with PublishToSocialPlatform (post failed/partial)
- Integrate with VerifyWorkspaceConnections (batch disconnection)
- Integrate with SocialAccount::markAsDisconnected (single disconnection)
- All use SendNotification::dispatch() instead of direct Mail::to()

Frontend:
- NotificationBell component in sidebar footer with unread badge
- Dialog with notification list, mark as read, mark all read, archive all
- Click navigates to relevant page (post edit, accounts)
- i18n for notifications UI (en, es, pt-BR)

Tests:
- 8 tests for NotificationController (auth, CRUD, workspace scoping)
- 4 tests for SendNotification job (channels, email, data storage)

All 745 tests passing.
2026-03-30 16:47:03 -03:00

128 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Jobs;
use App\Enums\Notification\Channel;
use App\Enums\Notification\Type;
use App\Enums\SocialAccount\Status;
use App\Exceptions\TokenExpiredException;
use App\Mail\WorkspaceConnectionsDisconnected;
use App\Models\SocialAccount;
use App\Models\Workspace;
use App\Services\Social\ConnectionVerifier;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
class VerifyWorkspaceConnections implements ShouldQueue
{
use Queueable;
public int $tries = 1;
public int $timeout = 120;
public function __construct(public Workspace $workspace) {}
public function handle(ConnectionVerifier $verifier): void
{
$connectedAccounts = $this->workspace->socialAccounts()
->where('status', Status::Connected)
->get();
if ($connectedAccounts->isEmpty()) {
return;
}
Log::info('Verifying workspace connections', [
'workspace_id' => $this->workspace->id,
'workspace_name' => $this->workspace->name,
'account_count' => $connectedAccounts->count(),
]);
$disconnectedAccounts = collect();
foreach ($connectedAccounts as $account) {
if ($this->verifyAccount($verifier, $account)) {
continue;
}
$disconnectedAccounts->push($account);
}
if ($disconnectedAccounts->isNotEmpty()) {
$this->notifyOwner($disconnectedAccounts);
}
}
private function verifyAccount(ConnectionVerifier $verifier, SocialAccount $account): bool
{
try {
$verifier->verify($account);
Log::info('Social account connection verified', [
'account_id' => $account->id,
'platform' => $account->platform->value,
]);
return true;
} catch (TokenExpiredException $e) {
Log::warning('Social account connection is invalid', [
'account_id' => $account->id,
'platform' => $account->platform->value,
'error' => $e->getMessage(),
]);
$account->update([
'status' => Status::Disconnected,
'error_message' => $e->getMessage(),
'disconnected_at' => now(),
]);
return false;
} catch (\Exception $e) {
Log::error('Failed to verify social account connection', [
'account_id' => $account->id,
'platform' => $account->platform->value,
'error' => $e->getMessage(),
]);
return true;
}
}
/**
* @param Collection<int, SocialAccount> $disconnectedAccounts
*/
private function notifyOwner(Collection $disconnectedAccounts): void
{
$owner = $this->workspace->owner;
if (! $owner) {
return;
}
Log::info('Sending workspace disconnection notification', [
'workspace_id' => $this->workspace->id,
'disconnected_count' => $disconnectedAccounts->count(),
]);
$accountNames = $disconnectedAccounts
->map(fn ($account) => $account->platform->label().' (@'.($account->username ?? $account->display_name).')')
->implode(', ');
SendNotification::dispatch(
user: $owner,
workspaceId: $this->workspace->id,
type: Type::AccountDisconnected,
channel: Channel::Both,
title: $disconnectedAccounts->count().' '.($disconnectedAccounts->count() === 1 ? 'account' : 'accounts').' disconnected',
body: $accountNames,
data: ['workspace_id' => $this->workspace->id],
mailable: new WorkspaceConnectionsDisconnected($this->workspace, $disconnectedAccounts),
);
}
}