From ecb255fc6aef2addc77eec44aff6d0b41be328d2 Mon Sep 17 00:00:00 2001 From: Jonatan Rek Date: Tue, 6 Aug 2024 11:02:45 +0200 Subject: [PATCH] Progress --- app/Jobs/ScheduleNextMaintenance.php | 19 ++++++++------ app/Livewire/Maintenance/Form.php | 18 +++++++------ .../{MaintenanceTask => Task}/DataTable.php | 16 ++++++------ .../{MaintenanceTask => Task}/Form.php | 23 ++++++++--------- app/Models/Maintenance.php | 10 ++++---- app/Models/MaintenanceHistory.php | 5 ++++ app/Models/MaintenanceHostHistory.php | 11 +++++--- app/Models/MaintenanceTask.php | 9 +++++-- app/Models/MaintenanceTaskHistory.php | 15 +++++++++-- app/Models/Task.php | 5 ++++ ..._083526_create_maintenance_tasks_table.php | 1 + ...reate_maintenance_task_histories_table.php | 3 ++- ...reate_maintenance_host_histories_table.php | 4 ++- .../views/livewire/maintenance/form.blade.php | 2 +- .../{maintenance-task => task}/form.blade.php | 1 - .../maintenance/planned-detail.blade.php | 25 +++++++++++-------- resources/views/task/index.blade.php | 6 ++--- 17 files changed, 108 insertions(+), 65 deletions(-) rename app/Livewire/{MaintenanceTask => Task}/DataTable.php (65%) rename app/Livewire/{MaintenanceTask => Task}/Form.php (54%) rename resources/views/livewire/{maintenance-task => task}/form.blade.php (74%) diff --git a/app/Jobs/ScheduleNextMaintenance.php b/app/Jobs/ScheduleNextMaintenance.php index 24e896e..c87b326 100644 --- a/app/Jobs/ScheduleNextMaintenance.php +++ b/app/Jobs/ScheduleNextMaintenance.php @@ -4,6 +4,7 @@ use App\Models\Maintenance; use App\Models\MaintenanceHistory; +use App\Models\MaintenanceTaskHistory; use Cron\CronExpression; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; @@ -41,17 +42,21 @@ public function handle(): void 'start_at' => $cron->getNextRunDate(null, 2) ]); $maintenancePlanned->refresh(); - - $tasks = $maintenancePlanned->tasks; - if(!empty($tasks)){ - dd($tasks); + + $hosts = $maintenance->hosts; + foreach ($hosts as $key => $host) { + $maintenancePlannedHost = $maintenancePlanned->historyHosts()->create([ + 'host_id' => $host->id + ]); + + $tasks = $maintenance->tasks; foreach ($tasks as $key => $task) { - $maintenancePlanned->tasks()->create([ - 'host_id' => $task->host_id, + $maintenancePlannedHost->historyTasks()->create([ + 'maintenance_task_id' => $task->id, + 'maintenance_history_id' => $maintenancePlanned->id ]); } } - } die(); } diff --git a/app/Livewire/Maintenance/Form.php b/app/Livewire/Maintenance/Form.php index 98b2bde..2fa945b 100644 --- a/app/Livewire/Maintenance/Form.php +++ b/app/Livewire/Maintenance/Form.php @@ -6,7 +6,7 @@ use Livewire\Component; use App\Models\Maintenance; use App\Models\MaintenanceTask; -use Illuminate\Console\View\Components\Task; +use App\Models\Task; class Form extends Component { @@ -35,7 +35,7 @@ protected function rules() public function mount($model = null) { $this->hosts_available = Host::all()->pluck('hostname', 'id')->toArray(); - $this->hosts_tasks_available = MaintenanceTask::all()->pluck('name', 'id')->toArray(); + $this->hosts_tasks_available = Task::all()->pluck('name', 'id')->toArray(); if (!empty($model)) { $maintenance = Maintenance::find($model); @@ -48,7 +48,7 @@ public function mount($model = null) $this->hosts = $maintenance->hosts()->pluck('hosts.id')->toArray(); foreach ($maintenance->tasks as $task) { - $this->hosts_tasks_available[$task->host_id][] = $task->id; + $this->hosts_tasks[$task->host_id][] = $task->task_id; } $this->action = 'update'; @@ -71,7 +71,7 @@ public function update() $validatedData = $this->validate(); $maintenance = Maintenance::find($this->model); $maintenance->hosts()->whereNotIn('host_id', $this->hosts)->delete(); - + if (!empty($maintenance)) { $maintenance->update($validatedData); $hosts = Host::whereIn('id', $this->hosts)->whereNotIn('id', $maintenance->hosts()->pluck('hosts.id')->toArray())->get(); @@ -79,10 +79,14 @@ public function update() $maintenance->hosts()->attach($host); } $maintenance->refresh(); + foreach ($maintenance->hosts as $host) { - foreach ($this->hosts_tasks[$host->id] as $task_id => $host_task_name) { - $host->tasks()->create([ - 'task_id' => $task_id + $alreadyUsedHosts = $maintenance->tasks()->where('host_id', $host->id)->distinct('maintenance_tasks.task_id')->pluck('maintenance_tasks.task_id')->toArray(); + $tasks = Task::whereIn('id', $this->hosts_tasks[$host->id])->whereNotIn('id', $alreadyUsedHosts)->get(); + foreach ($tasks as $key => $task) { + $maintenance->tasks()->create([ + 'task_id' => $task->id, + 'host_id' => $host->id, ]); } } diff --git a/app/Livewire/MaintenanceTask/DataTable.php b/app/Livewire/Task/DataTable.php similarity index 65% rename from app/Livewire/MaintenanceTask/DataTable.php rename to app/Livewire/Task/DataTable.php index 0c247b8..88b52af 100644 --- a/app/Livewire/MaintenanceTask/DataTable.php +++ b/app/Livewire/Task/DataTable.php @@ -1,20 +1,20 @@ '$refresh', + 'taskAdded' => '$refresh', 'closeModal' => '$refresh', ]; public function query(): Builder { - return MaintenanceTask::query(); + return Task::query(); } public function headers(): array @@ -25,8 +25,8 @@ public function headers(): array ]; } - public function remove($maintenancetask_id){ - MaintenanceTask::find($maintenancetask_id)->delete(); + public function remove($task_id){ + Task::find($task_id)->delete(); } public function actions($item) @@ -47,8 +47,8 @@ public function actions($item) ]; } - public function edit($maintenancetask_id) + public function edit($task_id) { - $this->dispatch('openModal', 'maintenance-task.form', __('boilerplate::maintenance-task.edit'), ['model' => $maintenancetask_id]); + $this->dispatch('openModal', 'task.form', __('boilerplate::task.edit'), ['model' => $task_id]); } } diff --git a/app/Livewire/MaintenanceTask/Form.php b/app/Livewire/Task/Form.php similarity index 54% rename from app/Livewire/MaintenanceTask/Form.php rename to app/Livewire/Task/Form.php index 35a08b4..88f9be0 100644 --- a/app/Livewire/MaintenanceTask/Form.php +++ b/app/Livewire/Task/Form.php @@ -1,13 +1,12 @@ 'required', 'name' => 'required', 'description' => 'required', ]; @@ -24,13 +22,12 @@ protected function rules() public function mount ($model = null){ if (!empty($model)) { - $maintenanceTask = MaintenanceTask::find($model); + $task = Task::find($model); $this->model = $model; - $this->maintenance_id = $maintenanceTask->maintenance_id; - $this->name = $maintenanceTask->name; - $this->description = $maintenanceTask->description; + $this->name = $task->name; + $this->description = $task->description; $this->action = 'update'; } @@ -39,22 +36,22 @@ public function mount ($model = null){ public function store() { $validatedData = $this->validate(); - MaintenanceTask::create($validatedData); + Task::create($validatedData); $this->dispatch('closeModal'); } public function update() { $validatedData = $this->validate(); - $maintenanceTask = MaintenanceTask::find($this->model); - if (!empty($maintenanceTask)) { - $maintenanceTask->update($validatedData); + $task = Task::find($this->model); + if (!empty($task)) { + $task->update($validatedData); } $this->dispatch('closeModal'); } public function render() { - return view('livewire.maintenance-task.form'); + return view('livewire.task.form'); } } diff --git a/app/Models/Maintenance.php b/app/Models/Maintenance.php index 393986b..57abc06 100644 --- a/app/Models/Maintenance.php +++ b/app/Models/Maintenance.php @@ -15,16 +15,16 @@ class Maintenance extends Model 'schedule', ]; - public function history() - { - return $this->hasMany(MaintenanceHistory::class); - } - public function hosts() { return $this->belongsToMany(Host::class, "maintenance_host", "maintenance_id", "host_id" ); } + public function history() + { + return $this->hasMany(MaintenanceHistory::class); + } + public function tasks() { return $this->hasMany(MaintenanceTask::class,); diff --git a/app/Models/MaintenanceHistory.php b/app/Models/MaintenanceHistory.php index d956f08..ddd1a56 100644 --- a/app/Models/MaintenanceHistory.php +++ b/app/Models/MaintenanceHistory.php @@ -19,4 +19,9 @@ public function maintenance() { return $this->BelongsTo(Maintenance::class); } + + public function historyHosts() + { + return $this->hasMany(MaintenanceHostHistory::class); + } } diff --git a/app/Models/MaintenanceHostHistory.php b/app/Models/MaintenanceHostHistory.php index b8756ba..60e994a 100644 --- a/app/Models/MaintenanceHostHistory.php +++ b/app/Models/MaintenanceHostHistory.php @@ -9,12 +9,17 @@ class MaintenanceHostHistory extends Model { use HasFactory; - public function hosts() + protected $fillable = [ + 'maintenance_history_id', + 'host_id', + ]; + + public function host() { - return $this->belongsToMany(MaintenanceHostHistory::class); + return $this->belongsTo(Host::class); } - public function tasks() + public function historyTasks() { return $this->hasMany(MaintenanceTaskHistory::class); } diff --git a/app/Models/MaintenanceTask.php b/app/Models/MaintenanceTask.php index 64b66e7..5f7d556 100644 --- a/app/Models/MaintenanceTask.php +++ b/app/Models/MaintenanceTask.php @@ -12,7 +12,12 @@ class MaintenanceTask extends Model protected $fillable = [ 'maintenance_id', 'host_id', - 'name', - 'description', + 'task_id', ]; + + + public function task() + { + return $this->belongsTo(Task::class); + } } diff --git a/app/Models/MaintenanceTaskHistory.php b/app/Models/MaintenanceTaskHistory.php index 8bcd6aa..9ff909f 100644 --- a/app/Models/MaintenanceTaskHistory.php +++ b/app/Models/MaintenanceTaskHistory.php @@ -8,9 +8,20 @@ class MaintenanceTaskHistory extends Model { use HasFactory; + + protected $fillable = [ + 'maintenance_task_id', + 'maintenance_history_id', + 'maintenance_host_history_id', + ]; - public function task() + public function maintenance() { - return $this->BelongsTo(MaintenanceTask::class); + return $this->BelongsTo(MaintenanceHistory::class); + } + + public function maintenanceTask() + { + return $this->belongsTo(MaintenanceTask::class); } } diff --git a/app/Models/Task.php b/app/Models/Task.php index 5458207..576ac5e 100644 --- a/app/Models/Task.php +++ b/app/Models/Task.php @@ -8,4 +8,9 @@ class Task extends Model { use HasFactory; + + protected $fillable = [ + 'name', + 'description', + ]; } diff --git a/database/migrations/2024_07_30_083526_create_maintenance_tasks_table.php b/database/migrations/2024_07_30_083526_create_maintenance_tasks_table.php index da07d79..2f13a32 100644 --- a/database/migrations/2024_07_30_083526_create_maintenance_tasks_table.php +++ b/database/migrations/2024_07_30_083526_create_maintenance_tasks_table.php @@ -18,6 +18,7 @@ public function up(): void $table->id(); $table->foreignIdFor(Maintenance::class); $table->foreignIdFor(Task::class); + $table->foreignIdFor(Host::class); $table->timestamps(); }); } diff --git a/database/migrations/2024_07_30_083546_create_maintenance_task_histories_table.php b/database/migrations/2024_07_30_083546_create_maintenance_task_histories_table.php index b1f19f7..a4b0fd8 100644 --- a/database/migrations/2024_07_30_083546_create_maintenance_task_histories_table.php +++ b/database/migrations/2024_07_30_083546_create_maintenance_task_histories_table.php @@ -2,6 +2,7 @@ use App\Models\Host; use App\Models\MaintenanceHistory; +use App\Models\MaintenanceHostHistory; use App\Models\MaintenanceTask; use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; @@ -18,7 +19,7 @@ public function up(): void $table->id(); $table->foreignIdFor(MaintenanceTask::class); $table->foreignIdFor(MaintenanceHistory::class); - $table->foreignIdFor(Host::class); + $table->foreignIdFor(MaintenanceHostHistory::class); $table->string('status')->default(1); $table->datetime('finished_at')->nullable(); $table->timestamps(); diff --git a/database/migrations/2024_07_30_083607_create_maintenance_host_histories_table.php b/database/migrations/2024_07_30_083607_create_maintenance_host_histories_table.php index 3f80778..dd8bcf2 100644 --- a/database/migrations/2024_07_30_083607_create_maintenance_host_histories_table.php +++ b/database/migrations/2024_07_30_083607_create_maintenance_host_histories_table.php @@ -2,6 +2,8 @@ use App\Models\Host; use App\Models\Maintenance; +use App\Models\MaintenanceHistory; +use App\Models\MaintenanceHostHistory; use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; @@ -15,7 +17,7 @@ public function up(): void { Schema::create('maintenance_host_histories', function (Blueprint $table) { $table->id(); - $table->foreignIdFor(Maintenance::class); + $table->foreignIdFor(MaintenanceHistory::class); $table->foreignIdFor(Host::class); $table->timestamps(); }); diff --git a/resources/views/livewire/maintenance/form.blade.php b/resources/views/livewire/maintenance/form.blade.php index f9e4fd8..e673b14 100644 --- a/resources/views/livewire/maintenance/form.blade.php +++ b/resources/views/livewire/maintenance/form.blade.php @@ -5,7 +5,7 @@ - @foreach($hosts as $host_id) + @foreach($hosts as $key => $host_id) @endforeach diff --git a/resources/views/livewire/maintenance-task/form.blade.php b/resources/views/livewire/task/form.blade.php similarity index 74% rename from resources/views/livewire/maintenance-task/form.blade.php rename to resources/views/livewire/task/form.blade.php index 2a1a1e4..d2c959b 100644 --- a/resources/views/livewire/maintenance-task/form.blade.php +++ b/resources/views/livewire/task/form.blade.php @@ -1,6 +1,5 @@
- Create diff --git a/resources/views/maintenance/planned-detail.blade.php b/resources/views/maintenance/planned-detail.blade.php index e85fb34..53f82e6 100644 --- a/resources/views/maintenance/planned-detail.blade.php +++ b/resources/views/maintenance/planned-detail.blade.php @@ -3,18 +3,21 @@ -

{!! $maintenance_history->maintenance->description !!}

- @foreach ($maintenance_history->maintenance->hosts as $host) -
-
- {{ $host->hostname }} -
-
-
-
Description
-
+

{!! $maintenance_history->maintenance->description !!}

+ @foreach ($maintenance_history->historyHosts as $historyHost) +
+
+ {{ $historyHost->host->hostname }} +
+
+
+ @foreach ($historyHost->historyTasks as $historyTasks) + + {!! $historyTasks->maintenanceTask->task->description !!} + @endforeach
+
@endforeach
- + \ No newline at end of file diff --git a/resources/views/task/index.blade.php b/resources/views/task/index.blade.php index 6190612..28190f7 100644 --- a/resources/views/task/index.blade.php +++ b/resources/views/task/index.blade.php @@ -1,13 +1,13 @@
- @livewire('maintenance-task.data-table', [], key('data-table')) + @livewire('task.data-table', [], key('data-table'))