Browse Source

Support tasks, support individual settings, and other types are global settings

moell 8 months ago
parent
commit
494b698a08

+ 20 - 3
app/Http/Controllers/API/ApprovalFlowController.php

@@ -7,6 +7,7 @@ use App\Http\Requests\API\ApprovalFlow\CreateOrUpdateRequest;
 use App\Http\Resources\API\ApprovalFlowDetailResource;
 use App\Http\Resources\API\ApprovalFlowResource;
 use App\Models\ApprovalFlow;
+use App\Models\Enums\ApprovalFlowType;
 use App\Models\User;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Auth;
@@ -24,9 +25,22 @@ class ApprovalFlowController extends Controller
     {
         $nodes = $this->getApprovalNodes($request);
 
+        $limitObject = $request->get("object_type") && $request->get("object_id") && in_array($request->get("type"), [ApprovalFlowType::TASK->value]);
+
+        $exists = ApprovalFlow::query()->where("type", $request->type)
+            ->when(
+                $limitObject,
+                fn ($query) => $query->where([
+                    'object_type' => $request->object_type,
+                    'object_id' => $request->object_id,
+                ])
+            )
+            ->count();
+        throw_validation_if($exists, "Please do not add repeatedly.");
+
         $approvalFlow = new ApprovalFlow();
         $approvalFlow->mergeFillable([
-            'company_id'
+            'company_id', 'type'
         ]);
 
         $approvalFlow->fill([
@@ -83,11 +97,12 @@ class ApprovalFlowController extends Controller
         if ($nodeIsChange) {
             $newApprovalFlow = new ApprovalFlow();
             $newApprovalFlow->mergeFillable([
-                'company_id'
+                'company_id', 'type',
             ]);
 
             $newApprovalFlow->fill([
                 'name' => $request->name,
+                'type' => $approvalFlow->type,
                 'object_type' => $approvalFlow->object_type,
                 'object_id' => $approvalFlow->object_id,
                 'company_id' => Auth::user()->company_id,
@@ -97,13 +112,15 @@ class ApprovalFlowController extends Controller
             $newApprovalFlow->save();
 
             $approvalFlow->delete();
+
+            return new ApprovalFlowDetailResource($newApprovalFlow);
         } else {
             $approvalFlow->name = $request->name;
             $approvalFlow->nodes = $nodes;
             $approvalFlow->save();
         }
 
-        return $this->noContent();
+        return new ApprovalFlowDetailResource($approvalFlow);
     }
 
     public function show(string $id)

+ 5 - 2
app/Http/Requests/API/ApprovalFlow/CreateOrUpdateRequest.php

@@ -3,6 +3,7 @@
 namespace App\Http\Requests\API\ApprovalFlow;
 
 use App\Models\Enums\ApprovalFlowObjectType;
+use App\Models\Enums\ApprovalFlowType;
 use Illuminate\Foundation\Http\FormRequest;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Validation\Rules\Enum;
@@ -39,12 +40,14 @@ class CreateOrUpdateRequest extends FormRequest
         if ($this->method() == "POST") {
             $rules = [
                 ...$rules,
+                "type" => [
+                    "required",
+                    new Enum(ApprovalFlowType::class),
+                ],
                 "object_type" => [
-                    'required',
                     new Enum(ApprovalFlowObjectType::class),
                 ],
                 "object_id" => [
-                    'required',
                     function ($attribute, $value, $fail) {
                         $exist = ApprovalFlowObjectType::from($this->get("object_type"))
                             ->modelBuilderAllowed($value)

+ 1 - 0
app/Http/Resources/API/ApprovalFlowDetailResource.php

@@ -40,6 +40,7 @@ class ApprovalFlowDetailResource extends JsonResource
         return [
             "id" => $this->id,
             "name" => $this->name,
+            "type" => $this->type,
             "object_type" => $this->object_type,
             "object_id" => $this->object_id,
             "nodes" => $nodes,

+ 1 - 0
app/Http/Resources/API/ApprovalFlowResource.php

@@ -17,6 +17,7 @@ class ApprovalFlowResource extends JsonResource
         return [
             'id' => $this->id,
             'name' => $this->name,
+            'type' => $this->type,
             'object_type' => $this->object_type,
             'object_id' => $this->object_id,
             'created_at' => (string)$this->created_at,

+ 0 - 16
app/Models/Enums/ApprovalFlowObjectType.php

@@ -17,35 +17,19 @@ use App\Services\History\Detector\TaskDetector;
 
 enum ApprovalFlowObjectType: string
 {
-    case ASSET = "asset";
-
     case PROJECT = "project";
 
-    case REQUIREMENT = "requirement";
-
-    case TASK = "task";
-
-    case CONTAINER = "container";
-
     public function modelBuilder(): \Illuminate\Database\Eloquent\Builder
     {
         return match ($this) {
-            self::ASSET => Asset::query(),
             self::PROJECT => Project::query(),
-            self::TASK => Task::query(),
-            self::REQUIREMENT => Requirement::query(),
-            self::CONTAINER => Container::query(),
         };
     }
 
     public function modelBuilderAllowed(string $id = null): \Illuminate\Database\Eloquent\Builder
     {
         return match ($this) {
-            self::ASSET => Asset::query()->allowed(),
             self::PROJECT => Project::query()->allowed($id),
-            self::TASK => Task::query()->allowed($id),
-            self::REQUIREMENT => Requirement::query(),
-            self::CONTAINER => Container::query()->allowed($id),
         };
     }
 }

+ 14 - 0
app/Models/Enums/ApprovalFlowType.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace App\Models\Enums;
+
+enum ApprovalFlowType: string
+{
+    case PROJECT = "project";
+
+    case REQUIREMENT = "requirement";
+
+    case TASK = "task";
+
+    case CONTAINER = "container";
+}

+ 28 - 0
database/migrations/2024_06_24_160602_add_type_to_approval_flows_table.php

@@ -0,0 +1,28 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+        Schema::table('approval_flows', function (Blueprint $table) {
+            $table->string("type")->after("company_id");
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::table('approval_flows', function (Blueprint $table) {
+            $table->dropColumn(['type']);
+        });
+    }
+};