nanosanteservice/app/Listeners/NotifyUser.php

72 lines
2.0 KiB
PHP
Raw 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;
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'),
];
$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)) {
$body = new \stdClass();
$body->user_code = $user->user_code;
$body->message = $event->notification;
$body->data = [];
try {
$body->date = $this->getCurrentTimeByCountryCode($user->network->country->code_country);
} catch (Throwable $t) {
Log::error($t->getMessage());
$body->date = date('Y-m-d H:i:s');
}
$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());
}
}
}