+ Optimize the sending of mail and notification

This commit is contained in:
Djery-Tom 2020-12-06 17:55:00 +01:00
parent 30d4acfb74
commit 6210645b5d
3 changed files with 58 additions and 39 deletions

View File

@ -31,6 +31,7 @@ use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Mail;
use PDO; use PDO;
@ -38,17 +39,23 @@ trait Helper
{ {
public function sendMail($email, $title, $messageText) public function sendMail($email, $title, $messageText)
{ {
try{
$recipients = [preg_replace("/\s+/", "", $email)]; $recipients = [preg_replace("/\s+/", "", $email)]; // Supprimer les espaces dans le mail
Mail::mailer('smtp')->raw($messageText, function ($message) use ($recipients, $title) { Mail::mailer('smtp')->raw($messageText, function ($message) use ($recipients, $title) {
$message->subject($title); $message->subject($title);
$message->to($recipients); $message->to($recipients);
}); });
}catch (\Throwable $t){
Log::error('-------- Mail not sent -----------');
Log::error($t->getMessage());
}
// return $this->successResponse("mail envoye"); // return $this->successResponse("mail envoye");
} }
public function sendPushNotificationToUser($user_code, $message, $data = null, $date = null) public function sendPushNotificationToUser($user_code, $message, $data = null, $date = null)
{ {
try{
$client = new \GuzzleHttp\Client([ $client = new \GuzzleHttp\Client([
'base_uri' => env('NOTIFICATION_SERVICE_URL'), 'base_uri' => env('NOTIFICATION_SERVICE_URL'),
]); ]);
@ -60,8 +67,9 @@ trait Helper
$body->message = $message; $body->message = $message;
$body->data = $data; $body->data = $data;
try { try {
$body->date = $date->format('Y-m-d H:i:s'); $body->date = ($date instanceof \DateTime) ? $date->format('Y-m-d H:i:s') : $date;
}catch (\Exception $e){ } catch (\Throwable $t) {
Log::error($t->getMessage());
$body->date = $date; $body->date = $date;
} }
$promise = $client->requestAsync('POST', '/onesignal/pushToUser', ['json' => $body, 'headers' => $headers])->then(); $promise = $client->requestAsync('POST', '/onesignal/pushToUser', ['json' => $body, 'headers' => $headers])->then();
@ -75,10 +83,16 @@ trait Helper
// ); // );
$promise->wait(); $promise->wait();
// return $response->getBody()->getContents(); // 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) public function sendPushNotificationToAgent($agent_code, $message, $data = null, $date = null)
{ {
try{
$client = new \GuzzleHttp\Client([ $client = new \GuzzleHttp\Client([
'base_uri' => env('NOTIFICATION_SERVICE_URL'), 'base_uri' => env('NOTIFICATION_SERVICE_URL'),
]); ]);
@ -90,12 +104,17 @@ trait Helper
$body->message = $message; $body->message = $message;
$body->data = $data; $body->data = $data;
try { try {
$body->date = $date->format('Y-m-d H:i:s'); $body->date = ($date instanceof \DateTime) ? $date->format('Y-m-d H:i:s') : $date;
}catch (\Exception $e){ } catch (\Throwable $t) {
Log::error($t->getMessage());
$body->date = $date; $body->date = $date;
} }
$promise = $client->requestAsync('POST', '/onesignal/pushToAgent', ['json' => $body, 'headers' => $headers])->then(); $promise = $client->requestAsync('POST', '/onesignal/pushToAgent', ['json' => $body, 'headers' => $headers])->then();
$promise->wait(); $promise->wait();
}catch (\Throwable $t){
Log::error('-------- Agent notification not sent-----------');
Log::error($t->getMessage());
}
} }
public function checkPassword($password, $encrypted_password, $salt) public function checkPassword($password, $encrypted_password, $salt)

View File

@ -45,9 +45,9 @@ return [
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
'middleware' => [ 'middleware' => [
'api' => ['docs'], 'api' => [],
'asset' => [], 'asset' => [],
'docs' => ['docs'], 'docs' => [],
'oauth2_callback' => [], 'oauth2_callback' => [],
], ],
], ],