Skip to content

Commit

Permalink
Merge pull request #3580 from CachetHQ/schedule-fixes
Browse files Browse the repository at this point in the history
Fix issues with scheduled maintenance
  • Loading branch information
jbrooksuk committed May 9, 2019
2 parents 7479b99 + 182ef26 commit ef51636
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/Composers/ScheduledComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ScheduledComposer
*/
public function compose(View $view)
{
$scheduledMaintenance = Schedule::futureSchedules()->orderBy('scheduled_at')->get();
$scheduledMaintenance = Schedule::uncompleted()->orderBy('scheduled_at')->get();

$view->withScheduledMaintenance($scheduledMaintenance);
}
Expand Down
41 changes: 31 additions & 10 deletions app/Models/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
use Illuminate\Database\Eloquent\SoftDeletes;
use McCool\LaravelAutoPresenter\HasPresenter;

/**
* This is the schedule class.
*
* @author James Brooks <[email protected]>
*/
class Schedule extends Model implements HasPresenter
{
use HasMeta,
Expand Down Expand Up @@ -148,6 +143,20 @@ public function components()
return $this->hasMany(ScheduleComponent::class);
}

/**
* Scope schedules that are uncompleted.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeUncompleted(Builder $query)
{
return $query->whereIn('status', [self::UPCOMING, self::IN_PROGRESS])->where(function (Builder $query) {
return $query->whereNull('completed_at');
});
}

/**
* Scope schedules that are in progress.
*
Expand All @@ -158,7 +167,7 @@ public function components()
public function scopeInProgress(Builder $query)
{
return $query->where('scheduled_at', '<=', Carbon::now())->where('status', '<>', self::COMPLETE)->where(function ($query) {
$query->whereNull('completed_at')->orWhere('completed_at', '>', Carbon::now());
$query->whereNull('completed_at');
});
}

Expand All @@ -169,21 +178,33 @@ public function scopeInProgress(Builder $query)
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeFutureSchedules($query)
public function scopeScheduledInFuture($query)
{
return $query->whereIn('status', [self::UPCOMING, self::IN_PROGRESS])->where('scheduled_at', '>=', Carbon::now());
}

/**
* Scopes schedules to those in the past.
* Scopes schedules to those scheduled in the past.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeScheduledInPast($query)
{
return $query->whereIn('status', [self::UPCOMING, self::IN_PROGRESS])->where('scheduled_at', '<=', Carbon::now());
}

/**
* Scopes schedules to those completed in the past.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopePastSchedules($query)
public function scopeCompletedInPast($query)
{
return $query->where('status', '<', self::COMPLETE)->where('scheduled_at', '<=', Carbon::now());
return $query->where('status', '=', self::COMPLETE)->where('completed_at', '<=', Carbon::now());
}

/**
Expand Down

0 comments on commit ef51636

Please sign in to comment.