feat(events): associate purchases and integrations with events
This commit is contained in:
@@ -2,13 +2,14 @@
|
||||
|
||||
namespace App\Domains\Commerce\Purchase\Models;
|
||||
|
||||
use App\Domains\Core\Auth\Models\User;
|
||||
use App\Domains\Commerce\Cart\Models\Cart;
|
||||
use App\Domains\Commerce\Catalog\Models\StockReservation;
|
||||
use App\Shared\Logging\Models\Concerns\LogsValueChanges;
|
||||
use App\Domains\Commerce\Purchase\Events\PurchasePaid;
|
||||
use App\Domains\Core\Auth\Models\User;
|
||||
use App\Domains\Core\Tenant\Models\Tenant;
|
||||
use App\Domains\Ticketing\Event\Models\Event;
|
||||
use App\Domains\Ticketing\Ticket\Models\Ticket;
|
||||
use App\Shared\Logging\Models\Concerns\LogsValueChanges;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@@ -21,6 +22,7 @@ use Illuminate\Support\Facades\DB;
|
||||
#[Fillable([
|
||||
'cart_id',
|
||||
'stock_reservation_id',
|
||||
'event_id',
|
||||
'tenant_codigo',
|
||||
'user_id',
|
||||
'status',
|
||||
@@ -149,6 +151,7 @@ class Purchase extends Model
|
||||
return [
|
||||
'cart_id' => 'integer',
|
||||
'stock_reservation_id' => 'integer',
|
||||
'event_id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
'total' => 'decimal:2',
|
||||
];
|
||||
@@ -162,6 +165,12 @@ class Purchase extends Model
|
||||
return $this->belongsTo(Tenant::class, 'tenant_codigo', 'codigo');
|
||||
}
|
||||
|
||||
/** @return BelongsTo<Event, $this> */
|
||||
public function event(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Event::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<User, $this>
|
||||
*/
|
||||
|
||||
@@ -36,11 +36,11 @@ class PurchaseResource extends JsonResource
|
||||
=== StorefrontWebsiteType::CHECKOUT_SUMMARY_EVENT;
|
||||
/** @var Event|null $checkoutEvent */
|
||||
$checkoutEvent = $usesEventSummary
|
||||
? $items
|
||||
? ($this->event ?? $items
|
||||
->map(fn (PurchaseItem $item) => $item->sourceCatalogItem?->event)
|
||||
->filter()
|
||||
->unique('id')
|
||||
->first()
|
||||
->first())
|
||||
: null;
|
||||
|
||||
$subtotal = $items->isNotEmpty()
|
||||
@@ -62,6 +62,7 @@ class PurchaseResource extends JsonResource
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'cart_id' => $this->cart_id,
|
||||
'event_id' => $this->event_id,
|
||||
'tenant_codigo' => $this->tenant_codigo,
|
||||
'user_id' => $this->user_id,
|
||||
'created_at' => $this->created_at,
|
||||
|
||||
@@ -14,6 +14,7 @@ class PurchaseResponseLoader
|
||||
=== StorefrontWebsiteType::CHECKOUT_SUMMARY_EVENT;
|
||||
$relations = [
|
||||
'tenant',
|
||||
'event.attachment',
|
||||
'items.sourceCatalogItem.event.attachment',
|
||||
'stockReservation',
|
||||
];
|
||||
|
||||
@@ -206,6 +206,7 @@ class StartCheckoutService
|
||||
fn (array $line): float => $line['selection']->getPrice() * $line['quantity'],
|
||||
),
|
||||
$cart->getKey(),
|
||||
$cartItems->first()?->catalogItem?->event_id,
|
||||
);
|
||||
$cart->update(['current_purchase_id' => $purchase->getKey()]);
|
||||
$this->reservations->attachToPurchase($cart, $purchase, $this->checkoutExpiration());
|
||||
@@ -273,6 +274,7 @@ class StartCheckoutService
|
||||
$purchaseData,
|
||||
$cart->getTotalAmount(),
|
||||
$cart->getKey(),
|
||||
$cartItems->first()?->catalogItem?->event_id,
|
||||
);
|
||||
$cart->update(['current_purchase_id' => $purchase->getKey()]);
|
||||
$this->reservations->attachToPurchase($cart, $purchase, $this->checkoutExpiration());
|
||||
@@ -410,10 +412,12 @@ class StartCheckoutService
|
||||
array $purchaseData,
|
||||
float $total,
|
||||
?int $cartId,
|
||||
?int $eventId,
|
||||
): Purchase {
|
||||
return Purchase::query()->create([
|
||||
...$purchaseData,
|
||||
'cart_id' => $cartId,
|
||||
'event_id' => $eventId,
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
'user_id' => $userId,
|
||||
'status' => Purchase::STATUS_CREATED,
|
||||
|
||||
Reference in New Issue
Block a user