nanosanteservice/app/Listeners/NotifyUser.php

74 lines
2.1 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Listeners;
use App\Events\ExampleEvent;
use App\Events\InsuranceEvent;
use App\Traits\Helper;
use GuzzleHttp\Client;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
2021-10-28 12:18:01 +00:00
use stdClass;
use Throwable;
class NotifyUser
{
use Helper;
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param InsuranceEvent $event
* @return void
*/
public function handle(InsuranceEvent $event)
{
//
$subscription = $event->subscription;
$user = $subscription->user;
try {
$client = new Client([
'base_uri' => config('services.notification_service.base_uri'),
]);
$headers = [
'Authorization' => config('services.notification_service.key'),
];
2021-10-28 12:18:01 +00:00
$body = new stdClass();
$body->title = $event->mailTitle;
$body->message = $event->mailMessage;
$body->email = $user->email;
$client->request('POST', '/send-mail', ['json' => $body, 'headers' => $headers]);
if (isset($event->notification)) {
2021-10-28 12:18:01 +00:00
$body = new stdClass();
$body->user_code = $user->user_code;
$body->message = $event->notification;
$body->date = $subscription->created_at->format('Y-m-d H:i:s') ?? date('Y-m-d H:i:s');
2021-10-29 15:52:10 +00:00
2021-10-28 12:18:01 +00:00
$data = new stdClass();
2021-10-28 12:09:41 +00:00
$data->screen = "notificationview";
2021-10-28 12:18:01 +00:00
$data->data = new stdClass();
2021-10-28 12:09:41 +00:00
$data->data->subscription_id = $data->data->id = $subscription->id;
2021-10-28 12:18:01 +00:00
$body->data = $data;
$client->request('POST', '/onesignal/pushToUser', ['json' => $body, 'headers' => $headers]);
}
} catch (Throwable $t) {
Log::error('-------- User notification not sent-----------');
Log::error($t->getMessage() . '\n' . $t->getTraceAsString());
}
}
}