diff --git a/app/Http/Controllers/TaskController.php b/app/Http/Controllers/TaskController.php new file mode 100644 index 0000000..2c2e755 --- /dev/null +++ b/app/Http/Controllers/TaskController.php @@ -0,0 +1,13 @@ + [' fas fa-server', 'host'], 'Maintenance' => [' fas fa-calendar', 'maintenance'], + 'Tasks' => [' fas fa-calendar', 'tasks'], ]; foreach ($systemRoutes as $title => $route_data) { diff --git a/app/Jobs/ScheduleNextMaintenance.php b/app/Jobs/ScheduleNextMaintenance.php index b3e884f..24e896e 100644 --- a/app/Jobs/ScheduleNextMaintenance.php +++ b/app/Jobs/ScheduleNextMaintenance.php @@ -37,15 +37,22 @@ public function handle(): void } $cron = new CronExpression($maintenance->schedule); - if (!$cron->isDue('now')) { - dd($cron->getNextRunDate()); - return; - } - - $maintenance->history()->create([ + $maintenancePlanned = $maintenance->history()->create([ 'start_at' => $cron->getNextRunDate(null, 2) ]); - die(); + $maintenancePlanned->refresh(); + + $tasks = $maintenancePlanned->tasks; + if(!empty($tasks)){ + dd($tasks); + foreach ($tasks as $key => $task) { + $maintenancePlanned->tasks()->create([ + 'host_id' => $task->host_id, + ]); + } + } + } + die(); } } diff --git a/app/Livewire/Maintenance/Form.php b/app/Livewire/Maintenance/Form.php index d951719..98b2bde 100644 --- a/app/Livewire/Maintenance/Form.php +++ b/app/Livewire/Maintenance/Form.php @@ -5,6 +5,8 @@ use App\Models\Host; use Livewire\Component; use App\Models\Maintenance; +use App\Models\MaintenanceTask; +use Illuminate\Console\View\Components\Task; class Form extends Component { @@ -18,6 +20,8 @@ class Form extends Component public $hosts = []; public $hosts_available = []; + public $hosts_tasks = []; + public $hosts_tasks_available = []; protected function rules() { @@ -31,6 +35,8 @@ 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(); + if (!empty($model)) { $maintenance = Maintenance::find($model); @@ -39,7 +45,11 @@ public function mount($model = null) $this->name = $maintenance->name; $this->description = $maintenance->description; $this->schedule = $maintenance->schedule; + $this->hosts = $maintenance->hosts()->pluck('hosts.id')->toArray(); + foreach ($maintenance->tasks as $task) { + $this->hosts_tasks_available[$task->host_id][] = $task->id; + } $this->action = 'update'; } @@ -61,13 +71,21 @@ 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(); foreach ($hosts as $key => $host) { $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 + ]); + } + } } $this->dispatch('closeModal'); diff --git a/app/Livewire/MaintenanceTask/DataTable.php b/app/Livewire/MaintenanceTask/DataTable.php new file mode 100644 index 0000000..0c247b8 --- /dev/null +++ b/app/Livewire/MaintenanceTask/DataTable.php @@ -0,0 +1,54 @@ + '$refresh', + 'closeModal' => '$refresh', + ]; + + public function query(): Builder + { + return MaintenanceTask::query(); + } + + public function headers(): array + { + return [ + 'name' => 'name', + 'description' => 'description', + ]; + } + + public function remove($maintenancetask_id){ + MaintenanceTask::find($maintenancetask_id)->delete(); + } + + public function actions($item) + { + return [ + [ + 'type' => "livewire", + 'action' => "edit", + 'text' => "edit", + 'parameters' => $item['id'] + ], + [ + 'type' => "livewire", + 'action' => "remove", + 'text' => "remove", + 'parameters' => $item['id'] + ] + ]; + } + + public function edit($maintenancetask_id) + { + $this->dispatch('openModal', 'maintenance-task.form', __('boilerplate::maintenance-task.edit'), ['model' => $maintenancetask_id]); + } +} diff --git a/app/Livewire/MaintenanceTask/Form.php b/app/Livewire/MaintenanceTask/Form.php new file mode 100644 index 0000000..35a08b4 --- /dev/null +++ b/app/Livewire/MaintenanceTask/Form.php @@ -0,0 +1,60 @@ + 'required', + 'name' => 'required', + 'description' => 'required', + ]; + } + + public function mount ($model = null){ + if (!empty($model)) { + $maintenanceTask = MaintenanceTask::find($model); + + $this->model = $model; + + $this->maintenance_id = $maintenanceTask->maintenance_id; + $this->name = $maintenanceTask->name; + $this->description = $maintenanceTask->description; + + $this->action = 'update'; + } + } + + public function store() + { + $validatedData = $this->validate(); + MaintenanceTask::create($validatedData); + $this->dispatch('closeModal'); + } + + public function update() + { + $validatedData = $this->validate(); + $maintenanceTask = MaintenanceTask::find($this->model); + if (!empty($maintenanceTask)) { + $maintenanceTask->update($validatedData); + } + $this->dispatch('closeModal'); + } + + public function render() + { + return view('livewire.maintenance-task.form'); + } +} diff --git a/app/Models/Host.php b/app/Models/Host.php index 87b6a8a..0cc252c 100644 --- a/app/Models/Host.php +++ b/app/Models/Host.php @@ -12,4 +12,9 @@ class Host extends Model protected $fillable = [ 'hostname', ]; + + public function tasks() + { + return $this->hasMany(MaintenanceTask::class,); + } } diff --git a/app/Models/Maintenance.php b/app/Models/Maintenance.php index decb1a0..393986b 100644 --- a/app/Models/Maintenance.php +++ b/app/Models/Maintenance.php @@ -24,4 +24,9 @@ public function hosts() { return $this->belongsToMany(Host::class, "maintenance_host", "maintenance_id", "host_id" ); } + + public function tasks() + { + return $this->hasMany(MaintenanceTask::class,); + } } diff --git a/app/Models/MaintenanceHostHistory.php b/app/Models/MaintenanceHostHistory.php index 4bd9486..b8756ba 100644 --- a/app/Models/MaintenanceHostHistory.php +++ b/app/Models/MaintenanceHostHistory.php @@ -8,4 +8,14 @@ class MaintenanceHostHistory extends Model { use HasFactory; + + public function hosts() + { + return $this->belongsToMany(MaintenanceHostHistory::class); + } + + public function tasks() + { + return $this->hasMany(MaintenanceTaskHistory::class); + } } diff --git a/app/Models/MaintenanceTask.php b/app/Models/MaintenanceTask.php index 739e6f8..64b66e7 100644 --- a/app/Models/MaintenanceTask.php +++ b/app/Models/MaintenanceTask.php @@ -8,4 +8,11 @@ class MaintenanceTask extends Model { use HasFactory; + + protected $fillable = [ + 'maintenance_id', + 'host_id', + 'name', + 'description', + ]; } diff --git a/app/Models/MaintenanceTaskHistory.php b/app/Models/MaintenanceTaskHistory.php index 691b48a..8bcd6aa 100644 --- a/app/Models/MaintenanceTaskHistory.php +++ b/app/Models/MaintenanceTaskHistory.php @@ -8,4 +8,9 @@ class MaintenanceTaskHistory extends Model { use HasFactory; + + public function task() + { + return $this->BelongsTo(MaintenanceTask::class); + } } diff --git a/app/Models/Task.php b/app/Models/Task.php new file mode 100644 index 0000000..5458207 --- /dev/null +++ b/app/Models/Task.php @@ -0,0 +1,11 @@ +=12.0.0", @@ -6702,13 +6702,13 @@ }, "require-dev": { "larastan/larastan": "^2.9.8", - "laravel/framework": "^11.16.0", - "laravel/pint": "^1.16.2", - "laravel/sail": "^1.30.2", + "laravel/framework": "^11.19.0", + "laravel/pint": "^1.17.1", + "laravel/sail": "^1.31.0", "laravel/sanctum": "^4.0.2", "laravel/tinker": "^2.9.0", - "orchestra/testbench-core": "^9.2.1", - "pestphp/pest": "^2.34.9 || ^3.0.0", + "orchestra/testbench-core": "^9.2.3", + "pestphp/pest": "^2.35.0 || ^3.0.0", "sebastian/environment": "^6.1.0 || ^7.0.0" }, "type": "library", @@ -6771,7 +6771,7 @@ "type": "patreon" } ], - "time": "2024-07-16T22:41:01+00:00" + "time": "2024-08-03T15:32:23+00:00" }, { "name": "phar-io/manifest", @@ -7216,16 +7216,16 @@ }, { "name": "phpunit/phpunit", - "version": "11.2.9", + "version": "11.3.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "c197bbaaca360efda351369bf1fd9cc1ca6bcbf7" + "reference": "a8dce73a8938dfec7ac0daa91bdbcaae7d7188a3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/c197bbaaca360efda351369bf1fd9cc1ca6bcbf7", - "reference": "c197bbaaca360efda351369bf1fd9cc1ca6bcbf7", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/a8dce73a8938dfec7ac0daa91bdbcaae7d7188a3", + "reference": "a8dce73a8938dfec7ac0daa91bdbcaae7d7188a3", "shasum": "" }, "require": { @@ -7264,7 +7264,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "11.2-dev" + "dev-main": "11.3-dev" } }, "autoload": { @@ -7296,7 +7296,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/11.2.9" + "source": "https://github.com/sebastianbergmann/phpunit/tree/11.3.0" }, "funding": [ { @@ -7312,7 +7312,7 @@ "type": "tidelift" } ], - "time": "2024-07-30T11:09:23+00:00" + "time": "2024-08-02T03:56:43+00:00" }, { "name": "sebastian/cli-parser", diff --git a/database/migrations/2024_07_30_083513_create_tasks_table.php b/database/migrations/2024_07_30_083513_create_tasks_table.php new file mode 100644 index 0000000..cc1c1e9 --- /dev/null +++ b/database/migrations/2024_07_30_083513_create_tasks_table.php @@ -0,0 +1,29 @@ +id(); + $table->string('name'); + $table->text('description'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('tasks'); + } +}; 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 a18cf88..da07d79 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 @@ -1,6 +1,8 @@ id(); $table->foreignIdFor(Maintenance::class); - $table->string('name'); - $table->text('description'); + $table->foreignIdFor(Task::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 7828311..b1f19f7 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 @@ -19,8 +19,8 @@ public function up(): void $table->foreignIdFor(MaintenanceTask::class); $table->foreignIdFor(MaintenanceHistory::class); $table->foreignIdFor(Host::class); - $table->string('status'); - $table->datetime('finished_at'); + $table->string('status')->default(1); + $table->datetime('finished_at')->nullable(); $table->timestamps(); }); } diff --git a/resources/views/livewire/maintenance-task/form.blade.php b/resources/views/livewire/maintenance-task/form.blade.php new file mode 100644 index 0000000..2a1a1e4 --- /dev/null +++ b/resources/views/livewire/maintenance-task/form.blade.php @@ -0,0 +1,8 @@ +