feat(catalog): omit hidden products before pagination

This commit is contained in:
2026-08-24 15:15:55 -03:00
parent 2323994f30
commit e27f7bb166
7 changed files with 112 additions and 46 deletions

View File

@@ -22,7 +22,10 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class CatalogService
{
public function __construct(protected AttachmentService $attachmentService) {}
public function __construct(
protected AttachmentService $attachmentService,
private readonly CatalogItemAllowanceService $allowances,
) {}
/**
* @param array<string, mixed> $data
@@ -229,7 +232,7 @@ class CatalogService
$containsPattern = "%{$normalizedTerm}%";
$startsWithPattern = "{$normalizedTerm}%";
$paginator = CatalogItem::query()
$query = CatalogItem::query()
->where('tenant_code', $tenant->codigo)
->where(function (Builder $query) use ($containsPattern): void {
$query
@@ -257,7 +260,10 @@ class CatalogService
'variants.definitions.itemAttribute.attribute.options',
'bundleComponents.catalogItem',
'bundleComponents.variant.catalogItem',
])
]);
$paginator = $this->allowances
->applyProductVisibilityPolicy($query)
->orderByRaw(
'CASE WHEN LOWER(nombre) = ? THEN 0 WHEN LOWER(nombre) LIKE ? THEN 1 ELSE 2 END',
[$normalizedTerm, $startsWithPattern],
@@ -277,7 +283,7 @@ class CatalogService
int $perPage,
int $page,
): LengthAwarePaginator {
return CatalogItem::query()
$query = CatalogItem::query()
->where('tenant_code', $tenant->codigo)
->where('category_id', $category->id)
->with([
@@ -291,7 +297,10 @@ class CatalogService
'variants.definitions.itemAttribute.attribute.options',
'bundleComponents.catalogItem',
'bundleComponents.variant.catalogItem',
])
]);
return $this->allowances
->applyProductVisibilityPolicy($query)
->orderBy('nombre')
->paginate(perPage: $perPage, pageName: 'page', page: $page);
}