feat(forms): implement MerchandiseFormController, MerchandiseFormService, and MerchandiseFormResource for managing merchandise options
This commit is contained in:
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Forms\Controllers\AdminApp;
|
||||||
|
|
||||||
|
use App\Domains\Forms\Resources\MerchandiseFormResource;
|
||||||
|
use App\Domains\Forms\Services\MerchandiseFormService;
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class MerchandiseFormController extends Controller
|
||||||
|
{
|
||||||
|
public function __construct(protected MerchandiseFormService $merchandiseFormService) {}
|
||||||
|
|
||||||
|
public function __invoke(Request $request): MerchandiseFormResource
|
||||||
|
{
|
||||||
|
return MerchandiseFormResource::make(
|
||||||
|
$this->merchandiseFormService->get(
|
||||||
|
$request->user('sanctum')->tenant()->firstOrFail()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
32
app/Domains/Forms/Resources/MerchandiseFormResource.php
Normal file
32
app/Domains/Forms/Resources/MerchandiseFormResource.php
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Forms\Resources;
|
||||||
|
|
||||||
|
use App\Domains\Catalog\Models\AttributeOption;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
|
class MerchandiseFormResource extends JsonResource
|
||||||
|
{
|
||||||
|
/** @return array<string, mixed> */
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'colors' => $this->options($this->resource['colors']),
|
||||||
|
'sizes' => $this->options($this->resource['sizes']),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Collection<int, AttributeOption> $options
|
||||||
|
* @return Collection<int, array{value: string, label: string}>
|
||||||
|
*/
|
||||||
|
private function options(Collection $options): Collection
|
||||||
|
{
|
||||||
|
return $options->map(fn (AttributeOption $option): array => [
|
||||||
|
'value' => $option->value,
|
||||||
|
'label' => $option->label,
|
||||||
|
])->values();
|
||||||
|
}
|
||||||
|
}
|
||||||
32
app/Domains/Forms/Services/MerchandiseFormService.php
Normal file
32
app/Domains/Forms/Services/MerchandiseFormService.php
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Forms\Services;
|
||||||
|
|
||||||
|
use App\Domains\Catalog\Models\Attribute;
|
||||||
|
use App\Domains\Catalog\Models\AttributeOption;
|
||||||
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
|
|
||||||
|
class MerchandiseFormService
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return array{
|
||||||
|
* colors: Collection<int, AttributeOption>,
|
||||||
|
* sizes: Collection<int, AttributeOption>
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
public function get(Tenant $tenant): array
|
||||||
|
{
|
||||||
|
$attributes = Attribute::query()
|
||||||
|
->where('tenant_codigo', $tenant->codigo)
|
||||||
|
->whereIn('codigo', ['color', 'talle'])
|
||||||
|
->with('options')
|
||||||
|
->get()
|
||||||
|
->keyBy('codigo');
|
||||||
|
|
||||||
|
return [
|
||||||
|
'colors' => $attributes->get('color')?->options ?? new Collection,
|
||||||
|
'sizes' => $attributes->get('talle')?->options ?? new Collection,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,6 +19,7 @@ Bajo `/v1/adminapp/forms`, con `auth:sanctum` y `adminapp.tenant`:
|
|||||||
- `GET /event`.
|
- `GET /event`.
|
||||||
- `GET /sale`.
|
- `GET /sale`.
|
||||||
- `GET /staff`.
|
- `GET /staff`.
|
||||||
|
- `GET /fiesta-futbol-infantil/merchandise`: opciones de color y talle del tenant para merchandising.
|
||||||
|
|
||||||
## Dependencias
|
## Dependencias
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Domains\Forms\Controllers\AdminApp\EventFormController;
|
use App\Domains\Forms\Controllers\AdminApp\EventFormController;
|
||||||
|
use App\Domains\Forms\Controllers\AdminApp\MerchandiseFormController;
|
||||||
use App\Domains\Forms\Controllers\AdminApp\SaleFormController;
|
use App\Domains\Forms\Controllers\AdminApp\SaleFormController;
|
||||||
use App\Domains\Forms\Controllers\AdminApp\StaffFormController;
|
use App\Domains\Forms\Controllers\AdminApp\StaffFormController;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
@@ -11,4 +12,8 @@ Route::prefix('v1/adminapp/forms')
|
|||||||
Route::get('event', EventFormController::class);
|
Route::get('event', EventFormController::class);
|
||||||
Route::get('sale', SaleFormController::class);
|
Route::get('sale', SaleFormController::class);
|
||||||
Route::get('staff', StaffFormController::class);
|
Route::get('staff', StaffFormController::class);
|
||||||
|
Route::get(
|
||||||
|
'fiesta-futbol-infantil/merchandise',
|
||||||
|
MerchandiseFormController::class
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
102
tests/Feature/Forms/AdminAppMerchandiseFormControllerTest.php
Normal file
102
tests/Feature/Forms/AdminAppMerchandiseFormControllerTest.php
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Forms;
|
||||||
|
|
||||||
|
use App\Domains\Auth\Models\User;
|
||||||
|
use App\Domains\Authorization\Enums\RoleCode;
|
||||||
|
use App\Domains\Catalog\Models\Attribute;
|
||||||
|
use App\Domains\Shared\Enums\FieldType;
|
||||||
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
|
use App\Domains\Tenant\Models\WebsiteType;
|
||||||
|
use Database\Seeders\AuthorizationSeeder;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Laravel\Sanctum\Sanctum;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class AdminAppMerchandiseFormControllerTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->seed(AuthorizationSeeder::class);
|
||||||
|
WebsiteType::query()->create([
|
||||||
|
'codigo' => 'onticket',
|
||||||
|
'nombre' => 'OnTicket',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_authentication_is_required(): void
|
||||||
|
{
|
||||||
|
$this->getJson('/api/v1/adminapp/forms/fiesta-futbol-infantil/merchandise')
|
||||||
|
->assertUnauthorized();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_it_returns_color_and_size_options_for_the_authenticated_tenant(): void
|
||||||
|
{
|
||||||
|
$tenant = $this->createTenant('fiesta');
|
||||||
|
$otherTenant = $this->createTenant('other');
|
||||||
|
$this->createAttribute($tenant, 'color', [
|
||||||
|
['value' => 'verde', 'label' => 'Verde', 'sort_order' => 2],
|
||||||
|
['value' => 'blanco', 'label' => 'Blanco', 'sort_order' => 1],
|
||||||
|
]);
|
||||||
|
$this->createAttribute($tenant, 'talle', [
|
||||||
|
['value' => 'S', 'label' => 'S', 'sort_order' => 1],
|
||||||
|
['value' => 'M', 'label' => 'M', 'sort_order' => 2],
|
||||||
|
]);
|
||||||
|
$this->createAttribute($otherTenant, 'color', [
|
||||||
|
['value' => 'negro', 'label' => 'Negro', 'sort_order' => 1],
|
||||||
|
]);
|
||||||
|
|
||||||
|
Sanctum::actingAs(User::factory()->create([
|
||||||
|
'rol_codigo' => RoleCode::AdminApp->value,
|
||||||
|
'tenant_codigo' => $tenant->codigo,
|
||||||
|
]));
|
||||||
|
|
||||||
|
$this->getJson('/api/v1/adminapp/forms/fiesta-futbol-infantil/merchandise')
|
||||||
|
->assertOk()
|
||||||
|
->assertJsonCount(2, 'data.colors')
|
||||||
|
->assertJsonCount(2, 'data.sizes')
|
||||||
|
->assertJsonPath('data.colors.0.value', 'blanco')
|
||||||
|
->assertJsonPath('data.colors.0.label', 'Blanco')
|
||||||
|
->assertJsonPath('data.colors.1.value', 'verde')
|
||||||
|
->assertJsonPath('data.sizes.0.value', 'S')
|
||||||
|
->assertJsonPath('data.sizes.1.value', 'M')
|
||||||
|
->assertJsonMissing(['value' => 'negro']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_a_customer_cannot_get_the_merchandise_form(): void
|
||||||
|
{
|
||||||
|
Sanctum::actingAs(User::factory()->create([
|
||||||
|
'rol_codigo' => RoleCode::User->value,
|
||||||
|
]));
|
||||||
|
|
||||||
|
$this->getJson('/api/v1/adminapp/forms/fiesta-futbol-infantil/merchandise')
|
||||||
|
->assertForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createTenant(string $code): Tenant
|
||||||
|
{
|
||||||
|
return Tenant::query()->create([
|
||||||
|
'codigo' => $code,
|
||||||
|
'nombre' => ucfirst($code),
|
||||||
|
'dominio' => "{$code}.test",
|
||||||
|
'website_type_code' => 'onticket',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param array<int, array{value: string, label: string, sort_order: int}> $options */
|
||||||
|
private function createAttribute(Tenant $tenant, string $code, array $options): void
|
||||||
|
{
|
||||||
|
$attribute = Attribute::query()->create([
|
||||||
|
'tenant_codigo' => $tenant->codigo,
|
||||||
|
'codigo' => $code,
|
||||||
|
'nombre' => ucfirst($code),
|
||||||
|
'type' => FieldType::Select,
|
||||||
|
'is_required' => true,
|
||||||
|
]);
|
||||||
|
$attribute->options()->createMany($options);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user