Compare commits
2 Commits
e16d91f3f9
...
5c457db1c9
| Author | SHA1 | Date | |
|---|---|---|---|
| 5c457db1c9 | |||
| 8dc80411d7 |
@@ -4,9 +4,11 @@ namespace App\Domains\Catalog\Controllers;
|
|||||||
|
|
||||||
use App\Domains\Catalog\Enums\GroupLayout;
|
use App\Domains\Catalog\Enums\GroupLayout;
|
||||||
use App\Domains\Catalog\Models\CatalogItem;
|
use App\Domains\Catalog\Models\CatalogItem;
|
||||||
|
use App\Domains\Catalog\Models\Category;
|
||||||
use App\Domains\Catalog\Models\FeaturedGroup;
|
use App\Domains\Catalog\Models\FeaturedGroup;
|
||||||
use App\Domains\Catalog\Models\FeaturedItem;
|
use App\Domains\Catalog\Models\FeaturedItem;
|
||||||
use App\Domains\Catalog\Requests\CatalogItemDetailRequest;
|
use App\Domains\Catalog\Requests\CatalogItemDetailRequest;
|
||||||
|
use App\Domains\Catalog\Requests\CategoryPageRequest;
|
||||||
use App\Domains\Catalog\Requests\FeaturedGroupPageRequest;
|
use App\Domains\Catalog\Requests\FeaturedGroupPageRequest;
|
||||||
use App\Domains\Catalog\Requests\SearchCatalogItemsRequest;
|
use App\Domains\Catalog\Requests\SearchCatalogItemsRequest;
|
||||||
use App\Domains\Catalog\Requests\StoreCatalogItemRequest;
|
use App\Domains\Catalog\Requests\StoreCatalogItemRequest;
|
||||||
@@ -57,6 +59,32 @@ class CatalogController extends Controller
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function category(
|
||||||
|
CategoryPageRequest $request,
|
||||||
|
Tenant $tenant,
|
||||||
|
Category $category,
|
||||||
|
CatalogService $catalogService,
|
||||||
|
): AnonymousResourceCollection {
|
||||||
|
abort_unless($category->tenant_code === $tenant->codigo, 404);
|
||||||
|
|
||||||
|
return CatalogSearchItemResource::collection(
|
||||||
|
$catalogService->categoryItems(
|
||||||
|
$tenant,
|
||||||
|
$category,
|
||||||
|
$tenant->search_items_per_page,
|
||||||
|
(int) $request->validated('page', 1),
|
||||||
|
)
|
||||||
|
)->additional([
|
||||||
|
'category' => [
|
||||||
|
'id' => $category->id,
|
||||||
|
'nombre' => $category->nombre,
|
||||||
|
'categoria_id' => $category->categoria_id,
|
||||||
|
],
|
||||||
|
'layout' => $tenant->search_product_layout->value,
|
||||||
|
'group_layout' => $tenant->search_group_layout->value,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
public function featuredGroupItems(
|
public function featuredGroupItems(
|
||||||
FeaturedGroupPageRequest $request,
|
FeaturedGroupPageRequest $request,
|
||||||
Tenant $tenant,
|
Tenant $tenant,
|
||||||
|
|||||||
21
app/Domains/Catalog/Requests/CategoryPageRequest.php
Normal file
21
app/Domains/Catalog/Requests/CategoryPageRequest.php
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Catalog\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class CategoryPageRequest extends FormRequest
|
||||||
|
{
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return array<string, list<string>> */
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'page' => ['sometimes', 'integer', 'min:1'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ use App\Domains\Attachable\Services\AttachmentService;
|
|||||||
use App\Domains\Catalog\Enums\CatalogItemType;
|
use App\Domains\Catalog\Enums\CatalogItemType;
|
||||||
use App\Domains\Catalog\Models\Attribute;
|
use App\Domains\Catalog\Models\Attribute;
|
||||||
use App\Domains\Catalog\Models\CatalogItem;
|
use App\Domains\Catalog\Models\CatalogItem;
|
||||||
|
use App\Domains\Catalog\Models\Category;
|
||||||
use App\Domains\Catalog\Models\Inventory;
|
use App\Domains\Catalog\Models\Inventory;
|
||||||
use App\Domains\Catalog\Models\ItemAttribute;
|
use App\Domains\Catalog\Models\ItemAttribute;
|
||||||
use App\Domains\Catalog\Models\Variant;
|
use App\Domains\Catalog\Models\Variant;
|
||||||
@@ -210,6 +211,29 @@ class CatalogService
|
|||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return LengthAwarePaginator<CatalogItem> */
|
||||||
|
public function categoryItems(
|
||||||
|
Tenant $tenant,
|
||||||
|
Category $category,
|
||||||
|
int $perPage,
|
||||||
|
int $page,
|
||||||
|
): LengthAwarePaginator {
|
||||||
|
return CatalogItem::query()
|
||||||
|
->where('tenant_code', $tenant->codigo)
|
||||||
|
->where('category_id', $category->id)
|
||||||
|
->with([
|
||||||
|
'attachments',
|
||||||
|
'inventory',
|
||||||
|
'variants.inventory',
|
||||||
|
'variants.attachments',
|
||||||
|
'variants.definitions.itemAttribute.attribute',
|
||||||
|
'bundleComponents.catalogItem',
|
||||||
|
'bundleComponents.variant.catalogItem',
|
||||||
|
])
|
||||||
|
->orderBy('nombre')
|
||||||
|
->paginate(perPage: $perPage, pageName: 'page', page: $page);
|
||||||
|
}
|
||||||
|
|
||||||
public function delete(CatalogItem $catalogItem): void
|
public function delete(CatalogItem $catalogItem): void
|
||||||
{
|
{
|
||||||
DB::transaction(function () use ($catalogItem): void {
|
DB::transaction(function () use ($catalogItem): void {
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ Route::prefix('tenants/{tenant:codigo}')->group(function (): void {
|
|||||||
->name('catalog.featured-groups.items.index');
|
->name('catalog.featured-groups.items.index');
|
||||||
Route::get('catalog-items', [CatalogController::class, 'search'])
|
Route::get('catalog-items', [CatalogController::class, 'search'])
|
||||||
->name('catalog-items.index');
|
->name('catalog-items.index');
|
||||||
|
Route::get('categories/{category}', [CatalogController::class, 'category'])
|
||||||
|
->name('categories.show');
|
||||||
Route::get('catalog-items/{catalogItem}', [CatalogController::class, 'show']);
|
Route::get('catalog-items/{catalogItem}', [CatalogController::class, 'show']);
|
||||||
Route::post('catalog-items', [CatalogController::class, 'store']);
|
Route::post('catalog-items', [CatalogController::class, 'store']);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -33,5 +33,18 @@ class CategorySeeder extends Seeder
|
|||||||
'tenant_code' => $tenant->codigo,
|
'tenant_code' => $tenant->codigo,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$indumentaria = Category::updateOrCreate(
|
||||||
|
[
|
||||||
|
'nombre' => 'Indumentaria',
|
||||||
|
'tenant_code' => $tenant->codigo,
|
||||||
|
],
|
||||||
|
['categoria_id' => null],
|
||||||
|
);
|
||||||
|
|
||||||
|
Category::query()
|
||||||
|
->where('nombre', 'Remeras')
|
||||||
|
->where('tenant_code', $tenant->codigo)
|
||||||
|
->update(['categoria_id' => $indumentaria->id]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
123
tests/Feature/Catalog/CategoryDetailTest.php
Normal file
123
tests/Feature/Catalog/CategoryDetailTest.php
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Catalog;
|
||||||
|
|
||||||
|
use App\Domains\Catalog\Enums\GroupLayout;
|
||||||
|
use App\Domains\Catalog\Enums\ProductLayout;
|
||||||
|
use App\Domains\Catalog\Models\CatalogItem;
|
||||||
|
use App\Domains\Catalog\Models\Category;
|
||||||
|
use App\Domains\Catalog\Models\Inventory;
|
||||||
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class CategoryDetailTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
public function test_it_returns_category_products_paginated_with_the_tenant_search_layout(): void
|
||||||
|
{
|
||||||
|
$tenant = $this->createTenant('category-detail');
|
||||||
|
$tenant->update([
|
||||||
|
'search_product_layout' => ProductLayout::Row->value,
|
||||||
|
'search_group_layout' => GroupLayout::SimpleVertical->value,
|
||||||
|
'search_items_per_page' => 2,
|
||||||
|
]);
|
||||||
|
$category = $this->createCategory($tenant, 'Remeras');
|
||||||
|
$otherCategory = $this->createCategory($tenant, 'Pantalones');
|
||||||
|
|
||||||
|
$this->createCatalogItem($tenant, $category, 'Remera C');
|
||||||
|
$firstItem = $this->createCatalogItem($tenant, $category, 'Remera A');
|
||||||
|
$secondItem = $this->createCatalogItem($tenant, $category, 'Remera B');
|
||||||
|
$this->createCatalogItem($tenant, $otherCategory, 'Pantalón');
|
||||||
|
|
||||||
|
$this->getJson("/api/tenants/{$tenant->codigo}/categories/{$category->id}")
|
||||||
|
->assertOk()
|
||||||
|
->assertJsonPath('category.id', $category->id)
|
||||||
|
->assertJsonPath('category.nombre', 'Remeras')
|
||||||
|
->assertJsonPath('category.categoria_id', null)
|
||||||
|
->assertJsonPath('layout', ProductLayout::Row->value)
|
||||||
|
->assertJsonPath('group_layout', GroupLayout::SimpleVertical->value)
|
||||||
|
->assertJsonPath('meta.current_page', 1)
|
||||||
|
->assertJsonPath('meta.per_page', 2)
|
||||||
|
->assertJsonPath('meta.total', 3)
|
||||||
|
->assertJsonCount(2, 'data')
|
||||||
|
->assertJsonPath('data.0.id', $firstItem->id)
|
||||||
|
->assertJsonPath('data.1.id', $secondItem->id)
|
||||||
|
->assertJsonMissing(['nombre' => 'Pantalón']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_it_returns_an_empty_paginated_response_for_a_category_without_products(): void
|
||||||
|
{
|
||||||
|
$tenant = $this->createTenant('empty-category');
|
||||||
|
$category = $this->createCategory($tenant, 'Sin productos');
|
||||||
|
|
||||||
|
$this->getJson("/api/tenants/{$tenant->codigo}/categories/{$category->id}")
|
||||||
|
->assertOk()
|
||||||
|
->assertJsonPath('category.id', $category->id)
|
||||||
|
->assertJsonPath('meta.total', 0)
|
||||||
|
->assertJsonPath('meta.current_page', 1)
|
||||||
|
->assertJsonCount(0, 'data');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_it_rejects_categories_from_another_tenant(): void
|
||||||
|
{
|
||||||
|
$tenant = $this->createTenant('category-owner');
|
||||||
|
$otherTenant = $this->createTenant('category-foreign');
|
||||||
|
$foreignCategory = $this->createCategory($otherTenant, 'Ajena');
|
||||||
|
|
||||||
|
$this->getJson("/api/tenants/{$tenant->codigo}/categories/{$foreignCategory->id}")
|
||||||
|
->assertNotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_it_validates_the_page(): void
|
||||||
|
{
|
||||||
|
$tenant = $this->createTenant('category-page');
|
||||||
|
$category = $this->createCategory($tenant, 'Remeras');
|
||||||
|
|
||||||
|
$this->getJson("/api/tenants/{$tenant->codigo}/categories/{$category->id}?page=0")
|
||||||
|
->assertUnprocessable()
|
||||||
|
->assertJsonValidationErrors('page');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createTenant(string $code): Tenant
|
||||||
|
{
|
||||||
|
return Tenant::query()->create([
|
||||||
|
'codigo' => $code,
|
||||||
|
'nombre' => ucfirst($code),
|
||||||
|
'dominio' => "{$code}.local",
|
||||||
|
'primary_color' => '#000000',
|
||||||
|
'secondary_color' => '#000000',
|
||||||
|
'danger_color' => '#000000',
|
||||||
|
'success_color' => '#000000',
|
||||||
|
'header_bg_color' => '#000000',
|
||||||
|
'footer_bg_color' => '#000000',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createCategory(Tenant $tenant, string $name): Category
|
||||||
|
{
|
||||||
|
return Category::query()->create([
|
||||||
|
'tenant_code' => $tenant->codigo,
|
||||||
|
'nombre' => $name,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createCatalogItem(
|
||||||
|
Tenant $tenant,
|
||||||
|
Category $category,
|
||||||
|
string $name,
|
||||||
|
): CatalogItem {
|
||||||
|
$inventory = Inventory::query()->create(['real_stock' => 10]);
|
||||||
|
|
||||||
|
return CatalogItem::query()->create([
|
||||||
|
'tenant_code' => $tenant->codigo,
|
||||||
|
'category_id' => $category->id,
|
||||||
|
'inventory_id' => $inventory->id,
|
||||||
|
'slug' => str($name)->slug()->toString(),
|
||||||
|
'nombre' => $name,
|
||||||
|
'descripcion' => "{$name} description",
|
||||||
|
'precio' => 100,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user