From 96df431d6042cde8bd6869b017355d6df17e364d Mon Sep 17 00:00:00 2001 From: ncoronel Date: Fri, 11 Sep 2026 12:28:51 -0300 Subject: [PATCH] feat(event): implement admin date creation, rescheduling, and cancellation endpoints --- .../Controllers/AdminApp/EventController.php | 37 +++ .../Requests/RescheduleEventDateRequest.php | 21 ++ .../Event/Requests/StoreEventDateRequest.php | 23 ++ .../Event/Requests/UpdateEventRequest.php | 5 - .../Event/Resources/EventDateResource.php | 28 +++ app/Domains/Event/Resources/EventResource.php | 10 +- app/Domains/Event/Services/EventService.php | 202 +++++++++++++-- app/Domains/Event/routes/adminapp.php | 3 + .../Event/AdminAppEventControllerTest.php | 231 +++++++++++++----- 9 files changed, 462 insertions(+), 98 deletions(-) create mode 100644 app/Domains/Event/Requests/RescheduleEventDateRequest.php create mode 100644 app/Domains/Event/Requests/StoreEventDateRequest.php create mode 100644 app/Domains/Event/Resources/EventDateResource.php diff --git a/app/Domains/Event/Controllers/AdminApp/EventController.php b/app/Domains/Event/Controllers/AdminApp/EventController.php index b6036d9..eb626b5 100644 --- a/app/Domains/Event/Controllers/AdminApp/EventController.php +++ b/app/Domains/Event/Controllers/AdminApp/EventController.php @@ -2,7 +2,11 @@ namespace App\Domains\Event\Controllers\AdminApp; +use App\Domains\Event\Models\EventDate; +use App\Domains\Event\Requests\RescheduleEventDateRequest; +use App\Domains\Event\Requests\StoreEventDateRequest; use App\Domains\Event\Requests\UpdateEventRequest; +use App\Domains\Event\Resources\EventDateResource; use App\Domains\Event\Resources\EventResource; use App\Domains\Event\Services\EventService; use App\Http\Controllers\Controller; @@ -28,4 +32,37 @@ class EventController extends Controller ) ); } + + public function storeDate(StoreEventDateRequest $request): EventDateResource + { + return EventDateResource::make( + $this->eventService->createDateForTenant( + $request->user()->tenant()->firstOrFail(), + $request->validated(), + ) + ); + } + + public function rescheduleDate( + RescheduleEventDateRequest $request, + EventDate $eventDate, + ): EventDateResource { + return EventDateResource::make( + $this->eventService->rescheduleDateForTenant( + $request->user()->tenant()->firstOrFail(), + $eventDate, + $request->validated(), + ) + ); + } + + public function cancelDate(Request $request, EventDate $eventDate): EventDateResource + { + return EventDateResource::make( + $this->eventService->cancelDateForTenant( + $request->user()->tenant()->firstOrFail(), + $eventDate, + ) + ); + } } diff --git a/app/Domains/Event/Requests/RescheduleEventDateRequest.php b/app/Domains/Event/Requests/RescheduleEventDateRequest.php new file mode 100644 index 0000000..3573c8a --- /dev/null +++ b/app/Domains/Event/Requests/RescheduleEventDateRequest.php @@ -0,0 +1,21 @@ + */ + public function rules(): array + { + return [ + 'date' => ['required', 'date_format:Y-m-d'], + ]; + } +} diff --git a/app/Domains/Event/Requests/StoreEventDateRequest.php b/app/Domains/Event/Requests/StoreEventDateRequest.php new file mode 100644 index 0000000..74bbe0e --- /dev/null +++ b/app/Domains/Event/Requests/StoreEventDateRequest.php @@ -0,0 +1,23 @@ + */ + public function rules(): array + { + return [ + 'date' => ['required', 'date_format:Y-m-d'], + 'start_time' => ['required', 'date_format:H:i'], + 'end_time' => ['required', 'date_format:H:i'], + ]; + } +} diff --git a/app/Domains/Event/Requests/UpdateEventRequest.php b/app/Domains/Event/Requests/UpdateEventRequest.php index 987dccb..adf383d 100644 --- a/app/Domains/Event/Requests/UpdateEventRequest.php +++ b/app/Domains/Event/Requests/UpdateEventRequest.php @@ -19,11 +19,6 @@ class UpdateEventRequest extends FormRequest return [ 'title' => ['required', 'string', 'max:255'], 'location' => ['required', 'string', 'max:255'], - 'dates' => ['required', 'array', 'min:1'], - 'dates.*' => ['required', 'array:date,start_time,end_time'], - 'dates.*.date' => ['required', 'date_format:Y-m-d', 'distinct'], - 'dates.*.start_time' => ['required', 'date_format:H:i'], - 'dates.*.end_time' => ['required', 'date_format:H:i'], 'social_media' => ['sometimes', 'array'], 'social_media.*' => ['required', 'array:code,url,orden'], 'social_media.*.code' => [ diff --git a/app/Domains/Event/Resources/EventDateResource.php b/app/Domains/Event/Resources/EventDateResource.php new file mode 100644 index 0000000..4268940 --- /dev/null +++ b/app/Domains/Event/Resources/EventDateResource.php @@ -0,0 +1,28 @@ + */ + public function toArray(Request $request): array + { + return [ + 'id' => $this->id, + 'validity_time_id' => $this->validity_time_id, + 'validity_time' => ValidityTimeResource::make($this->whenLoaded('validityTime')), + 'date' => $this->date->format('Y-m-d'), + 'start_time' => substr($this->time_start, 0, 5), + 'end_time' => substr($this->time_end, 0, 5), + 'status' => $this->status->value, + 'rescheduled_to_event_date_id' => $this->rescheduled_to_event_date_id, + 'cancelled_at' => $this->cancelled_at?->toISOString(), + ]; + } +} diff --git a/app/Domains/Event/Resources/EventResource.php b/app/Domains/Event/Resources/EventResource.php index 1dd4735..feed495 100644 --- a/app/Domains/Event/Resources/EventResource.php +++ b/app/Domains/Event/Resources/EventResource.php @@ -3,7 +3,6 @@ namespace App\Domains\Event\Resources; use App\Domains\Tenant\Models\Tenant; -use App\Domains\Ticket\Resources\ValidityTimeResource; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; @@ -19,14 +18,7 @@ class EventResource extends JsonResource 'id' => $this->id, 'title' => $this->event_title, 'location' => $this->event_location, - 'dates' => $this->eventDates->map(fn ($eventDate): array => [ - 'id' => $eventDate->id, - 'validity_time_id' => $eventDate->validity_time_id, - 'validity_time' => ValidityTimeResource::make($eventDate->validityTime), - 'date' => $eventDate->date->format('Y-m-d'), - 'start_time' => substr($eventDate->time_start, 0, 5), - 'end_time' => substr($eventDate->time_end, 0, 5), - ])->values(), + 'dates' => EventDateResource::collection($this->eventDates), 'social_media' => $this->socialMedia->map(fn ($item): array => [ 'code' => $item->code, 'url' => $item->pivot->url, diff --git a/app/Domains/Event/Services/EventService.php b/app/Domains/Event/Services/EventService.php index 3425eae..fc7bb40 100644 --- a/app/Domains/Event/Services/EventService.php +++ b/app/Domains/Event/Services/EventService.php @@ -2,7 +2,10 @@ namespace App\Domains\Event\Services; +use App\Domains\Catalog\Models\Variant; +use App\Domains\Event\Models\EventDate; use App\Domains\Tenant\Models\Tenant; +use App\Domains\Ticket\Models\Ticket; use Illuminate\Support\Facades\DB; use Illuminate\Validation\ValidationException; @@ -14,6 +17,10 @@ class EventService 'facebook_url' => 'facebook', ]; + public function __construct( + private readonly EffectiveEventDateResolver $effectiveEventDateResolver, + ) {} + public function forTenant(Tenant $tenant): Tenant { return $tenant->load(['eventDates.validityTime', 'socialMedia']); @@ -29,7 +36,6 @@ class EventService 'event_location' => $data['location'], ]); - $this->syncDates($tenant, $data['dates']); if (array_key_exists('social_media', $data)) { $this->syncSocialMedia($tenant, $data['social_media']); } else { @@ -40,41 +46,185 @@ class EventService }); } - /** @param array $dates */ - private function syncDates(Tenant $tenant, array $dates): void + /** @param array{date: string, start_time: string, end_time: string} $data */ + public function createDateForTenant(Tenant $tenant, array $data): EventDate { - $existingDates = $tenant->eventDates()->get()->values(); + return DB::transaction(function () use ($tenant, $data): EventDate { + $attributes = $this->dateAttributes($data); - foreach (array_values($dates) as $index => $date) { - $attributes = [ - 'date' => $date['date'], - 'time_start' => $date['start_time'], - 'time_end' => $date['end_time'], - ]; + if ($tenant->eventDates()->where($attributes)->exists()) { + throw ValidationException::withMessages([ + 'date' => ['La fecha y el horario ya existen.'], + ]); + } - $existingDate = $existingDates->get($index); + return $tenant->eventDates()->create($attributes)->load('validityTime'); + }); + } - if ($existingDate) { - $existingDate->update($attributes); - } else { - $tenant->eventDates()->create($attributes); + /** @param array{date: string} $data */ + public function rescheduleDateForTenant(Tenant $tenant, EventDate $eventDate, array $data): EventDate + { + return DB::transaction(function () use ($tenant, $eventDate, $data): EventDate { + $source = $this->lockedDateForTenant($tenant, $eventDate); + + if ($source->cancelled_at !== null) { + throw ValidationException::withMessages([ + 'event_date' => ['No se puede reprogramar una fecha cancelada.'], + ]); + } + + if ($source->rescheduled_to_event_date_id !== null) { + throw ValidationException::withMessages([ + 'event_date' => ['La fecha ya fue reprogramada.'], + ]); + } + + $destination = $tenant->eventDates() + ->whereDate('date', $data['date']) + ->lockForUpdate() + ->first(); + + if ($destination === null) { + $destination = $tenant->eventDates()->create([ + 'date' => $data['date'], + 'time_start' => $source->time_start, + 'time_end' => $source->time_end, + ]); + } + + if ($destination->is($source) || $this->chainContains($destination, $source)) { + throw ValidationException::withMessages([ + 'date' => ['La reprogramación generaría una referencia circular.'], + ]); + } + + if ($this->effectiveEventDateResolver->resolve($destination) === null) { + throw ValidationException::withMessages([ + 'date' => ['La fecha de destino no es utilizable.'], + ]); + } + + $source->update(['rescheduled_to_event_date_id' => $destination->getKey()]); + + return $source->fresh(['validityTime', 'rescheduledTo.validityTime']); + }); + } + + public function cancelDateForTenant(Tenant $tenant, EventDate $eventDate): EventDate + { + return DB::transaction(function () use ($tenant, $eventDate): EventDate { + $date = $this->lockedDateForTenant($tenant, $eventDate); + + if ($date->rescheduled_to_event_date_id !== null) { + throw ValidationException::withMessages([ + 'event_date' => ['No se puede cancelar una fecha que ya fue reprogramada.'], + ]); + } + + if ($date->cancelled_at !== null) { + return $date->load('validityTime'); + } + + $date->update(['cancelled_at' => now()]); + $this->disableTicketsWithoutUsableDates($tenant, $date); + + return $date->fresh('validityTime'); + }); + } + + private function lockedDateForTenant(Tenant $tenant, EventDate $eventDate): EventDate + { + return $tenant->eventDates() + ->whereKey($eventDate->getKey()) + ->lockForUpdate() + ->firstOrFail(); + } + + private function chainContains(EventDate $start, EventDate $expected): bool + { + $current = $start; + $visited = []; + + while ($current->rescheduled_to_event_date_id !== null) { + if ($current->is($expected)) { + return true; + } + + if (isset($visited[$current->getKey()])) { + return true; + } + + $visited[$current->getKey()] = true; + $current = $current->rescheduledTo()->lockForUpdate()->first(); + + if ($current === null) { + return false; } } - $datesToDelete = $existingDates->slice(count($dates)); + return $current->is($expected); + } - if ($datesToDelete->contains(fn ($eventDate): bool => $eventDate - ->selectedByVariants() - ->whereHas('sourceTickets') - ->exists() - || $eventDate->variants()->whereHas('sourceTickets')->exists())) { - throw ValidationException::withMessages([ - 'dates' => ['No se puede eliminar una fecha utilizada por tickets generados.'], - ]); + private function disableTicketsWithoutUsableDates(Tenant $tenant, EventDate $cancelledDate): void + { + $affectedDateIds = collect([$cancelledDate->getKey()]); + $frontier = $affectedDateIds; + + while ($frontier->isNotEmpty()) { + $predecessors = $tenant->eventDates() + ->whereIn('rescheduled_to_event_date_id', $frontier) + ->pluck('id') + ->diff($affectedDateIds) + ->values(); + $affectedDateIds = $affectedDateIds->merge($predecessors)->unique()->values(); + $frontier = $predecessors; } - $datesToDelete->each->delete(); - $tenant->unsetRelation('eventDates'); + $variants = Variant::withTrashed() + ->where(function ($query) use ($affectedDateIds): void { + $query->whereIn('event_date_id', $affectedDateIds) + ->orWhereHas('eventDates', fn ($eventDates) => $eventDates + ->whereIn('event_dates.id', $affectedDateIds)); + }) + ->with(['eventDates', 'eventDate']) + ->get(); + + foreach ($variants as $variant) { + $hasUsableDate = $variant->selectedEventDates()->contains( + fn (EventDate $candidate): bool => $this->effectiveEventDateResolver->resolve($candidate) !== null + ); + + if ($hasUsableDate) { + continue; + } + + Ticket::query() + ->where('tenant_code', $tenant->codigo) + ->where('source_variant_id', $variant->getKey()) + ->whereNull('disabled_at') + ->whereNull('cancelled_at') + ->whereNull('refunded_at') + ->lockForUpdate() + ->get() + ->each(function (Ticket $ticket): void { + $ticket->markAsDisabled(); + $ticket->save(); + }); + } + } + + /** + * @param array{date: string, start_time: string, end_time: string} $data + * @return array{date: string, time_start: string, time_end: string} + */ + private function dateAttributes(array $data): array + { + return [ + 'date' => $data['date'], + 'time_start' => $data['start_time'].':00', + 'time_end' => $data['end_time'].':00', + ]; } /** @param array $contact */ diff --git a/app/Domains/Event/routes/adminapp.php b/app/Domains/Event/routes/adminapp.php index 75ae9ea..c437381 100644 --- a/app/Domains/Event/routes/adminapp.php +++ b/app/Domains/Event/routes/adminapp.php @@ -8,4 +8,7 @@ Route::prefix('v1/adminapp/tenant') ->group(function (): void { Route::get('event', [EventController::class, 'show']); Route::put('event', [EventController::class, 'update']); + Route::post('event-dates', [EventController::class, 'storeDate']); + Route::post('event-dates/{eventDate}/reschedule', [EventController::class, 'rescheduleDate']); + Route::post('event-dates/{eventDate}/cancel', [EventController::class, 'cancelDate']); }); diff --git a/tests/Feature/Event/AdminAppEventControllerTest.php b/tests/Feature/Event/AdminAppEventControllerTest.php index cdf9a98..53ca2aa 100644 --- a/tests/Feature/Event/AdminAppEventControllerTest.php +++ b/tests/Feature/Event/AdminAppEventControllerTest.php @@ -6,12 +6,17 @@ use App\Domains\Attachable\Enums\AttachmentType; use App\Domains\Attachable\Models\Attachment; use App\Domains\Auth\Models\User; use App\Domains\Authorization\Enums\RoleCode; +use App\Domains\Catalog\Models\CatalogItem; +use App\Domains\Catalog\Models\Inventory; +use App\Domains\Catalog\Models\Variant; use App\Domains\Tenant\Models\Tenant; use App\Domains\Tenant\Models\WebsiteType; use App\Domains\Ticket\Enums\ValidityTimeType; +use App\Domains\Ticket\Models\Ticket; use Database\Seeders\AuthorizationSeeder; use Database\Seeders\SocialMediaSeeder; use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Str; use Laravel\Sanctum\Sanctum; use Tests\TestCase; @@ -34,9 +39,10 @@ class AdminAppEventControllerTest extends TestCase { $this->getJson('/api/v1/adminapp/tenant/event')->assertUnauthorized(); $this->putJson('/api/v1/adminapp/tenant/event', $this->eventPayload())->assertUnauthorized(); + $this->postJson('/api/v1/adminapp/tenant/event-dates', $this->datePayload())->assertUnauthorized(); } - public function test_an_adminapp_user_can_create_the_active_event_and_contact_information(): void + public function test_an_adminapp_user_can_update_event_and_contact_information_without_synchronizing_dates(): void { $tenant = $this->createTenant('acme'); Sanctum::actingAs($this->createAdminAppUser($tenant)); @@ -45,10 +51,7 @@ class AdminAppEventControllerTest extends TestCase ->assertOk() ->assertJsonPath('data.title', 'Festival Acme') ->assertJsonPath('data.location', 'Predio Ferial, Rosario') - ->assertJsonPath('data.dates.0.date', '2026-10-09') - ->assertJsonPath('data.dates.0.validity_time.type', 'fixed_window') - ->assertJsonPath('data.dates.0.start_time', '09:00') - ->assertJsonPath('data.dates.0.end_time', '18:30') + ->assertJsonCount(0, 'data.dates') ->assertJsonPath('data.contact.whatsapp_url', 'https://wa.me/5493415550101') ->assertJsonPath('data.contact.instagram_url', 'https://instagram.com/acme') ->assertJsonPath('data.contact.facebook_url', null); @@ -58,25 +61,9 @@ class AdminAppEventControllerTest extends TestCase 'id' => $tenant->id, 'event_title' => 'Festival Acme', 'event_location' => 'Predio Ferial, Rosario', - 'event_date_text' => '9 de Octubre 2026', + 'event_date_text' => null, ]); - $this->assertDatabaseHas('event_dates', [ - 'tenant_code' => $tenant->codigo, - 'date' => '2026-10-09', - 'time_start' => '09:00:00', - 'time_end' => '18:30:00', - ]); - $eventDate = $tenant->eventDates()->with('validityTime')->sole(); - $this->assertSame($eventDate->validity_time_id, $response->json('data.dates.0.validity_time_id')); - $this->assertSame(ValidityTimeType::FixedWindow, $eventDate->validityTime->type); - $this->assertSame( - '2026-10-09 09:00:00', - $eventDate->validityTime->fixed_starts_at->format('Y-m-d H:i:s'), - ); - $this->assertSame( - '2026-10-09 18:30:00', - $eventDate->validityTime->fixed_expires_at->format('Y-m-d H:i:s'), - ); + $this->assertDatabaseCount('event_dates', 0); $this->assertDatabaseHas('tenant_social_media', [ 'tenant_code' => $tenant->codigo, 'social_media_code' => 'whatsapp', @@ -109,7 +96,7 @@ class AdminAppEventControllerTest extends TestCase ->assertJsonCount(0, 'data.dates'); } - public function test_updating_reuses_the_active_event_and_synchronizes_dates_and_contact(): void + public function test_updating_event_does_not_change_or_delete_existing_dates(): void { $tenant = $this->createTenant('acme'); $eventTenant = $this->createActiveEvent($tenant, 'Old Event'); @@ -124,7 +111,6 @@ class AdminAppEventControllerTest extends TestCase 'time_end' => '12:00', ]); $firstValidityTimeId = $firstDate->validity_time_id; - $removedValidityTimeId = $removedDate->validity_time_id; $tenant->socialMedia()->attach('facebook', [ 'url' => 'https://facebook.com/old', 'orden' => 2, @@ -136,12 +122,6 @@ class AdminAppEventControllerTest extends TestCase Sanctum::actingAs($this->createAdminAppUser($tenant)); $payload = $this->eventPayload(); - $payload['dates'] = [[ - 'date' => '2026-11-15', - 'start_time' => '10:00', - 'end_time' => '20:00', - ]]; - $this->putJson('/api/v1/adminapp/tenant/event', $payload) ->assertOk() ->assertJsonPath('data.id', $tenant->id) @@ -151,17 +131,16 @@ class AdminAppEventControllerTest extends TestCase $this->assertDatabaseHas('event_dates', [ 'id' => $firstDate->id, 'validity_time_id' => $firstValidityTimeId, - 'date' => '2026-11-15', + 'date' => '2026-10-01', ]); $this->assertDatabaseHas('validity_times', [ 'id' => $firstValidityTimeId, 'type' => ValidityTimeType::FixedWindow->value, - 'fixed_starts_at' => '2026-11-15 10:00:00', - 'fixed_expires_at' => '2026-11-15 20:00:00', + 'fixed_starts_at' => '2026-10-01 08:00:00', + 'fixed_expires_at' => '2026-10-01 12:00:00', ]); - $this->assertDatabaseMissing('event_dates', ['id' => $removedDate->id]); - $this->assertDatabaseMissing('validity_times', ['id' => $removedValidityTimeId]); - $this->assertSame('15 de Noviembre 2026', $tenant->fresh()->event_date_text); + $this->assertDatabaseHas('event_dates', ['id' => $removedDate->id]); + $this->assertSame('1 y 2 de Octubre 2026', $tenant->fresh()->event_date_text); $this->assertDatabaseMissing('tenant_social_media', [ 'tenant_code' => $tenant->codigo, 'social_media_code' => 'facebook', @@ -173,20 +152,17 @@ class AdminAppEventControllerTest extends TestCase ]); } - public function test_updating_event_dates_recalculates_the_tenant_date_text(): void + public function test_dates_are_created_independently_and_recalculate_the_tenant_date_text(): void { $tenant = $this->createTenant('acme'); Sanctum::actingAs($this->createAdminAppUser($tenant)); - $payload = $this->eventPayload(); - $payload['dates'] = collect([9, 10, 11, 12]) - ->map(fn (int $day): array => [ + foreach ([9, 10, 11, 12] as $day) { + $this->postJson('/api/v1/adminapp/tenant/event-dates', [ 'date' => sprintf('2026-10-%02d', $day), 'start_time' => '09:00', 'end_time' => '18:30', - ]) - ->all(); - - $this->putJson('/api/v1/adminapp/tenant/event', $payload)->assertOk(); + ])->assertCreated()->assertJsonPath('data.status', 'scheduled'); + } $this->assertSame( '9, 10, 11 y 12 de Octubre 2026', @@ -194,7 +170,115 @@ class AdminAppEventControllerTest extends TestCase ); } - public function test_update_validates_event_dates_and_contact_urls(): void + public function test_rescheduling_reuses_an_existing_date_and_tickets_resolve_its_validity(): void + { + $tenant = $this->createTenant('acme'); + $admin = $this->createAdminAppUser($tenant); + $original = $tenant->eventDates()->create([ + 'date' => '2027-10-09', + 'time_start' => '09:00', + 'time_end' => '18:30', + ]); + $destination = $tenant->eventDates()->create([ + 'date' => '2027-10-20', + 'time_start' => '11:00', + 'time_end' => '20:00', + ]); + $variant = $this->createVariant($tenant, $original->id); + $ticket = $this->createTicket($tenant, $admin, $variant); + Sanctum::actingAs($admin); + + $this->postJson("/api/v1/adminapp/tenant/event-dates/{$original->id}/reschedule", [ + 'date' => '2027-10-20', + ]) + ->assertOk() + ->assertJsonPath('data.status', 'rescheduled') + ->assertJsonPath('data.rescheduled_to_event_date_id', $destination->id); + + $this->assertDatabaseCount('event_dates', 2); + $this->assertSame( + '2027-10-20 11:00:00', + $ticket->resolvedValidity()->effectiveStartsAt()?->format('Y-m-d H:i:s'), + ); + $this->assertDatabaseHas('validity_times', [ + 'id' => $original->validity_time_id, + 'fixed_starts_at' => '2027-10-09 09:00:00', + ]); + + $this->postJson("/api/v1/adminapp/tenant/event-dates/{$destination->id}/reschedule", [ + 'date' => '2027-10-25', + ])->assertOk()->assertJsonPath('data.status', 'rescheduled'); + + $this->assertDatabaseCount('event_dates', 3); + $this->assertSame( + '2027-10-25 11:00:00', + $ticket->fresh()->resolvedValidity()->effectiveStartsAt()?->format('Y-m-d H:i:s'), + ); + } + + public function test_cancelling_disables_only_tickets_without_another_usable_date(): void + { + $tenant = $this->createTenant('acme'); + $admin = $this->createAdminAppUser($tenant); + $cancelledDate = $tenant->eventDates()->create([ + 'date' => '2027-10-09', + 'time_start' => '09:00', + 'time_end' => '18:30', + ]); + $otherDate = $tenant->eventDates()->create([ + 'date' => '2027-10-10', + 'time_start' => '09:00', + 'time_end' => '18:30', + ]); + $singleDateVariant = $this->createVariant($tenant, $cancelledDate->id); + $multipleDateVariant = $this->createVariant($tenant); + $multipleDateVariant->eventDates()->sync([$cancelledDate->id, $otherDate->id]); + $singleDateTicket = $this->createTicket($tenant, $admin, $singleDateVariant); + $multipleDateTicket = $this->createTicket($tenant, $admin, $multipleDateVariant); + Sanctum::actingAs($admin); + + $this->postJson("/api/v1/adminapp/tenant/event-dates/{$cancelledDate->id}/cancel") + ->assertOk() + ->assertJsonPath('data.status', 'cancelled'); + + $this->assertNotNull($singleDateTicket->fresh()->disabled_at); + $this->assertNull($multipleDateTicket->fresh()->disabled_at); + $this->assertSame( + '2027-10-10 09:00:00', + $multipleDateTicket->fresh()->resolvedValidity()->effectiveStartsAt()?->format('Y-m-d H:i:s'), + ); + } + + public function test_cancelling_a_reschedule_destination_disables_tickets_from_predecessor_dates(): void + { + $tenant = $this->createTenant('acme'); + $admin = $this->createAdminAppUser($tenant); + $original = $tenant->eventDates()->create([ + 'date' => '2027-10-09', + 'time_start' => '09:00', + 'time_end' => '18:30', + ]); + $destination = $tenant->eventDates()->create([ + 'date' => '2027-10-20', + 'time_start' => '09:00', + 'time_end' => '18:30', + ]); + $original->update(['rescheduled_to_event_date_id' => $destination->id]); + $ticket = $this->createTicket( + $tenant, + $admin, + $this->createVariant($tenant, $original->id), + ); + Sanctum::actingAs($admin); + + $this->postJson("/api/v1/adminapp/tenant/event-dates/{$destination->id}/cancel") + ->assertOk(); + + $this->assertNotNull($ticket->fresh()->disabled_at); + $this->assertFalse($ticket->fresh()->resolvedValidity()->isResolvable); + } + + public function test_update_and_date_creation_validate_their_own_payloads(): void { $tenant = $this->createTenant('acme'); Sanctum::actingAs($this->createAdminAppUser($tenant)); @@ -202,10 +286,6 @@ class AdminAppEventControllerTest extends TestCase $this->putJson('/api/v1/adminapp/tenant/event', [ 'title' => '', 'location' => '', - 'dates' => [ - ['date' => '09/10/2026', 'start_time' => '9am', 'end_time' => '18:00'], - ['date' => '09/10/2026', 'start_time' => '09:00', 'end_time' => '18:00'], - ], 'contact' => [ 'whatsapp_url' => 'not-a-url', 'instagram_url' => null, @@ -216,12 +296,15 @@ class AdminAppEventControllerTest extends TestCase ->assertJsonValidationErrors([ 'title', 'location', - 'dates.0.date', - 'dates.0.start_time', - 'dates.1.date', 'contact.whatsapp_url', ]); + $this->postJson('/api/v1/adminapp/tenant/event-dates', [ + 'date' => '09/10/2026', + 'start_time' => '9am', + 'end_time' => '18:00', + ])->assertUnprocessable()->assertJsonValidationErrors(['date', 'start_time']); + $this->assertNull($tenant->fresh()->event_title); } @@ -273,11 +356,6 @@ class AdminAppEventControllerTest extends TestCase return [ 'title' => 'Festival Acme', 'location' => 'Predio Ferial, Rosario', - 'dates' => [[ - 'date' => '2026-10-09', - 'start_time' => '09:00', - 'end_time' => '18:30', - ]], 'contact' => [ 'whatsapp_url' => 'https://wa.me/5493415550101', 'instagram_url' => 'https://instagram.com/acme', @@ -286,6 +364,43 @@ class AdminAppEventControllerTest extends TestCase ]; } + /** @return array{date: string, start_time: string, end_time: string} */ + private function datePayload(): array + { + return [ + 'date' => '2026-10-09', + 'start_time' => '09:00', + 'end_time' => '18:30', + ]; + } + + private function createVariant(Tenant $tenant, ?int $eventDateId = null): Variant + { + $item = CatalogItem::query()->create([ + 'tenant_code' => $tenant->codigo, + 'slug' => 'item-'.Str::uuid(), + 'nombre' => 'Entrada', + 'precio' => '1000.00', + ]); + + return Variant::query()->create([ + 'catalog_item_id' => $item->id, + 'inventory_id' => Inventory::query()->create()->id, + 'event_date_id' => $eventDateId, + ]); + } + + private function createTicket(Tenant $tenant, User $user, Variant $variant): Ticket + { + return Ticket::query()->create([ + 'tenant_code' => $tenant->codigo, + 'ticket' => (string) Str::uuid(), + 'user_id' => $user->id, + 'source_catalog_item_id' => $variant->catalog_item_id, + 'source_variant_id' => $variant->id, + ]); + } + private function createTenant(string $code): Tenant { $headerLogo = $this->createAttachment("{$code}-header");