From bd12cf6dfb2f01297eb23b38bec735b8dc5caff5 Mon Sep 17 00:00:00 2001 From: Clement Desmidt Date: Fri, 20 Mar 2020 16:37:36 +0100 Subject: [PATCH] =?UTF-8?q?:sparkles:=20Ajoute=20la=20notification=20journ?= =?UTF-8?q?ali=C3=A8re?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Il manque l'intégration du mail Pour #13 --- .../Commands/SendDailyNotification.php | 66 +++++++++++++++ app/Console/Kernel.php | 17 ++-- app/Exceptions/WrongHourException.php | 10 +++ app/Jobs/SendReminderEmail.php | 4 +- app/Mail/DailyNotification.php | 38 +++++++++ app/Services/HourService.php | 82 +++++++++++++++++++ resources/views/emails/daily.blade.php | 1 + 7 files changed, 208 insertions(+), 10 deletions(-) create mode 100644 app/Console/Commands/SendDailyNotification.php create mode 100644 app/Exceptions/WrongHourException.php create mode 100644 app/Mail/DailyNotification.php create mode 100644 app/Services/HourService.php create mode 100644 resources/views/emails/daily.blade.php diff --git a/app/Console/Commands/SendDailyNotification.php b/app/Console/Commands/SendDailyNotification.php new file mode 100644 index 0000000..aa709db --- /dev/null +++ b/app/Console/Commands/SendDailyNotification.php @@ -0,0 +1,66 @@ +getCurrentNotificationId(); + $this->info(sprintf('Notification id = %s', $notification_id)); + } catch (WrongHourException $e) { + $this->error($e->getMessage()); + exit; + } + $users = User::where('notification_hour', '=', $notification_id) + ->where('email_verified_at', '!=', null) + ->whereNotExists(function($query) { + $query->select() + ->from('posts') + ->whereRaw('posts.user_id = users.id') + ->whereDate('date_post', date('Y-m-d')); + }) + ->get(); + foreach ($users as $user) { + Mail::to($user)->queue(new DailyNotification($user)); + } + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 4ec547c..84d6dfc 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -2,12 +2,11 @@ namespace App\Console; +use App\Console\Commands\SendDailyNotification; use App\Jobs\SendReminderEmail; -use App\Jobs\SendWelcomeEmail; use App\User; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; -use Illuminate\Support\Carbon; class Kernel extends ConsoleKernel { @@ -17,7 +16,7 @@ class Kernel extends ConsoleKernel * @var array */ protected $commands = [ - // + SendDailyNotification::class, ]; /** @@ -28,17 +27,17 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { + $schedule + ->command('send:daily') + ->cron('0 */6 * * *'); + $schedule->call(function() { - $users = User::where('email_verified_at', '!=', null) - ->where('notification_hour', '!=', null) + $users = User::where('email_verified_at', '=', null) ->get(); - var_dump(count($users)); foreach ($users as $user) { dispatch((new SendReminderEmail($user))); } - })->everyMinute(); - // $schedule->command('inspire') - // ->hourly(); + })->daily(); } /** diff --git a/app/Exceptions/WrongHourException.php b/app/Exceptions/WrongHourException.php new file mode 100644 index 0000000..c4095ad --- /dev/null +++ b/app/Exceptions/WrongHourException.php @@ -0,0 +1,10 @@ +user = $user; + } + + /** + * Build the message. + * + * @return $this + */ + public function build() + { + return $this->view('emails.daily'); + } +} diff --git a/app/Services/HourService.php b/app/Services/HourService.php new file mode 100644 index 0000000..5a5bef9 --- /dev/null +++ b/app/Services/HourService.php @@ -0,0 +1,82 @@ + 'slash', + self::MIDNIGHT_NOTIFICATION => 'moon', + self::MORNING_NOTIFICATION => 'sunrise', + self::NOON_NOTIFICATION => 'sun', + self::EVENING_NOTIFICATION => 'sunset', + ]; + + /** + * @param User $user + * @return array + */ + public function getHoursBracket(User $user) + { + switch ((int)$user->notification_hour) { + case self::MIDNIGHT_NOTIFICATION: + return ['2100-2359', '0000-0600']; + break; + case self::MORNING_NOTIFICATION: + return ['0601-1100']; + break; + case self::NOON_NOTIFICATION: + return ['1101-1530']; + break; + case self::EVENING_NOTIFICATION: + return ['1531-2059']; + break; + case self::NO_NOTIFICATION: + default: + return []; + } + } + + /** + * @param null|int|string $current_hour the hour and minute we want to find the id to. Formatted like `date('Hi')` + * @return int + * @throws WrongHourException + */ + public function getCurrentNotificationId($current_hour = null) + { + if (null === $current_hour) { + $current_hour = date('Hi'); + } + + $current_hour = (int) $current_hour; + if ( + ($current_hour >= 2100 && $current_hour < 2359) || + ($current_hour >= 0 && $current_hour < 600) + ) { + return self::MIDNIGHT_NOTIFICATION; + } + + if ($current_hour >= 600 && $current_hour < 1100) { + return self::MORNING_NOTIFICATION; + } + + if ($current_hour >= 1100 && $current_hour < 1530) { + return self::NOON_NOTIFICATION; + } + + if ($current_hour >= 1530 && $current_hour < 2100) { + return self::EVENING_NOTIFICATION; + } + + throw new WrongHourException(sprintf('Given Time « %s » is not compatible', $current_hour)); + } +} diff --git a/resources/views/emails/daily.blade.php b/resources/views/emails/daily.blade.php new file mode 100644 index 0000000..6974a3b --- /dev/null +++ b/resources/views/emails/daily.blade.php @@ -0,0 +1 @@ +Merci d'écrire !