From 2a83d2520c4e22558f3360610014e813ffecc44e Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Oct 2023 22:58:27 +0200 Subject: [PATCH] feat: Modularisation of types --- app/Helpers/TaskType.php | 14 --- app/Http/Controllers/TaskController.php | 17 +-- app/Models/Task.php | 9 ++ app/Tasks/TaskTypes.php | 35 ++++++ app/Tasks/Types/CustomRequest.php | 26 +++++ app/Tasks/Types/RatingUpgrade.php | 34 ++++++ app/Tasks/Types/SoloEndorsement.php | 27 +++++ app/Tasks/Types/TheoreticalExam.php | 34 ++++++ composer.json | 2 + .../migrations/2023_10_04_085425_tasks.php | 2 +- resources/views/tasks/index.blade.php | 101 ++++++------------ resources/views/tasks/parts/row.blade.php | 26 +++++ 12 files changed, 228 insertions(+), 99 deletions(-) delete mode 100644 app/Helpers/TaskType.php create mode 100644 app/Tasks/TaskTypes.php create mode 100644 app/Tasks/Types/CustomRequest.php create mode 100644 app/Tasks/Types/RatingUpgrade.php create mode 100644 app/Tasks/Types/SoloEndorsement.php create mode 100644 app/Tasks/Types/TheoreticalExam.php create mode 100644 resources/views/tasks/parts/row.blade.php diff --git a/app/Helpers/TaskType.php b/app/Helpers/TaskType.php deleted file mode 100644 index 0fcf0bee4..000000000 --- a/app/Helpers/TaskType.php +++ /dev/null @@ -1,14 +0,0 @@ - ['text' => 'Theoretical Exam Access', 'icon' => 'fas fa-key'], - 2 => ['text' => 'Solo Endorsement', 'icon' => 'fas fa-clock'], - 3 => ['text' => 'Rating Upgrade', 'icon' => 'fas fa-circle-arrow-up'], - 4 => ['text' => 'Custom Memo', 'icon' => 'fas fa-message'], - ]; - /** * * Show the application task dashboard. @@ -27,10 +17,9 @@ class TaskController extends Controller public function index() { $user = auth()->user(); - $tasks = Task::where('recipient_user_id', $user->id)->get()->sortByDesc('created_at'); - $taskTypes = self::$types; - - return view('tasks.index', compact('tasks', 'taskTypes')); + $tasks = Task::where('recipient_user_id', $user->id)->get()->sortBy('created_at'); + + return view('tasks.index', compact('tasks')); } } diff --git a/app/Models/Task.php b/app/Models/Task.php index e27dbbf6b..2603343d8 100644 --- a/app/Models/Task.php +++ b/app/Models/Task.php @@ -22,4 +22,13 @@ public function sender() { return $this->belongsTo(User::class, 'sender_user_id'); } + + public function type(){ + if($this->type) { + return app($this->type); + } else { + throw new \Exception('Invalid task type: ' . $this->type); + } + } + } diff --git a/app/Tasks/TaskTypes.php b/app/Tasks/TaskTypes.php new file mode 100644 index 000000000..5f39a73d2 --- /dev/null +++ b/app/Tasks/TaskTypes.php @@ -0,0 +1,35 @@ +name = $this->getName(); + $this->icon = $this->getIcon(); + } + + public function onCreated(){ + // Default logic for creating a task + } + + public function onCompleted(){ + // Default logic for completing a task + } + + public function onDeclined(){ + // Default logic for declining a task + } + + abstract public function getName(); + abstract public function getIcon(); + abstract public function getText(Task $model); + abstract public function getLink(Task $model); + +} \ No newline at end of file diff --git a/app/Tasks/Types/CustomRequest.php b/app/Tasks/Types/CustomRequest.php new file mode 100644 index 000000000..7ea3379b4 --- /dev/null +++ b/app/Tasks/Types/CustomRequest.php @@ -0,0 +1,26 @@ +message; + } + + public function getLink(Task $model){ + return false; + } + +} \ No newline at end of file diff --git a/app/Tasks/Types/RatingUpgrade.php b/app/Tasks/Types/RatingUpgrade.php new file mode 100644 index 000000000..2fb2526da --- /dev/null +++ b/app/Tasks/Types/RatingUpgrade.php @@ -0,0 +1,34 @@ +reference_training_id)->getInlineRatings(); + } + + public function getLink(Task $model){ + $user = User::find($model->reference_user_id); + $userEud = $user->division == 'EUD'; + + if($userEud){ + return 'https://www.atsimtest.com/index.php?cmd=admin&sub=memberdetail&memberid=' . $model->reference_user_id; + } + + return false; + } +} \ No newline at end of file diff --git a/app/Tasks/Types/SoloEndorsement.php b/app/Tasks/Types/SoloEndorsement.php new file mode 100644 index 000000000..f2aecafcb --- /dev/null +++ b/app/Tasks/Types/SoloEndorsement.php @@ -0,0 +1,27 @@ +reference_user_id); + } +} \ No newline at end of file diff --git a/app/Tasks/Types/TheoreticalExam.php b/app/Tasks/Types/TheoreticalExam.php new file mode 100644 index 000000000..701bc9476 --- /dev/null +++ b/app/Tasks/Types/TheoreticalExam.php @@ -0,0 +1,34 @@ +reference_user_id); + $userEud = $user->division == 'EUD'; + + if($userEud){ + return 'https://www.atsimtest.com/index.php?cmd=admin&sub=memberdetail&memberid=' . $model->reference_user_id; + } + + return false; + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index c8a46b707..3e74e7763 100755 --- a/composer.json +++ b/composer.json @@ -48,6 +48,8 @@ "psr-4": { "App\\": "app/", "App\\Helpers\\": "app/helpers", + "App\\Tasks\\": "app/Tasks/", + "App\\Tasks\\Types\\": "app/Tasks/Types/", "Database\\Factories\\": "database/factories/", "Database\\Seeders\\": "database/seeders/" }, diff --git a/database/migrations/2023_10_04_085425_tasks.php b/database/migrations/2023_10_04_085425_tasks.php index 5662293d4..9fb16ee5a 100644 --- a/database/migrations/2023_10_04_085425_tasks.php +++ b/database/migrations/2023_10_04_085425_tasks.php @@ -15,7 +15,7 @@ public function up() { Schema::create('tasks', function (Blueprint $table) { $table->id(); - $table->tinyInteger('type'); + $table->string('type'); $table->tinyInteger('status')->default(0); $table->string('status_comment', 256)->nullable(); $table->string('message', 256)->nullable(); diff --git a/resources/views/tasks/index.blade.php b/resources/views/tasks/index.blade.php index aee732f47..6192a6ea4 100644 --- a/resources/views/tasks/index.blade.php +++ b/resources/views/tasks/index.blade.php @@ -15,82 +15,43 @@
-
-
Tasks
-
+ @if($tasks->count()) +
+
Tasks
+
+ @endif
- - - - - - - - - - - - - - @foreach($tasks as $task) + @if($tasks->count()) +
Task #ReceivedTypeRegardingRequestFromActions
+ - - - - - - - - + + + + + + - - @endforeach - - -
{{ $task->id }}{{ $task->created_at->diffForHumans() }} {{ $taskTypes[$task->type]["text"] }}{{ \App\Models\User::find($task->reference_user_id)->name }} ({{ $task->reference_user_id }}) - @if($task->type == \App\Helpers\TaskType::THEORY_EXAM->value) - - @if(\App\Models\User::find($task->reference_user_id)->division == 'EUD') - - Grant theoretical exam access - @else - Grant theoretical exam access - @endif - - @elseif($task->type == \App\Helpers\TaskType::SOLO_ENDORSEMENT->value) - - - Grant solo endorsement - - @elseif($task->type == \App\Helpers\TaskType::RATING_UPGRADE->value) - - @if(\App\Models\User::find($task->reference_user_id)->division == 'EUD') - - Upgrade rating to {{ \App\Models\Training::find($task->reference_training_id)->getInlineRatings() }} - @else - Upgrade rating to {{ \App\Models\Training::find($task->reference_training_id)->getInlineRatings() }} - @endif - - @if(\App\Models\Training::find($task->reference_training_id)->status == \App\Helpers\TrainingStatus::COMPLETED->value) - (Examination completed) - @endif - - @elseif($task->type == \App\Helpers\TaskType::MEMO->value) - {{ $task->message }} - @endif - - @isset($task->sender_user_id) - {{ \App\Models\User::find($task->sender_user_id)->name }} ({{ $task->sender_user_id }}) - @else - System - @endisset - - Complete - Decline - Task #ReceivedRegardingRequestFromActions
+ + + @foreach($tasks as $task) + @include('tasks.parts.row', ['task' => $task]) + @endforeach + + + @endif + + @if($tasks->count() == 0) +
+ +

+ You have no tasks +

+
+ @endif
diff --git a/resources/views/tasks/parts/row.blade.php b/resources/views/tasks/parts/row.blade.php new file mode 100644 index 000000000..80a3d729b --- /dev/null +++ b/resources/views/tasks/parts/row.blade.php @@ -0,0 +1,26 @@ + + {{ $task->id }} + {{ $task->created_at->diffForHumans() }} + {{ \App\Models\User::find($task->reference_user_id)->name }} ({{ $task->reference_user_id }}) + + + + @if($task->type()->getLink($task)) + {{ $task->type()->getText($task) }} + @else + {{ $task->type()->getText($task) }} + @endif + + + @isset($task->sender_user_id) + {{ \App\Models\User::find($task->sender_user_id)->name }} ({{ $task->sender_user_id }}) + @else + System + @endisset + + + + Complete + Decline + + \ No newline at end of file