diff --git a/ProcessMaker/Http/Controllers/Api/ProcessController.php b/ProcessMaker/Http/Controllers/Api/ProcessController.php index 6a834fef74..8ea9f56958 100644 --- a/ProcessMaker/Http/Controllers/Api/ProcessController.php +++ b/ProcessMaker/Http/Controllers/Api/ProcessController.php @@ -107,6 +107,11 @@ public function index(Request $request) if (!empty($filter)) { $processes->filter($filter); } + // Filter by category + $category = $request->input('category', null); + if (!empty($category)) { + $processes->processCategory($category); + } if (!empty($pmql)) { try { diff --git a/ProcessMaker/Models/Process.php b/ProcessMaker/Models/Process.php index 9bb5270516..3d471bcccc 100644 --- a/ProcessMaker/Models/Process.php +++ b/ProcessMaker/Models/Process.php @@ -450,6 +450,14 @@ public function scopeArchived($query) return $query->where('processes.status', 'ARCHIVED'); } + /** + * Scope a query to include a specific category + */ + public function scopeProcessCategory($query, int $id) + { + return $query->where('processes.process_category_id', $id); + } + public function getCollaborations() { $this->bpmnDefinitions = app(BpmnDocumentInterface::class, ['process' => $this]); diff --git a/tests/Feature/Api/ProcessTest.php b/tests/Feature/Api/ProcessTest.php index 018605ab4e..18bad14bc2 100644 --- a/tests/Feature/Api/ProcessTest.php +++ b/tests/Feature/Api/ProcessTest.php @@ -555,6 +555,29 @@ public function testSorting() ]); } + /** + * Test filter by Category + */ + public function testFilterCategory() + { + // Create Category + $categoryA = ProcessCategory::factory()->create(); + $categoryB = ProcessCategory::factory()->create(); + // Now we create process related to this + Process::factory()->count(5)->create([ + 'process_category_id' => $categoryB->id, + ]); + // Get process without category + $response = $this->apiCall('GET', route('api.processes.index', ['per_page' => 5, 'page' => 1])); + $response->assertJsonCount(5, 'data'); + // Get process related categoryA + $response = $this->apiCall('GET', route('api.processes.index', ['category' => $categoryA->id])); + $response->assertJsonCount(0, 'data'); + // Get process related categoryB + $response = $this->apiCall('GET', route('api.processes.index', ['category' => $categoryB->id])); + $response->assertJsonCount(5, 'data'); + } + /** * Test pagination of process list */