From a86793079cdf84082f93e5b481f33ce32455bf42 Mon Sep 17 00:00:00 2001 From: Anne-Greeth van Herwijnen Date: Fri, 26 Apr 2019 11:50:15 +0200 Subject: [PATCH 1/6] Make maintenance also optional to notify subscribers --- .../Schedule/CreateScheduleCommand.php | 12 +++++++++++- .../Schedule/ScheduleWasCreatedEvent.php | 11 ++++++++++- .../Schedule/CreateScheduleCommandHandler.php | 4 ++-- .../SendScheduleEmailNotificationHandler.php | 3 +++ .../Dashboard/ScheduleController.php | 18 +++++++++++++++--- .../views/dashboard/maintenance/add.blade.php | 10 +++++++++- 6 files changed, 50 insertions(+), 8 deletions(-) diff --git a/app/Bus/Commands/Schedule/CreateScheduleCommand.php b/app/Bus/Commands/Schedule/CreateScheduleCommand.php index ebeec0d839f4..ffaee9d7bb94 100644 --- a/app/Bus/Commands/Schedule/CreateScheduleCommand.php +++ b/app/Bus/Commands/Schedule/CreateScheduleCommand.php @@ -60,6 +60,13 @@ final class CreateScheduleCommand */ public $components; + /** + * Whether to notify that the incident was reported. + * + * @var bool + */ + public $notify; + /** * The validation rules. * @@ -72,6 +79,7 @@ final class CreateScheduleCommand 'scheduled_at' => 'required|string', 'completed_at' => 'nullable|string', 'components' => 'nullable|array', + 'notify' => 'nullable|bool' ]; /** @@ -83,10 +91,11 @@ final class CreateScheduleCommand * @param string $scheduled_at * @param string $completed_at * @param array $components + * @param notify $notify * * @return void */ - public function __construct($name, $message, $status, $scheduled_at, $completed_at, array $components = []) + public function __construct($name, $message, $status, $scheduled_at, $completed_at, array $components = [], $notify) { $this->name = $name; $this->message = $message; @@ -94,5 +103,6 @@ public function __construct($name, $message, $status, $scheduled_at, $completed_ $this->scheduled_at = $scheduled_at; $this->completed_at = $completed_at; $this->components = $components; + $this->notify = $notify; } } diff --git a/app/Bus/Events/Schedule/ScheduleWasCreatedEvent.php b/app/Bus/Events/Schedule/ScheduleWasCreatedEvent.php index 1e1ecb4ee8e9..d16612c02531 100644 --- a/app/Bus/Events/Schedule/ScheduleWasCreatedEvent.php +++ b/app/Bus/Events/Schedule/ScheduleWasCreatedEvent.php @@ -36,18 +36,27 @@ final class ScheduleWasCreatedEvent implements ActionInterface, ScheduleEventInt */ public $schedule; + /** + * Whether to notify that the incident was reported. + * + * @var bool + */ + public $notify; + /** * Create a new schedule was created event instance. * * @param \CachetHQ\Cachet\Models\User $user * @param \CachetHQ\Cachet\Models\Schedule $schedule + * @param bool notify * * @return void */ - public function __construct(User $user, Schedule $schedule) + public function __construct(User $user, Schedule $schedule, $notify = false) { $this->user = $user; $this->schedule = $schedule; + $this->notify = $notify; } /** diff --git a/app/Bus/Handlers/Commands/Schedule/CreateScheduleCommandHandler.php b/app/Bus/Handlers/Commands/Schedule/CreateScheduleCommandHandler.php index 23de49338363..9069eadb9c01 100644 --- a/app/Bus/Handlers/Commands/Schedule/CreateScheduleCommandHandler.php +++ b/app/Bus/Handlers/Commands/Schedule/CreateScheduleCommandHandler.php @@ -66,8 +66,7 @@ public function handle(CreateScheduleCommand $command) { try { $schedule = Schedule::create($this->filter($command)); - - event(new ScheduleWasCreatedEvent($this->auth->user(), $schedule)); + event(new ScheduleWasCreatedEvent($this->auth->user(), $schedule, (bool) $command->notify)); } catch (InvalidArgumentException $e) { throw new ValidationException(new MessageBag([$e->getMessage()])); } @@ -96,6 +95,7 @@ protected function filter(CreateScheduleCommand $command) 'status' => $command->status, 'scheduled_at' => $scheduledAt, 'completed_at' => $completedAt, + 'notify' => $command->notify, ]; $availableParams = array_filter($params, function ($val) { diff --git a/app/Bus/Handlers/Events/Schedule/SendScheduleEmailNotificationHandler.php b/app/Bus/Handlers/Events/Schedule/SendScheduleEmailNotificationHandler.php index 11e6b377034d..84e9db31d616 100644 --- a/app/Bus/Handlers/Events/Schedule/SendScheduleEmailNotificationHandler.php +++ b/app/Bus/Handlers/Events/Schedule/SendScheduleEmailNotificationHandler.php @@ -51,6 +51,9 @@ public function __construct(Subscriber $subscriber) public function handle(ScheduleEventInterface $event) { $schedule = $event->schedule; + if (!$event->notify) { + return false; + } // First notify all global subscribers. $globalSubscribers = $this->subscriber->isVerified()->isGlobal()->get()->each(function ($subscriber) use ($schedule) { diff --git a/app/Http/Controllers/Dashboard/ScheduleController.php b/app/Http/Controllers/Dashboard/ScheduleController.php index ceb369bcd405..6f32768f2cc4 100644 --- a/app/Http/Controllers/Dashboard/ScheduleController.php +++ b/app/Http/Controllers/Dashboard/ScheduleController.php @@ -15,6 +15,7 @@ use CachetHQ\Cachet\Bus\Commands\Schedule\CreateScheduleCommand; use CachetHQ\Cachet\Bus\Commands\Schedule\DeleteScheduleCommand; use CachetHQ\Cachet\Bus\Commands\Schedule\UpdateScheduleCommand; +use CachetHQ\Cachet\Integrations\Contracts\System; use CachetHQ\Cachet\Models\IncidentTemplate; use CachetHQ\Cachet\Models\Schedule; use GrahamCampbell\Binput\Facades\Binput; @@ -35,13 +36,22 @@ class ScheduleController extends Controller */ protected $subMenu = []; + /** + * The system instance. + * + * @var \CachetHQ\Cachet\Integrations\Contracts\System + */ + protected $system; + + /** * Creates a new schedule controller instance. * * @return void */ - public function __construct() + public function __construct(System $system) { + $this->system = $system; View::share('subTitle', trans('dashboard.schedule.title')); } @@ -70,7 +80,8 @@ public function showAddSchedule() return View::make('dashboard.maintenance.add') ->withPageTitle(trans('dashboard.schedule.add.title').' - '.trans('dashboard.dashboard')) - ->withIncidentTemplates($incidentTemplates); + ->withIncidentTemplates($incidentTemplates) + ->withNotificationsEnabled($this->system->canNotifySubscribers()); } /** @@ -87,7 +98,8 @@ public function addScheduleAction() Binput::get('status', Schedule::UPCOMING), Binput::get('scheduled_at'), Binput::get('completed_at'), - Binput::get('components', []) + Binput::get('components', []), + Binput::get('notify', false) )); } catch (ValidationException $e) { return cachet_redirect('dashboard.schedule.create') diff --git a/resources/views/dashboard/maintenance/add.blade.php b/resources/views/dashboard/maintenance/add.blade.php index 99a64bba5c15..07c5d6fa7eac 100644 --- a/resources/views/dashboard/maintenance/add.blade.php +++ b/resources/views/dashboard/maintenance/add.blade.php @@ -57,7 +57,15 @@ - + @if($notificationsEnabled) + +
+ +
+ @endif
From b5816a3340ec5d057fe89c6f4e9789a8f75ed229 Mon Sep 17 00:00:00 2001 From: Anne-Greeth van Herwijnen Date: Fri, 26 Apr 2019 13:12:30 +0200 Subject: [PATCH 2/6] Fix StyleCI --- app/Bus/Commands/Schedule/CreateScheduleCommand.php | 4 ++-- app/Bus/Events/Schedule/ScheduleWasCreatedEvent.php | 4 ++-- app/Http/Controllers/Dashboard/ScheduleController.php | 1 - 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/app/Bus/Commands/Schedule/CreateScheduleCommand.php b/app/Bus/Commands/Schedule/CreateScheduleCommand.php index ffaee9d7bb94..db65f737f568 100644 --- a/app/Bus/Commands/Schedule/CreateScheduleCommand.php +++ b/app/Bus/Commands/Schedule/CreateScheduleCommand.php @@ -79,7 +79,7 @@ final class CreateScheduleCommand 'scheduled_at' => 'required|string', 'completed_at' => 'nullable|string', 'components' => 'nullable|array', - 'notify' => 'nullable|bool' + 'notify' => 'nullable|bool', ]; /** @@ -95,7 +95,7 @@ final class CreateScheduleCommand * * @return void */ - public function __construct($name, $message, $status, $scheduled_at, $completed_at, array $components = [], $notify) + public function __construct($name, $message, $status, $scheduled_at, $completed_at, $components, $notify) { $this->name = $name; $this->message = $message; diff --git a/app/Bus/Events/Schedule/ScheduleWasCreatedEvent.php b/app/Bus/Events/Schedule/ScheduleWasCreatedEvent.php index d16612c02531..2438658d71be 100644 --- a/app/Bus/Events/Schedule/ScheduleWasCreatedEvent.php +++ b/app/Bus/Events/Schedule/ScheduleWasCreatedEvent.php @@ -36,7 +36,7 @@ final class ScheduleWasCreatedEvent implements ActionInterface, ScheduleEventInt */ public $schedule; - /** + /** * Whether to notify that the incident was reported. * * @var bool @@ -52,7 +52,7 @@ final class ScheduleWasCreatedEvent implements ActionInterface, ScheduleEventInt * * @return void */ - public function __construct(User $user, Schedule $schedule, $notify = false) + public function __construct(User $user, Schedule $schedule, $notify = false) { $this->user = $user; $this->schedule = $schedule; diff --git a/app/Http/Controllers/Dashboard/ScheduleController.php b/app/Http/Controllers/Dashboard/ScheduleController.php index 6f32768f2cc4..c06216a916f6 100644 --- a/app/Http/Controllers/Dashboard/ScheduleController.php +++ b/app/Http/Controllers/Dashboard/ScheduleController.php @@ -43,7 +43,6 @@ class ScheduleController extends Controller */ protected $system; - /** * Creates a new schedule controller instance. * From 1bbe3dcefaf979cdd615e5ac783c6c96c0c2e870 Mon Sep 17 00:00:00 2001 From: Anne-Greeth van Herwijnen Date: Fri, 26 Apr 2019 13:25:39 +0200 Subject: [PATCH 3/6] Fix tests --- tests/Bus/Commands/Schedule/CreateScheduleCommandTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Bus/Commands/Schedule/CreateScheduleCommandTest.php b/tests/Bus/Commands/Schedule/CreateScheduleCommandTest.php index 3c3896fc6948..3377bef9e01f 100644 --- a/tests/Bus/Commands/Schedule/CreateScheduleCommandTest.php +++ b/tests/Bus/Commands/Schedule/CreateScheduleCommandTest.php @@ -34,6 +34,7 @@ protected function getObjectAndParams() 'scheduled_at' => date('Y-m-d H:i'), 'completed_at' => date('Y-m-d H:i'), 'components' => [], + 'notify' => 1, ]; $object = new CreateScheduleCommand( $params['name'], @@ -41,7 +42,8 @@ protected function getObjectAndParams() $params['status'], $params['scheduled_at'], $params['completed_at'], - $params['components'] + $params['components'], + $params['notify'] ); return compact('params', 'object'); From 9f42d55bb689eaad3c19d912dd50dda123a033cd Mon Sep 17 00:00:00 2001 From: Anne-Greeth van Herwijnen Date: Fri, 26 Apr 2019 14:17:55 +0200 Subject: [PATCH 4/6] Oopsie in the docs --- app/Bus/Commands/Schedule/CreateScheduleCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Bus/Commands/Schedule/CreateScheduleCommand.php b/app/Bus/Commands/Schedule/CreateScheduleCommand.php index db65f737f568..c7adc096d686 100644 --- a/app/Bus/Commands/Schedule/CreateScheduleCommand.php +++ b/app/Bus/Commands/Schedule/CreateScheduleCommand.php @@ -91,7 +91,7 @@ final class CreateScheduleCommand * @param string $scheduled_at * @param string $completed_at * @param array $components - * @param notify $notify + * @param bool $notify * * @return void */ From 57dd008064a3f373c136c8c22ac8bacc4ecaeeed Mon Sep 17 00:00:00 2001 From: Anne-Greeth van Herwijnen Date: Fri, 26 Apr 2019 14:26:50 +0200 Subject: [PATCH 5/6] Adds notify to test --- app/Bus/Commands/Schedule/CreateScheduleCommand.php | 2 +- tests/Api/ScheduleTest.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Bus/Commands/Schedule/CreateScheduleCommand.php b/app/Bus/Commands/Schedule/CreateScheduleCommand.php index c7adc096d686..d40d68df1489 100644 --- a/app/Bus/Commands/Schedule/CreateScheduleCommand.php +++ b/app/Bus/Commands/Schedule/CreateScheduleCommand.php @@ -91,7 +91,7 @@ final class CreateScheduleCommand * @param string $scheduled_at * @param string $completed_at * @param array $components - * @param bool $notify + * @param bool $notify * * @return void */ diff --git a/tests/Api/ScheduleTest.php b/tests/Api/ScheduleTest.php index 99b67b6458a0..3c114c055f3b 100644 --- a/tests/Api/ScheduleTest.php +++ b/tests/Api/ScheduleTest.php @@ -52,6 +52,7 @@ public function test_can_create_schedule() 'message' => 'Foo bar, baz.', 'status' => 1, 'scheduled_at' => date('Y-m-d H:i'), + 'notify' => 1, ]; $response = $this->json('POST', '/api/v1/schedules/', $schedule); From 29e0cf95281ba205b6503458322d21de85638968 Mon Sep 17 00:00:00 2001 From: Anne-Greeth van Herwijnen Date: Fri, 26 Apr 2019 14:41:25 +0200 Subject: [PATCH 6/6] Forgot to add notify to CreatScheduleCommand in API --- app/Http/Controllers/Api/ScheduleController.php | 3 ++- tests/Api/ScheduleTest.php | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Api/ScheduleController.php b/app/Http/Controllers/Api/ScheduleController.php index d12eabc72acd..c59f46dbd62c 100644 --- a/app/Http/Controllers/Api/ScheduleController.php +++ b/app/Http/Controllers/Api/ScheduleController.php @@ -73,7 +73,8 @@ public function store() Binput::get('status'), Binput::get('scheduled_at'), Binput::get('completed_at'), - Binput::get('components', []) + Binput::get('components', []), + Binput::get('notify', false) )); } catch (QueryException $e) { throw new BadRequestHttpException(); diff --git a/tests/Api/ScheduleTest.php b/tests/Api/ScheduleTest.php index 3c114c055f3b..99b67b6458a0 100644 --- a/tests/Api/ScheduleTest.php +++ b/tests/Api/ScheduleTest.php @@ -52,7 +52,6 @@ public function test_can_create_schedule() 'message' => 'Foo bar, baz.', 'status' => 1, 'scheduled_at' => date('Y-m-d H:i'), - 'notify' => 1, ]; $response = $this->json('POST', '/api/v1/schedules/', $schedule);