Files
kulakpos_web/Modules/MarketingAddon/App/Jobs/SendNotificationJob.php
eko54r 05fd3230b8
All checks were successful
Build, Push and Deploy / build-and-push (push) Successful in 5m14s
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 17s
Build, Push and Deploy / deploy-staging (push) Successful in 41s
Build, Push and Deploy / deploy-production (push) Has been skipped
update marketing
2026-05-14 11:55:22 +07:00

122 lines
3.7 KiB
PHP

<?php
namespace Modules\MarketingAddon\App\Jobs;
use Throwable;
use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Config;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Modules\MarketingAddon\App\Emails\DynamicMail;
use Modules\MarketingAddon\App\Models\EmailSetting;
use Modules\MarketingAddon\App\Services\SmsService;
use Modules\MarketingAddon\App\Models\MessageReport;
use Modules\MarketingAddon\App\Services\WhatsAppService;
class SendNotificationJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(
protected string $channel,
protected object $template,
protected array $data,
protected int $notificationId,
protected int $businessId,
) {}
public function handle(
SmsService $sms,
WhatsAppService $whatsapp
): void {
try {
match ($this->channel) {
/** ===================== SMS ===================== */
'sms' => $sms->send(
phone: $this->data['phone'],
template: $this->template,
data: $this->data
),
/** ===================== WHATSAPP ===================== */
'whatsapp' => $whatsapp->send(
phone: $this->data['phone'],
template: $this->template,
businessId: $this->businessId,
name: $this->data['name'] ?? '',
data: $this->data
),
/** ===================== EMAIL ===================== */
'email' => $this->sendEmail(),
default => throw new \Exception('Invalid notification channel'),
};
MessageReport::where('id', $this->notificationId)->update([
'status' => 'success',
'send_at' => now(),
]);
} catch (Throwable $e) {
MessageReport::where('id', $this->notificationId)->update([
'status' => 'failed',
'send_at' => now(),
]);
throw $e;
}
}
/**
* Handle email sending with dynamic SMTP
*/
protected function sendEmail(): void
{
$smtp = EmailSetting::where('business_id', $this->businessId)->first();
if ($smtp) {
Config::set('mail.mailers.smtp.transport', $smtp->driver);
Config::set('mail.mailers.smtp.host', $smtp->host);
Config::set('mail.mailers.smtp.port', $smtp->port);
Config::set('mail.mailers.smtp.username', $smtp->username);
Config::set('mail.mailers.smtp.password', $smtp->password);
Config::set('mail.mailers.smtp.encryption', $smtp->encryption);
Config::set('mail.from.address', $smtp->from_address);
Config::set('mail.from.name', $smtp->from_name);
// IMPORTANT: reset mailer to avoid queue worker cache issues
Mail::forgetMailers();
Mail::to($this->data['email'])
->send(new DynamicMail($this->template, $this->data));
} else {
MessageReport::where('id', $this->notificationId)->update([
'status' => 'failed',
'send_at' => now(),
]);
}
}
/**
* Called automatically by Laravel after final failure
*/
public function failed(): void
{
MessageReport::where('id', $this->notificationId)->update([
'status' => 'failed',
'send_at' => now(),
]);
}
}