Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make maintenance also optional to notify subscribers #3567

Merged
merged 6 commits into from
Jun 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/Bus/Commands/Schedule/CreateScheduleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ final class CreateScheduleCommand
*/
public $components;

/**
* Whether to notify that the incident was reported.
*
* @var bool
*/
public $notify;

/**
* The validation rules.
*
Expand All @@ -72,6 +79,7 @@ final class CreateScheduleCommand
'scheduled_at' => 'required|string',
'completed_at' => 'nullable|string',
'components' => 'nullable|array',
'notify' => 'nullable|bool',
];

/**
Expand All @@ -83,16 +91,18 @@ final class CreateScheduleCommand
* @param string $scheduled_at
* @param string $completed_at
* @param array $components
* @param bool $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, $components, $notify)
{
$this->name = $name;
$this->message = $message;
$this->status = $status;
$this->scheduled_at = $scheduled_at;
$this->completed_at = $completed_at;
$this->components = $components;
$this->notify = $notify;
}
}
11 changes: 10 additions & 1 deletion app/Bus/Events/Schedule/ScheduleWasCreatedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()]));
}
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/Api/ScheduleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
17 changes: 14 additions & 3 deletions app/Http/Controllers/Dashboard/ScheduleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,13 +36,21 @@ 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'));
}

Expand Down Expand Up @@ -70,7 +79,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());
}

/**
Expand All @@ -87,7 +97,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')
Expand Down
10 changes: 9 additions & 1 deletion resources/views/dashboard/maintenance/add.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@
<input type="text" name="completed_at" class="form-control flatpickr-time" data-date-format="Y-m-d H:i" placeholder="{{ trans('forms.schedules.completed_at') }}">
</div>
</fieldset>

@if($notificationsEnabled)
<input type="hidden" name="notify" value="0">
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's a label missing for this input. That makes it hard for people using screen readers or voice control to use the input. If you don't want a visual label, try an aria-label attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how to fix this because this is the hidden input that should not have a label :(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be a switch then?

<div class="checkbox">
<label>
<input type="checkbox" name="notify" value="1" checked="{{ Binput::old('notify', 'checked') }}">
{{ trans('forms.incidents.notify_subscribers') }}
</label>
</div>
@endif
<div class="form-group">
<div class="btn-group">
<button type="submit" class="btn btn-success">{{ trans('forms.add') }}</button>
Expand Down
4 changes: 3 additions & 1 deletion tests/Bus/Commands/Schedule/CreateScheduleCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ 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'],
$params['message'],
$params['status'],
$params['scheduled_at'],
$params['completed_at'],
$params['components']
$params['components'],
$params['notify']
);

return compact('params', 'object');
Expand Down