feat(event): implement admin date creation, rescheduling, and cancellation endpoints
This commit is contained in:
@@ -2,7 +2,11 @@
|
|||||||
|
|
||||||
namespace App\Domains\Event\Controllers\AdminApp;
|
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\Requests\UpdateEventRequest;
|
||||||
|
use App\Domains\Event\Resources\EventDateResource;
|
||||||
use App\Domains\Event\Resources\EventResource;
|
use App\Domains\Event\Resources\EventResource;
|
||||||
use App\Domains\Event\Services\EventService;
|
use App\Domains\Event\Services\EventService;
|
||||||
use App\Http\Controllers\Controller;
|
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,
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
21
app/Domains/Event/Requests/RescheduleEventDateRequest.php
Normal file
21
app/Domains/Event/Requests/RescheduleEventDateRequest.php
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Event\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class RescheduleEventDateRequest extends FormRequest
|
||||||
|
{
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return array<string, mixed> */
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'date' => ['required', 'date_format:Y-m-d'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
23
app/Domains/Event/Requests/StoreEventDateRequest.php
Normal file
23
app/Domains/Event/Requests/StoreEventDateRequest.php
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Event\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class StoreEventDateRequest extends FormRequest
|
||||||
|
{
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return array<string, mixed> */
|
||||||
|
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'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,11 +19,6 @@ class UpdateEventRequest extends FormRequest
|
|||||||
return [
|
return [
|
||||||
'title' => ['required', 'string', 'max:255'],
|
'title' => ['required', 'string', 'max:255'],
|
||||||
'location' => ['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' => ['sometimes', 'array'],
|
||||||
'social_media.*' => ['required', 'array:code,url,orden'],
|
'social_media.*' => ['required', 'array:code,url,orden'],
|
||||||
'social_media.*.code' => [
|
'social_media.*.code' => [
|
||||||
|
|||||||
28
app/Domains/Event/Resources/EventDateResource.php
Normal file
28
app/Domains/Event/Resources/EventDateResource.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Event\Resources;
|
||||||
|
|
||||||
|
use App\Domains\Event\Models\EventDate;
|
||||||
|
use App\Domains\Ticket\Resources\ValidityTimeResource;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
/** @mixin EventDate */
|
||||||
|
class EventDateResource extends JsonResource
|
||||||
|
{
|
||||||
|
/** @return array<string, mixed> */
|
||||||
|
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(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,6 @@
|
|||||||
namespace App\Domains\Event\Resources;
|
namespace App\Domains\Event\Resources;
|
||||||
|
|
||||||
use App\Domains\Tenant\Models\Tenant;
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
use App\Domains\Ticket\Resources\ValidityTimeResource;
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Resources\Json\JsonResource;
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
@@ -19,14 +18,7 @@ class EventResource extends JsonResource
|
|||||||
'id' => $this->id,
|
'id' => $this->id,
|
||||||
'title' => $this->event_title,
|
'title' => $this->event_title,
|
||||||
'location' => $this->event_location,
|
'location' => $this->event_location,
|
||||||
'dates' => $this->eventDates->map(fn ($eventDate): array => [
|
'dates' => EventDateResource::collection($this->eventDates),
|
||||||
'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(),
|
|
||||||
'social_media' => $this->socialMedia->map(fn ($item): array => [
|
'social_media' => $this->socialMedia->map(fn ($item): array => [
|
||||||
'code' => $item->code,
|
'code' => $item->code,
|
||||||
'url' => $item->pivot->url,
|
'url' => $item->pivot->url,
|
||||||
|
|||||||
@@ -2,7 +2,10 @@
|
|||||||
|
|
||||||
namespace App\Domains\Event\Services;
|
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\Tenant\Models\Tenant;
|
||||||
|
use App\Domains\Ticket\Models\Ticket;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Validation\ValidationException;
|
use Illuminate\Validation\ValidationException;
|
||||||
|
|
||||||
@@ -14,6 +17,10 @@ class EventService
|
|||||||
'facebook_url' => 'facebook',
|
'facebook_url' => 'facebook',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private readonly EffectiveEventDateResolver $effectiveEventDateResolver,
|
||||||
|
) {}
|
||||||
|
|
||||||
public function forTenant(Tenant $tenant): Tenant
|
public function forTenant(Tenant $tenant): Tenant
|
||||||
{
|
{
|
||||||
return $tenant->load(['eventDates.validityTime', 'socialMedia']);
|
return $tenant->load(['eventDates.validityTime', 'socialMedia']);
|
||||||
@@ -29,7 +36,6 @@ class EventService
|
|||||||
'event_location' => $data['location'],
|
'event_location' => $data['location'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->syncDates($tenant, $data['dates']);
|
|
||||||
if (array_key_exists('social_media', $data)) {
|
if (array_key_exists('social_media', $data)) {
|
||||||
$this->syncSocialMedia($tenant, $data['social_media']);
|
$this->syncSocialMedia($tenant, $data['social_media']);
|
||||||
} else {
|
} else {
|
||||||
@@ -40,41 +46,185 @@ class EventService
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param array<int, array{date: string, start_time: string, end_time: string}> $dates */
|
/** @param array{date: string, start_time: string, end_time: string} $data */
|
||||||
private function syncDates(Tenant $tenant, array $dates): void
|
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) {
|
if ($tenant->eventDates()->where($attributes)->exists()) {
|
||||||
$attributes = [
|
throw ValidationException::withMessages([
|
||||||
'date' => $date['date'],
|
'date' => ['La fecha y el horario ya existen.'],
|
||||||
'time_start' => $date['start_time'],
|
]);
|
||||||
'time_end' => $date['end_time'],
|
}
|
||||||
];
|
|
||||||
|
|
||||||
$existingDate = $existingDates->get($index);
|
return $tenant->eventDates()->create($attributes)->load('validityTime');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if ($existingDate) {
|
/** @param array{date: string} $data */
|
||||||
$existingDate->update($attributes);
|
public function rescheduleDateForTenant(Tenant $tenant, EventDate $eventDate, array $data): EventDate
|
||||||
} else {
|
{
|
||||||
$tenant->eventDates()->create($attributes);
|
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
|
private function disableTicketsWithoutUsableDates(Tenant $tenant, EventDate $cancelledDate): void
|
||||||
->selectedByVariants()
|
{
|
||||||
->whereHas('sourceTickets')
|
$affectedDateIds = collect([$cancelledDate->getKey()]);
|
||||||
->exists()
|
$frontier = $affectedDateIds;
|
||||||
|| $eventDate->variants()->whereHas('sourceTickets')->exists())) {
|
|
||||||
throw ValidationException::withMessages([
|
while ($frontier->isNotEmpty()) {
|
||||||
'dates' => ['No se puede eliminar una fecha utilizada por tickets generados.'],
|
$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();
|
$variants = Variant::withTrashed()
|
||||||
$tenant->unsetRelation('eventDates');
|
->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<string, string|null> $contact */
|
/** @param array<string, string|null> $contact */
|
||||||
|
|||||||
@@ -8,4 +8,7 @@ Route::prefix('v1/adminapp/tenant')
|
|||||||
->group(function (): void {
|
->group(function (): void {
|
||||||
Route::get('event', [EventController::class, 'show']);
|
Route::get('event', [EventController::class, 'show']);
|
||||||
Route::put('event', [EventController::class, 'update']);
|
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']);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,12 +6,17 @@ use App\Domains\Attachable\Enums\AttachmentType;
|
|||||||
use App\Domains\Attachable\Models\Attachment;
|
use App\Domains\Attachable\Models\Attachment;
|
||||||
use App\Domains\Auth\Models\User;
|
use App\Domains\Auth\Models\User;
|
||||||
use App\Domains\Authorization\Enums\RoleCode;
|
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\Tenant;
|
||||||
use App\Domains\Tenant\Models\WebsiteType;
|
use App\Domains\Tenant\Models\WebsiteType;
|
||||||
use App\Domains\Ticket\Enums\ValidityTimeType;
|
use App\Domains\Ticket\Enums\ValidityTimeType;
|
||||||
|
use App\Domains\Ticket\Models\Ticket;
|
||||||
use Database\Seeders\AuthorizationSeeder;
|
use Database\Seeders\AuthorizationSeeder;
|
||||||
use Database\Seeders\SocialMediaSeeder;
|
use Database\Seeders\SocialMediaSeeder;
|
||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
use Laravel\Sanctum\Sanctum;
|
use Laravel\Sanctum\Sanctum;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
|
||||||
@@ -34,9 +39,10 @@ class AdminAppEventControllerTest extends TestCase
|
|||||||
{
|
{
|
||||||
$this->getJson('/api/v1/adminapp/tenant/event')->assertUnauthorized();
|
$this->getJson('/api/v1/adminapp/tenant/event')->assertUnauthorized();
|
||||||
$this->putJson('/api/v1/adminapp/tenant/event', $this->eventPayload())->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');
|
$tenant = $this->createTenant('acme');
|
||||||
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
||||||
@@ -45,10 +51,7 @@ class AdminAppEventControllerTest extends TestCase
|
|||||||
->assertOk()
|
->assertOk()
|
||||||
->assertJsonPath('data.title', 'Festival Acme')
|
->assertJsonPath('data.title', 'Festival Acme')
|
||||||
->assertJsonPath('data.location', 'Predio Ferial, Rosario')
|
->assertJsonPath('data.location', 'Predio Ferial, Rosario')
|
||||||
->assertJsonPath('data.dates.0.date', '2026-10-09')
|
->assertJsonCount(0, 'data.dates')
|
||||||
->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')
|
|
||||||
->assertJsonPath('data.contact.whatsapp_url', 'https://wa.me/5493415550101')
|
->assertJsonPath('data.contact.whatsapp_url', 'https://wa.me/5493415550101')
|
||||||
->assertJsonPath('data.contact.instagram_url', 'https://instagram.com/acme')
|
->assertJsonPath('data.contact.instagram_url', 'https://instagram.com/acme')
|
||||||
->assertJsonPath('data.contact.facebook_url', null);
|
->assertJsonPath('data.contact.facebook_url', null);
|
||||||
@@ -58,25 +61,9 @@ class AdminAppEventControllerTest extends TestCase
|
|||||||
'id' => $tenant->id,
|
'id' => $tenant->id,
|
||||||
'event_title' => 'Festival Acme',
|
'event_title' => 'Festival Acme',
|
||||||
'event_location' => 'Predio Ferial, Rosario',
|
'event_location' => 'Predio Ferial, Rosario',
|
||||||
'event_date_text' => '9 de Octubre 2026',
|
'event_date_text' => null,
|
||||||
]);
|
]);
|
||||||
$this->assertDatabaseHas('event_dates', [
|
$this->assertDatabaseCount('event_dates', 0);
|
||||||
'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->assertDatabaseHas('tenant_social_media', [
|
$this->assertDatabaseHas('tenant_social_media', [
|
||||||
'tenant_code' => $tenant->codigo,
|
'tenant_code' => $tenant->codigo,
|
||||||
'social_media_code' => 'whatsapp',
|
'social_media_code' => 'whatsapp',
|
||||||
@@ -109,7 +96,7 @@ class AdminAppEventControllerTest extends TestCase
|
|||||||
->assertJsonCount(0, 'data.dates');
|
->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');
|
$tenant = $this->createTenant('acme');
|
||||||
$eventTenant = $this->createActiveEvent($tenant, 'Old Event');
|
$eventTenant = $this->createActiveEvent($tenant, 'Old Event');
|
||||||
@@ -124,7 +111,6 @@ class AdminAppEventControllerTest extends TestCase
|
|||||||
'time_end' => '12:00',
|
'time_end' => '12:00',
|
||||||
]);
|
]);
|
||||||
$firstValidityTimeId = $firstDate->validity_time_id;
|
$firstValidityTimeId = $firstDate->validity_time_id;
|
||||||
$removedValidityTimeId = $removedDate->validity_time_id;
|
|
||||||
$tenant->socialMedia()->attach('facebook', [
|
$tenant->socialMedia()->attach('facebook', [
|
||||||
'url' => 'https://facebook.com/old',
|
'url' => 'https://facebook.com/old',
|
||||||
'orden' => 2,
|
'orden' => 2,
|
||||||
@@ -136,12 +122,6 @@ class AdminAppEventControllerTest extends TestCase
|
|||||||
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
||||||
|
|
||||||
$payload = $this->eventPayload();
|
$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)
|
$this->putJson('/api/v1/adminapp/tenant/event', $payload)
|
||||||
->assertOk()
|
->assertOk()
|
||||||
->assertJsonPath('data.id', $tenant->id)
|
->assertJsonPath('data.id', $tenant->id)
|
||||||
@@ -151,17 +131,16 @@ class AdminAppEventControllerTest extends TestCase
|
|||||||
$this->assertDatabaseHas('event_dates', [
|
$this->assertDatabaseHas('event_dates', [
|
||||||
'id' => $firstDate->id,
|
'id' => $firstDate->id,
|
||||||
'validity_time_id' => $firstValidityTimeId,
|
'validity_time_id' => $firstValidityTimeId,
|
||||||
'date' => '2026-11-15',
|
'date' => '2026-10-01',
|
||||||
]);
|
]);
|
||||||
$this->assertDatabaseHas('validity_times', [
|
$this->assertDatabaseHas('validity_times', [
|
||||||
'id' => $firstValidityTimeId,
|
'id' => $firstValidityTimeId,
|
||||||
'type' => ValidityTimeType::FixedWindow->value,
|
'type' => ValidityTimeType::FixedWindow->value,
|
||||||
'fixed_starts_at' => '2026-11-15 10:00:00',
|
'fixed_starts_at' => '2026-10-01 08:00:00',
|
||||||
'fixed_expires_at' => '2026-11-15 20:00:00',
|
'fixed_expires_at' => '2026-10-01 12:00:00',
|
||||||
]);
|
]);
|
||||||
$this->assertDatabaseMissing('event_dates', ['id' => $removedDate->id]);
|
$this->assertDatabaseHas('event_dates', ['id' => $removedDate->id]);
|
||||||
$this->assertDatabaseMissing('validity_times', ['id' => $removedValidityTimeId]);
|
$this->assertSame('1 y 2 de Octubre 2026', $tenant->fresh()->event_date_text);
|
||||||
$this->assertSame('15 de Noviembre 2026', $tenant->fresh()->event_date_text);
|
|
||||||
$this->assertDatabaseMissing('tenant_social_media', [
|
$this->assertDatabaseMissing('tenant_social_media', [
|
||||||
'tenant_code' => $tenant->codigo,
|
'tenant_code' => $tenant->codigo,
|
||||||
'social_media_code' => 'facebook',
|
'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');
|
$tenant = $this->createTenant('acme');
|
||||||
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
||||||
$payload = $this->eventPayload();
|
foreach ([9, 10, 11, 12] as $day) {
|
||||||
$payload['dates'] = collect([9, 10, 11, 12])
|
$this->postJson('/api/v1/adminapp/tenant/event-dates', [
|
||||||
->map(fn (int $day): array => [
|
|
||||||
'date' => sprintf('2026-10-%02d', $day),
|
'date' => sprintf('2026-10-%02d', $day),
|
||||||
'start_time' => '09:00',
|
'start_time' => '09:00',
|
||||||
'end_time' => '18:30',
|
'end_time' => '18:30',
|
||||||
])
|
])->assertCreated()->assertJsonPath('data.status', 'scheduled');
|
||||||
->all();
|
}
|
||||||
|
|
||||||
$this->putJson('/api/v1/adminapp/tenant/event', $payload)->assertOk();
|
|
||||||
|
|
||||||
$this->assertSame(
|
$this->assertSame(
|
||||||
'9, 10, 11 y 12 de Octubre 2026',
|
'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');
|
$tenant = $this->createTenant('acme');
|
||||||
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
||||||
@@ -202,10 +286,6 @@ class AdminAppEventControllerTest extends TestCase
|
|||||||
$this->putJson('/api/v1/adminapp/tenant/event', [
|
$this->putJson('/api/v1/adminapp/tenant/event', [
|
||||||
'title' => '',
|
'title' => '',
|
||||||
'location' => '',
|
'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' => [
|
'contact' => [
|
||||||
'whatsapp_url' => 'not-a-url',
|
'whatsapp_url' => 'not-a-url',
|
||||||
'instagram_url' => null,
|
'instagram_url' => null,
|
||||||
@@ -216,12 +296,15 @@ class AdminAppEventControllerTest extends TestCase
|
|||||||
->assertJsonValidationErrors([
|
->assertJsonValidationErrors([
|
||||||
'title',
|
'title',
|
||||||
'location',
|
'location',
|
||||||
'dates.0.date',
|
|
||||||
'dates.0.start_time',
|
|
||||||
'dates.1.date',
|
|
||||||
'contact.whatsapp_url',
|
'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);
|
$this->assertNull($tenant->fresh()->event_title);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,11 +356,6 @@ class AdminAppEventControllerTest extends TestCase
|
|||||||
return [
|
return [
|
||||||
'title' => 'Festival Acme',
|
'title' => 'Festival Acme',
|
||||||
'location' => 'Predio Ferial, Rosario',
|
'location' => 'Predio Ferial, Rosario',
|
||||||
'dates' => [[
|
|
||||||
'date' => '2026-10-09',
|
|
||||||
'start_time' => '09:00',
|
|
||||||
'end_time' => '18:30',
|
|
||||||
]],
|
|
||||||
'contact' => [
|
'contact' => [
|
||||||
'whatsapp_url' => 'https://wa.me/5493415550101',
|
'whatsapp_url' => 'https://wa.me/5493415550101',
|
||||||
'instagram_url' => 'https://instagram.com/acme',
|
'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
|
private function createTenant(string $code): Tenant
|
||||||
{
|
{
|
||||||
$headerLogo = $this->createAttachment("{$code}-header");
|
$headerLogo = $this->createAttachment("{$code}-header");
|
||||||
|
|||||||
Reference in New Issue
Block a user