Fix Device::isUnderMaintenance() add location relation

This commit is contained in:
Tony Murray
2020-06-14 12:39:10 -05:00
parent eb6370acd4
commit 0a5c3cc747
6 changed files with 25 additions and 18 deletions

View File

@@ -133,25 +133,25 @@ class Device extends BaseModel
public function isUnderMaintenance()
{
if (!$this->device_id) {
return false;
}
$query = AlertSchedule::isActive()
->join('alert_schedulables', 'alert_schedule.schedule_id', 'alert_schedulables.schedule_id')
->where(function ($query) {
$query->where(function ($query) {
$query->where('alert_schedulable_type', 'device')
->where('alert_schedulable_id', $this->device_id);
->where(function (Builder $query) {
$query->whereHas('devices', function (Builder $query) {
$query->where('alert_schedulables.alert_schedulable_id', $this->device_id);
});
if ($this->groups) {
$query->orWhere(function ($query) {
$query->where('alert_schedulable_type', 'device_group')
->whereIn('alert_schedulable_id', $this->groups->pluck('id'));
$query->orWhereHas('deviceGroups', function (Builder $query) {
$query->whereIn('alert_schedulables.alert_schedulable_id', $this->groups->pluck('id'));
});
}
if ($this->location) {
$query->orWhere(function ($query) {
$query->where('alert_schedulable_type', 'location')
->where('alert_schedulable_id', $this->location->id);
$query->orWhereHas('locations', function (Builder $query) {
$query->where('alert_schedulables.alert_schedulable_id', $this->location->id);
});
}
});