Files
nuhodds_fi4/app/Console/Commands/Services/Tasks/NotifyAlarms.php
T
2026-02-06 16:26:17 +01:00

140 lines
6.8 KiB
PHP

<?php
namespace App\Console\Commands\Services\Tasks;
use App\Console\Commands\Nuh;
use App\Console\Commands\Services\Tasks\Notification\CheckSend;
use App\Console\Commands\Services\Tasks\Notification\LoadAlarmEvents;
use App\Console\Commands\Services\Tasks\Notification\StoreSend;
use App\Mail\AlarmNotification;
use App\Models\Bookmaker;
use App\Models\Event;
use App\Models\OddsName;
use App\Models\PersonalNotificationSend;
use App\Models\User;
use App\Utilities\Notice;
use Filament\Notifications\Notification;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
class NotifyAlarms
{
public static function run()
{
$debug = Nuh::$options['d'] ?? 0;
if ($debug) {
Notice::note('Notify Alarms', 'b');
}
$stored_alarms = [];
$alarms = LoadAlarmEvents::load();
foreach ($alarms as $event_id => $alarm) {
$event = Event::with('competition_group')->with('home')->with('away')->find($event_id);
foreach ($alarm as $user_id => $alarm_user) {
$user = User::find($user_id);
foreach ($alarm_user as $bookmaker_id => $alarm_user_bookmaker) {
$bookmaker = Bookmaker::find($bookmaker_id);
foreach ($alarm_user_bookmaker as $mode => $alarm_user_bookmaker_mode) {
foreach ($alarm_user_bookmaker_mode as $fieldname => $alarm_level_id) {
$bet = OddsName::where('alarm', $fieldname)->select('table', 'bettype', 'name')->first()->toArray();
$quote = DB::table($bet['table'])->where('event_id', $event_id)->where('bookmaker_id', $bookmaker_id)->value($bet['name']);
switch ($mode) {
case 'mail':
$send = CheckSend::check($event_id, $user_id, $bookmaker_id, $alarm_level_id, $fieldname, 'notify_by_email');
if (! $send) {
$send = StoreSend::store($event_id, $user_id, $bookmaker_id, $alarm_level_id, $fieldname, 'notify_by_mail');
switch ($alarm_level_id) {
case 1:
$message = 'ALARM LEVEL '.$alarm_level_id;
break;
case 2:
$message = 'ALARM LEVEL '.$alarm_level_id;
break;
default:
$message = 'ALARM LEVEL '.$alarm_level_id;
break;
}
$tablecontent = [
['key' => 'League', 'value' => $event->competition_group->name],
['key' => 'Event', 'value' => $event->home->name.' vs '.$event->away->name],
['key' => 'Startime', 'value' => $event->starttime],
['key' => 'Bookmaker', 'value' => $bookmaker->name],
['key' => 'Bettype', 'value' => $bet['bettype']],
['key' => 'Odd Name', 'value' => strtoupper($bet['name'])],
['key' => 'Value', 'value' => $quote],
['key' => 'Id', 'value' => $event_id],
];
Mail::to($user)
->send(new AlarmNotification($message, $tablecontent, $alarm_level_id));
}
$stored_alarms[] = $send;
break;
case 'system':
$send = CheckSend::check($event_id, $user_id, $bookmaker_id, $alarm_level_id, $fieldname, 'notify_by_system');
if (! $send) {
$send = StoreSend::store($event_id, $user_id, $bookmaker_id, $alarm_level_id, $fieldname, 'notify_by_system');
switch ($alarm_level_id) {
case 1:
$title = '<div class="text-green-500">ALARM LEVEL '.$alarm_level_id.'</div>';
break;
case 2:
$title = '<div class="text-blue-500">ALARM LEVEL '.$alarm_level_id.'</div>';
break;
default:
$title = '<div class="text-red-500">ALARM LEVEL '.$alarm_level_id.'</div>';
break;
}
$message = '<a href="/events?search='.$event_id.'"><div class="grid grid-cols-2 gap-1">';
$message .= '<div>League</div><div>'.$event->competition_group->name.'</div>';
$message .= '<div>Event</div><div>'.$event->home->name.' vs '.$event->away->name.'</div>';
$message .= '<div>Startime</div><div>'.$event->starttime.'</div>';
$message .= '<div>Bookmaker</div><div>'.$bookmaker->name.'</div>';
$message .= '<div>Bettype</div><div>'.$bet['bettype'].'</div>';
$message .= '<div>Odd Name</div><div>'.$bet['name'].'</div>';
$message .= '<div>Value</div><div>'.$quote.'</div></div></a>';
Notification::make()
->title($title)
->icon('phosphor-envelope-duotone')
->body($message)
->sendToDatabase($user);
break;
}
$stored_alarms[] = $send;
}
}
}
}
}
}
if ($stored_alarms) {
PersonalNotificationSend::whereNotIn('id', $stored_alarms)->delete();
}
}
}