refactor(backend): reorganize domains into Core, Commerce, Ticketing and Shared
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\FiestaFutbolInfantil\Resources;
|
||||
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
/** @mixin CatalogItem */
|
||||
class AccommodationResource extends JsonResource
|
||||
{
|
||||
/** @return array<string, mixed> */
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
if ($this->resource === null) {
|
||||
return [
|
||||
'id' => null,
|
||||
'name' => 'Alojamiento',
|
||||
'variants' => [],
|
||||
];
|
||||
}
|
||||
|
||||
$typeAttribute = $this->itemAttributes
|
||||
->first(fn ($itemAttribute) => $itemAttribute->attribute?->codigo === 'tipo_alojamiento');
|
||||
$options = $typeAttribute?->attribute?->options?->keyBy('value') ?? collect();
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->nombre,
|
||||
'variants' => $this->variants->map(function ($variant) use ($typeAttribute, $options): array {
|
||||
$value = $variant->definitions
|
||||
->firstWhere('item_attribute_id', $typeAttribute?->id)
|
||||
?->value;
|
||||
|
||||
return [
|
||||
'id' => $variant->id,
|
||||
'title' => $options->get($value)?->label ?? $value,
|
||||
'value' => $value,
|
||||
'description' => $variant->descripcion,
|
||||
'stock' => $variant->inventory->real_stock,
|
||||
'price' => number_format($variant->getPrice(), 2, '.', ''),
|
||||
];
|
||||
})->values(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\FiestaFutbolInfantil\Resources;
|
||||
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
/** @mixin CatalogItem */
|
||||
class EntryResource extends JsonResource
|
||||
{
|
||||
/** @return array<string, mixed> */
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
$variants = $this->variants->whereNull('replaced_by_variant_id');
|
||||
|
||||
if ($variants->count() !== 1) {
|
||||
throw ValidationException::withMessages([
|
||||
'entries' => [sprintf(
|
||||
'La entrada %s tiene %d variantes sin reemplazar (IDs: %s). Se esperaba una.',
|
||||
$this->id,
|
||||
$variants->count(),
|
||||
$variants->pluck('id')->implode(', '),
|
||||
)],
|
||||
]);
|
||||
}
|
||||
|
||||
$variant = $variants->first();
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->nombre,
|
||||
'description' => $this->descripcion,
|
||||
'event_date_ids' => $variant->selectedEventDates()->pluck('id')->values(),
|
||||
'stock' => $variant->inventory->real_stock,
|
||||
'price' => $this->precio,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\FiestaFutbolInfantil\Resources;
|
||||
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\Variant;
|
||||
use App\Domains\Event\Enums\EventDateChangeType;
|
||||
use App\Domains\Event\Enums\EventDateStatus;
|
||||
use App\Domains\Event\Models\EventDate;
|
||||
use App\Domains\Event\Models\EventDateChange;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/** @mixin CatalogItem */
|
||||
class FoodResource extends JsonResource
|
||||
{
|
||||
/** @return array<string, mixed> */
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
if ($this->resource === null) {
|
||||
return [
|
||||
'id' => null,
|
||||
'name' => 'Comida',
|
||||
'variants' => [],
|
||||
'history' => [],
|
||||
];
|
||||
}
|
||||
|
||||
$currentVariants = $this->variants
|
||||
->filter(fn (Variant $variant): bool => $this->isCurrent($variant));
|
||||
$historicalVariants = $this->variants
|
||||
->filter(fn (Variant $variant): bool => $this->isHistorical($variant));
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->nombre,
|
||||
'variants' => $currentVariants->map($this->variantData(...))->values(),
|
||||
'history' => $this->historyData($historicalVariants),
|
||||
];
|
||||
}
|
||||
|
||||
private function isCurrent(Variant $variant): bool
|
||||
{
|
||||
if ($variant->sales_disabled_at !== null || $variant->replaced_by_variant_id !== null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$status = $variant->selectedEventDates()->first()?->status;
|
||||
|
||||
return $status === null || in_array(
|
||||
$status,
|
||||
[EventDateStatus::Scheduled, EventDateStatus::InProgress],
|
||||
true,
|
||||
);
|
||||
}
|
||||
|
||||
private function isHistorical(Variant $variant): bool
|
||||
{
|
||||
return in_array(
|
||||
$variant->selectedEventDates()->first()?->status,
|
||||
[
|
||||
EventDateStatus::Rescheduled,
|
||||
EventDateStatus::Suspended,
|
||||
EventDateStatus::Completed,
|
||||
],
|
||||
true,
|
||||
);
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function variantData(Variant $variant): array
|
||||
{
|
||||
$values = $variant->selectionValues();
|
||||
$eventDate = $variant->selectedEventDates()->first();
|
||||
|
||||
return [
|
||||
'id' => $variant->id,
|
||||
'event_date_id' => $eventDate?->id,
|
||||
'event_date' => $eventDate?->date?->format('Y-m-d'),
|
||||
'schedule' => $values->get('horario'),
|
||||
'service' => $values->get('servicio'),
|
||||
'description' => $variant->descripcion,
|
||||
'stock' => $variant->inventory->real_stock,
|
||||
'price' => number_format($variant->getPrice(), 2, '.', ''),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, Variant> $variants
|
||||
* @return Collection<int, array<string, mixed>>
|
||||
*/
|
||||
private function historyData(Collection $variants): Collection
|
||||
{
|
||||
return $variants
|
||||
->filter(fn (Variant $variant): bool => $variant->selectedEventDates()->first() !== null)
|
||||
->groupBy(fn (Variant $variant): int => (int) $variant->selectedEventDates()->first()->id)
|
||||
->map(function (Collection $dateVariants): array {
|
||||
/** @var EventDate $eventDate */
|
||||
$eventDate = $dateVariants->first()->selectedEventDates()->first();
|
||||
$status = $this->historicalStatus($eventDate);
|
||||
$change = $this->changeForStatus($eventDate, $status);
|
||||
|
||||
return [
|
||||
'id' => $eventDate->id,
|
||||
'change_id' => $change?->id,
|
||||
'status' => $status->value,
|
||||
'status_text' => match ($status) {
|
||||
EventDateStatus::Rescheduled => 'REPROGRAMADA',
|
||||
EventDateStatus::Suspended => 'CANCELADA',
|
||||
EventDateStatus::Completed => 'FINALIZADA',
|
||||
default => '',
|
||||
},
|
||||
'event_date_id' => $eventDate->id,
|
||||
'event_date' => $eventDate->date->format('Y-m-d'),
|
||||
'replacement_event_date_id' => $status === EventDateStatus::Rescheduled
|
||||
? ($change?->destination_event_date_id
|
||||
?? $eventDate->rescheduled_to_event_date_id)
|
||||
: null,
|
||||
'replacement_event_date' => $status === EventDateStatus::Rescheduled
|
||||
? ($change?->new_date?->format('Y-m-d')
|
||||
?? $eventDate->rescheduledTo?->date?->format('Y-m-d'))
|
||||
: null,
|
||||
'occurred_at' => ($change?->created_at ?? $eventDate->endsAt())->toISOString(),
|
||||
'variants' => $dateVariants->map($this->variantData(...))->values(),
|
||||
];
|
||||
})
|
||||
->sortByDesc('occurred_at')
|
||||
->values();
|
||||
}
|
||||
|
||||
private function historicalStatus(EventDate $eventDate): EventDateStatus
|
||||
{
|
||||
return match ($eventDate->status) {
|
||||
EventDateStatus::Rescheduled => EventDateStatus::Rescheduled,
|
||||
EventDateStatus::Suspended => EventDateStatus::Suspended,
|
||||
default => EventDateStatus::Completed,
|
||||
};
|
||||
}
|
||||
|
||||
private function changeForStatus(
|
||||
EventDate $eventDate,
|
||||
EventDateStatus $status,
|
||||
): ?EventDateChange {
|
||||
$changeType = match ($status) {
|
||||
EventDateStatus::Rescheduled => EventDateChangeType::Rescheduled,
|
||||
EventDateStatus::Suspended => EventDateChangeType::Suspended,
|
||||
default => null,
|
||||
};
|
||||
|
||||
return $changeType === null
|
||||
? null
|
||||
: $eventDate->changeHistory
|
||||
->first(fn (EventDateChange $change): bool => $change->change_type === $changeType);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\FiestaFutbolInfantil\Resources;
|
||||
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
/** @mixin CatalogItem */
|
||||
class MerchandiseResource extends JsonResource
|
||||
{
|
||||
/** @return array<string, mixed> */
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
$itemAttributes = $this->itemAttributes->keyBy(
|
||||
fn ($itemAttribute) => $itemAttribute->attribute?->codigo
|
||||
);
|
||||
$colorAttribute = $itemAttributes->get('color');
|
||||
$sizeAttribute = $itemAttributes->get('talle');
|
||||
$colorOptions = $colorAttribute?->attribute?->options?->keyBy('value') ?? collect();
|
||||
$sizeOptions = $sizeAttribute?->attribute?->options?->keyBy('value') ?? collect();
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->nombre,
|
||||
'description' => $this->descripcion,
|
||||
'max_units_per_user' => $this->max_units_per_user,
|
||||
'variants' => $this->variants->map(function ($variant) use (
|
||||
$colorAttribute,
|
||||
$sizeAttribute,
|
||||
$colorOptions,
|
||||
$sizeOptions,
|
||||
): array {
|
||||
$colorValue = $variant->definitions
|
||||
->firstWhere('item_attribute_id', $colorAttribute?->id)
|
||||
?->value;
|
||||
$sizeValue = $variant->definitions
|
||||
->firstWhere('item_attribute_id', $sizeAttribute?->id)
|
||||
?->value;
|
||||
|
||||
return [
|
||||
'id' => $variant->id,
|
||||
'color' => $colorOptions->get($colorValue)?->label ?? $colorValue,
|
||||
'color_value' => $colorValue,
|
||||
'size' => $sizeOptions->get($sizeValue)?->label ?? $sizeValue,
|
||||
'size_value' => $sizeValue,
|
||||
'stock' => $variant->inventory->real_stock,
|
||||
'price' => number_format($variant->getPrice(), 2, '.', ''),
|
||||
];
|
||||
})->values(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user