feat: refactor cart and purchase handling to support polymorphic buyable types and improve code readability

This commit is contained in:
2026-07-16 09:55:38 -03:00
parent c05206cc51
commit 6fc6ed1fac
14 changed files with 196 additions and 79 deletions

View File

@@ -2,7 +2,10 @@
namespace App\Domains\Cart\Requests;
use App\Domains\Bundle\Models\Bundle;
use App\Domains\Catalog\Models\ProductVariant;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class AddCartItemRequest extends FormRequest
{
@@ -17,7 +20,7 @@ class AddCartItemRequest extends FormRequest
public function rules(): array
{
return [
'buyable_type' => ['required', 'string', \Illuminate\Validation\Rule::in(['variant', 'bundle'])],
'buyable_type' => ['required', 'string', Rule::in(['variant', 'bundle'])],
'buyable_id' => ['required', 'integer'],
'cantidad' => ['required', 'integer', 'min:1'],
];
@@ -26,8 +29,8 @@ class AddCartItemRequest extends FormRequest
public function mappedBuyableType(): string
{
return match ($this->input('buyable_type')) {
'variant' => \App\Domains\Catalog\Models\ProductVariant::class,
'bundle' => \App\Domains\Bundle\Models\Bundle::class,
'variant' => ProductVariant::class,
'bundle' => Bundle::class,
default => throw new \InvalidArgumentException('Invalid buyable type'),
};
}

View File

@@ -18,6 +18,8 @@ class UpdateCartItemQuantityRequest extends FormRequest
{
return [
'cantidad' => ['required', 'integer', 'min:1'],
'buyable_type' => ['prohibited'],
'buyable_id' => ['prohibited'],
];
}
}