From ee30c51075c8a526299334c3537da38f39315b15 Mon Sep 17 00:00:00 2001 From: Djery-Tom Date: Sun, 6 Dec 2020 17:54:17 +0100 Subject: [PATCH] + Optimize the sending of mail and notification --- app/Traits/Helper.php | 90 ++++++++++++++++++++++++----------------- config/swagger-lume.php | 6 ++- 2 files changed, 57 insertions(+), 39 deletions(-) diff --git a/app/Traits/Helper.php b/app/Traits/Helper.php index 1a50a86..0805280 100644 --- a/app/Traits/Helper.php +++ b/app/Traits/Helper.php @@ -39,34 +39,40 @@ trait Helper { public function sendMail($email, $title, $messageText) { + try { + $recipients = [preg_replace("/\s+/", "", $email)]; // Supprimer les espaces dans le mail + Mail::mailer('smtp')->raw($messageText, function ($message) use ($recipients, $title) { + $message->subject($title); + $message->to($recipients); + }); + } catch (\Throwable $t) { + Log::error('-------- Mail not sent -----------'); + Log::error($t->getMessage()); + } - $recipients = [preg_replace("/\s+/", "", $email)]; // Supprimer les espaces dans le mail - Mail::mailer('smtp')->raw($messageText, function ($message) use ($recipients, $title) { - $message->subject($title); - $message->to($recipients); - }); // return $this->successResponse("mail envoye"); } public function sendPushNotificationToUser($user_code, $message, $data = null, $date = null) { - $client = new \GuzzleHttp\Client([ - 'base_uri' => env('NOTIFICATION_SERVICE_URL'), - ]); - $headers = [ - 'Authorization' => env('NOTIFICATION_SERVICE_KEY'), - ]; - $body = new \stdClass(); - $body->user_code = $user_code; - $body->message = $message; - $body->data = $data; try { - $body->date = ($date instanceof \DateTime) ? $date->format('Y-m-d H:i:s') : $date; - } catch (\Throwable $t) { - Log::error($t->getTraceAsString()); - $body->date = $date; - } - $promise = $client->requestAsync('POST', '/onesignal/pushToUser', ['json' => $body, 'headers' => $headers])->then(); + $client = new \GuzzleHttp\Client([ + 'base_uri' => env('NOTIFICATION_SERVICE_URL'), + ]); + $headers = [ + 'Authorization' => env('NOTIFICATION_SERVICE_KEY'), + ]; + $body = new \stdClass(); + $body->user_code = $user_code; + $body->message = $message; + $body->data = $data; + try { + $body->date = ($date instanceof \DateTime) ? $date->format('Y-m-d H:i:s') : $date; + } catch (\Throwable $t) { + Log::error($t->getMessage()); + $body->date = $date; + } + $promise = $client->requestAsync('POST', '/onesignal/pushToUser', ['json' => $body, 'headers' => $headers])->then(); // function (ResponseInterface $res) { // echo $res->getStatusCode() . "\n"; // }, @@ -75,30 +81,40 @@ trait Helper // echo $e->getRequest()->getMethod(); // } // ); - $promise->wait(); + $promise->wait(); // return $response->getBody()->getContents(); + } catch (\Throwable $t) { + Log::error('-------- User notification not sent-----------'); + Log::error($t->getMessage()); + } + } public function sendPushNotificationToAgent($agent_code, $message, $data = null, $date = null) { - $client = new \GuzzleHttp\Client([ - 'base_uri' => env('NOTIFICATION_SERVICE_URL'), - ]); - $headers = [ - 'Authorization' => env('NOTIFICATION_SERVICE_KEY'), - ]; - $body = new \stdClass(); - $body->agent_code = $agent_code; - $body->message = $message; - $body->data = $data; try { - $body->date = ($date instanceof \DateTime) ? $date->format('Y-m-d H:i:s') : $date; + $client = new \GuzzleHttp\Client([ + 'base_uri' => env('NOTIFICATION_SERVICE_URL'), + ]); + $headers = [ + 'Authorization' => env('NOTIFICATION_SERVICE_KEY'), + ]; + $body = new \stdClass(); + $body->agent_code = $agent_code; + $body->message = $message; + $body->data = $data; + try { + $body->date = ($date instanceof \DateTime) ? $date->format('Y-m-d H:i:s') : $date; + } catch (\Throwable $t) { + Log::error($t->getMessage()); + $body->date = $date; + } + $promise = $client->requestAsync('POST', '/onesignal/pushToAgent', ['json' => $body, 'headers' => $headers])->then(); + $promise->wait(); } catch (\Throwable $t) { - Log::error($t->getTraceAsString()); - $body->date = $date; + Log::error('-------- Agent notification not sent-----------'); + Log::error($t->getMessage()); } - $promise = $client->requestAsync('POST', '/onesignal/pushToAgent', ['json' => $body, 'headers' => $headers])->then(); - $promise->wait(); } public function checkPassword($password, $encrypted_password, $salt) diff --git a/config/swagger-lume.php b/config/swagger-lume.php index 3c28a02..60f4c25 100644 --- a/config/swagger-lume.php +++ b/config/swagger-lume.php @@ -45,9 +45,11 @@ return [ |-------------------------------------------------------------------------- */ 'middleware' => [ - 'api' => ['docs'], +// 'api' => ['docs'], + 'api' => [], 'asset' => [], - 'docs' => ['docs'], +// 'docs' => ['docs'], + 'docs' => [], 'oauth2_callback' => [], ], ],