update marketing
All checks were successful
All checks were successful
This commit is contained in:
121
Modules/MarketingAddon/App/Jobs/SendNotificationJob.php
Normal file
121
Modules/MarketingAddon/App/Jobs/SendNotificationJob.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?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(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user