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

@@ -10,6 +10,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
#[Fillable([
'cart_id',
@@ -28,9 +29,13 @@ class Purchase extends Model
use HasFactory;
public const STATUS_CREATED = 'created';
public const STATUS_PENDING_PAYMENT = 'pending_payment';
public const STATUS_PAID = 'paid';
public const STATUS_CANCELLED = 'cancelled';
public const STATUS_REJECTED = 'rejected';
protected $table = 'compras';
@@ -77,7 +82,7 @@ class Purchase extends Model
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasOne<TelepagosQr, $this>
* @return HasOne<TelepagosQr, $this>
*/
public function telepagosQr()
{
@@ -113,7 +118,7 @@ class Purchase extends Model
$cart = $this->relationLoaded('cart')
? $this->getRelation('cart')
: $this->cart()->with('items.variant.product')->first();
: $this->cart()->with('items.buyable')->first();
if (! $cart) {
return 0.0;