diff --git a/app/Http/Controllers/Api/V1/Filters/FilterController.php b/app/Http/Controllers/Api/V1/Filters/FilterController.php index d326d85..25d9773 100644 --- a/app/Http/Controllers/Api/V1/Filters/FilterController.php +++ b/app/Http/Controllers/Api/V1/Filters/FilterController.php @@ -6,6 +6,7 @@ use App\Http\Controllers\Api\V1\Filters\Requests\FilterIndexRequest; use App\Http\Controllers\Controller; use App\Models\Ecommerce\Channel\Channel; use App\Models\Ecommerce\Product\Brand\Brand; +use Illuminate\Support\Facades\DB; use App\Models\Ecommerce\Product\Category\Category; use App\Models\Ecommerce\Product\Collection\Collection; use Illuminate\Http\JsonResponse; @@ -45,7 +46,7 @@ class FilterController extends Controller } if ($this->shouldFilterByCollection()) { - return $this->filterByCategoryResource(Collection::find($this->request->collection_id)); + return $this->categoriesFor(); } if ($this->shouldFilterByBrand()) { @@ -111,6 +112,35 @@ class FilterController extends Controller ->unique('categories.id'); } + /** + * Categories for a resource + */ + private function categoriesFor($resource) + { + return DB::table('categories as c') + ->select('c.id', 'c.parent_id', 'c.name') + ->where('c.is_visible', true) + ->whereExists(function ($query) { + $query->select(DB::raw(1)) + ->from('product_has_relations as phr_cat') + ->join('products as p', 'p.id', '=', 'phr_cat.product_id') + ->join('product_has_relations as phr_chan', 'phr_chan.product_id', '=', 'p.id') + ->whereColumn('phr_cat.productable_id', 'c.id') + ->where('phr_cat.productable_type', 'category') + ->where('phr_chan.productable_type', 'channel') + ->where('phr_chan.productable_id', 6) + ->where('p.is_visible', true) + ->whereNull('p.parent_id') + ->where('p.stock', '>', 0); + }) + ->get() + ->map(fn ($category) => [ + 'id' => $category->id, + 'parent_id' => $category->parent_id, + 'name' => $category->name, + ]); + } + /** * Check if request should be filtered by category */