trypost/app/Jobs/SendNotification.php
Paulo Castellano 74c6442728 refactor: code review fixes — policies, enums, data_get, tests
- Refactor WorkspacePolicy to use pivot role instead of workspace.user_id
- Add manageBilling policy (owner only) to BillingController
- Fix ApiKeyController authorization (view → manageTeam for store/destroy)
- Fix WorkspaceInviteController using workspace.user_id for owner checks
- Fix WorkspaceController settings is_owner using workspace.user_id
- Create PostAction enum for UpdatePost/PostController action strings
- Create ApiToken\Status enum
- Add User::SUBSCRIPTION_NAME constant, replace all hardcoded 'default'
- Convert wantsEmailFor to accept NotificationType enum
- Convert all $data[] to data_get() across publishers, controllers, jobs
- Fix SocialLoginController callback missing try/catch
- Fix SocialController::toggleActive missing workspace null check
- Fix UpdatePost NPE on meta merge when postPlatform not found
- Remove HTML5 required attributes from form inputs
- Convert function declarations to arrow functions in Vue components
- Replace hardcoded URLs with Wayfinder route helpers
- Replace new Date() with dayjs
- Add 16 new test files covering policies, authorization, publishing
2026-03-31 00:40:18 -03:00

68 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Jobs;
use App\Enums\Notification\Channel;
use App\Enums\Notification\Type;
use App\Models\Notification;
use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
class SendNotification implements ShouldQueue
{
use Queueable;
public int $tries = 3;
public int $backoff = 10;
/**
* @param array<string, mixed>|null $data
*/
public function __construct(
public User $user,
public string $workspaceId,
public Type $type,
public Channel $channel,
public string $title,
public string $body,
public ?array $data = null,
public ?Mailable $mailable = null,
) {}
public function handle(): void
{
// Save in-app notification
if ($this->channel !== Channel::Email) {
Notification::create([
'user_id' => $this->user->id,
'workspace_id' => $this->workspaceId,
'type' => $this->type,
'channel' => $this->channel,
'title' => $this->title,
'body' => $this->body,
'data' => $this->data,
]);
}
// Send email (respects user preferences)
if ($this->mailable && $this->channel !== Channel::InApp && $this->user->wantsEmailFor($this->type)) {
Mail::to($this->user)->send($this->mailable);
}
}
public function failed(\Throwable $exception): void
{
Log::error('SendNotification job failed', [
'user_id' => $this->user->id,
'type' => $this->type->value,
'error' => $exception->getMessage(),
]);
}
}