65 lines
1.9 KiB
PHP
65 lines
1.9 KiB
PHP
|
|
<?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;
|
||
|
|
}
|
||
|
|
}
|