update marketing
All checks were successful
All checks were successful
This commit is contained in:
64
Modules/MarketingAddon/App/Services/NotifyService.php
Normal file
64
Modules/MarketingAddon/App/Services/NotifyService.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\MarketingAddon\App\Services;
|
||||
|
||||
use Modules\MarketingAddon\App\Models\Template;
|
||||
use Modules\MarketingAddon\App\Models\MessageReport;
|
||||
use Modules\MarketingAddon\App\Jobs\SendNotificationJob;
|
||||
|
||||
class NotifyService
|
||||
{
|
||||
public function send(string $event, array $data, string $businessId)
|
||||
{
|
||||
$settings = get_option('notification_settings_' . $businessId);
|
||||
|
||||
if (!isset($settings[$event])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$array = [];
|
||||
|
||||
foreach (['sms', 'whatsapp', 'email'] as $channel) {
|
||||
|
||||
// Check enabled in settings
|
||||
if (empty($settings[$event][$channel]['enabled'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check required data per channel
|
||||
if (in_array($channel, ['sms', 'whatsapp']) && empty($data['phone'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($channel === 'email' && empty($data['email'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$array[] = $channel;
|
||||
$template = Template::where('business_id', $businessId)->where('title', $event)->where('type', $channel)->first();
|
||||
|
||||
if (empty($template)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$notification = MessageReport::create([
|
||||
'title' => $event,
|
||||
'channel' => $channel,
|
||||
'business_id' => $businessId,
|
||||
'template_id' => $template->id,
|
||||
'message' => $template->message,
|
||||
]);
|
||||
|
||||
// Dispatch job
|
||||
SendNotificationJob::dispatch(
|
||||
data: $data,
|
||||
channel: $channel,
|
||||
template: $template,
|
||||
businessId: $businessId,
|
||||
notificationId: $notification->id,
|
||||
)->onQueue('high');
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user