feat(event): implement admin date creation, rescheduling, and cancellation endpoints
This commit is contained in:
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user