feat(events): authorize event editing and date changes by assigned event
This commit is contained in:
@@ -2,18 +2,19 @@
|
||||
|
||||
namespace App\Domains\Ticketing\Event\Services;
|
||||
|
||||
use App\Domains\Core\Auth\Models\User;
|
||||
use App\Domains\Commerce\Cart\Services\InvalidateEventDateCartsService;
|
||||
use App\Domains\Commerce\Catalog\Models\Variant;
|
||||
use App\Domains\Commerce\Catalog\Services\StockReservationService;
|
||||
use App\Domains\Commerce\Catalog\Services\VariantReplacementService;
|
||||
use App\Domains\Core\Auth\Models\User;
|
||||
use App\Domains\Core\Auth\Services\EventScopeService;
|
||||
use App\Domains\Core\Tenant\Models\Tenant;
|
||||
use App\Domains\Ticketing\Event\Enums\EventDateChangeType;
|
||||
use App\Domains\Ticketing\Event\Events\EventDateRescheduled;
|
||||
use App\Domains\Ticketing\Event\Events\EventDateSuspended;
|
||||
use App\Domains\Ticketing\Event\Models\Event;
|
||||
use App\Domains\Ticketing\Event\Models\EventDate;
|
||||
use App\Domains\Ticketing\Event\Models\EventDateChange;
|
||||
use App\Domains\Ticketing\Event\Models\Event;
|
||||
use App\Domains\Core\Tenant\Models\Tenant;
|
||||
use App\Domains\Ticketing\Ticket\Models\Ticket;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -34,16 +35,16 @@ class EventService
|
||||
private readonly InvalidateEventDateCartsService $invalidateEventDateCarts,
|
||||
) {}
|
||||
|
||||
public function forTenant(Tenant $tenant): Event
|
||||
public function forTenant(Tenant $tenant, ?int $eventId = null): Event
|
||||
{
|
||||
return $tenant->activeEvent->load(['dates.validityTime', 'socialMedia']);
|
||||
return $this->resolveEvent($tenant, $eventId)->load(['dates.validityTime', 'socialMedia']);
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $data */
|
||||
public function updateForTenant(Tenant $tenant, array $data): Event
|
||||
public function updateForTenant(Tenant $tenant, array $data, ?int $eventId = null): Event
|
||||
{
|
||||
return DB::transaction(function () use ($tenant, $data): Event {
|
||||
$event = $tenant->activeEvent;
|
||||
return DB::transaction(function () use ($tenant, $data, $eventId): Event {
|
||||
$event = $this->resolveEvent($tenant, $eventId);
|
||||
$event->update([
|
||||
'title' => $data['title'],
|
||||
'location' => $data['location'],
|
||||
@@ -69,10 +70,10 @@ class EventService
|
||||
}
|
||||
|
||||
/** @param array{date: string, start_time: string, end_time: string} $data */
|
||||
public function createDateForTenant(Tenant $tenant, array $data): EventDate
|
||||
public function createDateForTenant(Tenant $tenant, array $data, ?int $eventId = null): EventDate
|
||||
{
|
||||
return DB::transaction(function () use ($tenant, $data): EventDate {
|
||||
$event = $tenant->activeEvent;
|
||||
return DB::transaction(function () use ($tenant, $data, $eventId): EventDate {
|
||||
$event = $this->resolveEvent($tenant, $eventId);
|
||||
$attributes = $this->dateAttributes($data);
|
||||
|
||||
if ($event->dates()->where($attributes)->exists()) {
|
||||
@@ -93,7 +94,7 @@ class EventService
|
||||
?User $createdBy = null,
|
||||
): EventDate {
|
||||
return DB::transaction(function () use ($tenant, $eventDate, $data, $createdBy): EventDate {
|
||||
$source = $this->lockedDateForTenant($tenant, $eventDate);
|
||||
$source = $this->lockedDateForTenant($tenant, $eventDate, $createdBy);
|
||||
|
||||
if ($source->suspended_at !== null) {
|
||||
throw ValidationException::withMessages([
|
||||
@@ -172,7 +173,7 @@ class EventService
|
||||
?User $createdBy = null,
|
||||
): EventDate {
|
||||
return DB::transaction(function () use ($tenant, $eventDate, $createdBy): EventDate {
|
||||
$date = $this->lockedDateForTenant($tenant, $eventDate);
|
||||
$date = $this->lockedDateForTenant($tenant, $eventDate, $createdBy);
|
||||
|
||||
if ($date->rescheduled_to_event_date_id !== null) {
|
||||
throw ValidationException::withMessages([
|
||||
@@ -216,10 +217,18 @@ class EventService
|
||||
});
|
||||
}
|
||||
|
||||
private function lockedDateForTenant(Tenant $tenant, EventDate $eventDate): EventDate
|
||||
private function resolveEvent(Tenant $tenant, ?int $eventId): Event
|
||||
{
|
||||
return Event::query()->where('tenant_code', $tenant->codigo)
|
||||
->findOrFail($eventId ?? $tenant->active_event_id);
|
||||
}
|
||||
|
||||
private function lockedDateForTenant(Tenant $tenant, EventDate $eventDate, ?User $actor = null): EventDate
|
||||
{
|
||||
return $tenant->eventDates()
|
||||
->whereKey($eventDate->getKey())
|
||||
->when($actor !== null && ! $actor->isTenantAdministrator(),
|
||||
fn ($query) => $query->where('event_id', app(EventScopeService::class)->eventId($actor)))
|
||||
->lockForUpdate()
|
||||
->firstOrFail();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user