60 lines
1.8 KiB
PHP
60 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Jobs\Mail\Admin;
|
|
|
|
use App\Mail\Admin\AdminOrderNotification;
|
|
use App\Models\Setting;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
class AdminOrderNotificationJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public int $tries = 3;
|
|
public int $timeout = 60;
|
|
|
|
public function __construct(public readonly array $data) {}
|
|
|
|
public function handle(): void
|
|
{
|
|
$setting = Setting::where('key', 'setting')->first();
|
|
$settingValue = $setting ? json_decode((string) $setting->value, true) : [];
|
|
|
|
$adminEmail = $settingValue['admin_email'] ?? null;
|
|
|
|
if (empty($adminEmail)) {
|
|
Log::warning('AdminOrderNotificationJob: admin_email not configured in settings.');
|
|
return;
|
|
}
|
|
|
|
$payload = array_merge($this->data, [
|
|
'company_name' => $settingValue['company'] ?? 'Portal Admin',
|
|
]);
|
|
|
|
try {
|
|
Mail::to($adminEmail)->send(new AdminOrderNotification($payload));
|
|
} catch (\Throwable $e) {
|
|
Log::error('AdminOrderNotificationJob: failed to send email. ' . $e->getMessage(), [
|
|
'admin_email' => $adminEmail,
|
|
'order_num' => $this->data['order_num'] ?? null,
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function failed(\Throwable $e): void
|
|
{
|
|
Log::error('AdminOrderNotificationJob: job failed.', [
|
|
'error' => $e->getMessage(),
|
|
'order_num' => $this->data['order_num'] ?? null,
|
|
]);
|
|
}
|
|
}
|