Add Routing module: EvRoute, pricing, time slots, read API, caching (T3.1-T3.7)
Implements Phase 3 in full: EvRoute model with company/destination relations and a from/to-must-differ guard; the ev_route_time_slots pivot; RoutePricing with a per-route is_blocked flag (every route auto-manages exactly one price row per vehicle option via a fixed-row Filament repeater on both create and edit); PricingService::quote() with a shared VehicleOption enum; RoutingPlugin with the EvRouteResource admin UI (activation gated on non-blocked options being priced); the /api/v1/routes read API (list/show/pricing/time-slots, AI-agent-friendly nested shape); and Redis-tag-based response caching invalidated via EvRoute/RoutePricing observers.
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Routing\Filament\Resources\EvRoutes;
|
||||
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
use Modules\Routing\Filament\Resources\EvRoutes\Pages\CreateEvRoute;
|
||||
use Modules\Routing\Filament\Resources\EvRoutes\Pages\EditEvRoute;
|
||||
use Modules\Routing\Filament\Resources\EvRoutes\Pages\ListEvRoutes;
|
||||
use Modules\Routing\Filament\Resources\EvRoutes\Schemas\EvRouteForm;
|
||||
use Modules\Routing\Filament\Resources\EvRoutes\Tables\EvRoutesTable;
|
||||
use Modules\Routing\Models\EvRoute;
|
||||
use UnitEnum;
|
||||
|
||||
class EvRouteResource extends Resource
|
||||
{
|
||||
protected static ?string $model = EvRoute::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedMap;
|
||||
|
||||
protected static string|UnitEnum|null $navigationGroup = 'Routing';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return EvRouteForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return EvRoutesTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListEvRoutes::route('/'),
|
||||
'create' => CreateEvRoute::route('/create'),
|
||||
'edit' => EditEvRoute::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* A route can only be activated once every non-blocked vehicle option
|
||||
* carries a price above 0 — enforced here (form/UI level), not as a DB
|
||||
* constraint. A route with every option blocked can never be activated.
|
||||
*
|
||||
* Checked against the raw pricing repeater data submitted on the form
|
||||
* (not a DB read), since on the create form no pricing rows exist yet,
|
||||
* and on the edit form the repeater's relationship save hasn't run yet
|
||||
* by the time this is checked.
|
||||
*
|
||||
* @param array<int, array{price?: mixed, is_blocked?: mixed}> $pricingItems
|
||||
*/
|
||||
public static function hasCompletePricingData(array $pricingItems): bool
|
||||
{
|
||||
$nonBlocked = collect($pricingItems)->reject(fn (array $item) => (bool) ($item['is_blocked'] ?? false));
|
||||
|
||||
return $nonBlocked->isNotEmpty() && $nonBlocked->every(fn (array $item) => (float) ($item['price'] ?? 0) > 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Routing\Filament\Resources\EvRoutes\Pages;
|
||||
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Modules\Routing\Filament\Resources\EvRoutes\EvRouteResource;
|
||||
|
||||
class CreateEvRoute extends CreateRecord
|
||||
{
|
||||
protected static string $resource = EvRouteResource::class;
|
||||
|
||||
protected function beforeCreate(): void
|
||||
{
|
||||
if (
|
||||
($this->data['is_active'] ?? false)
|
||||
&& ! EvRouteResource::hasCompletePricingData($this->data['pricing'] ?? [])
|
||||
) {
|
||||
Notification::make()
|
||||
->warning()
|
||||
->title('Route cannot be activated yet')
|
||||
->body('Set a price above 0 for every non-blocked vehicle option first, then activate the route.')
|
||||
->send();
|
||||
|
||||
$this->halt();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Routing\Filament\Resources\EvRoutes\Pages;
|
||||
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
use Modules\Routing\Filament\Resources\EvRoutes\EvRouteResource;
|
||||
|
||||
class EditEvRoute extends EditRecord
|
||||
{
|
||||
protected static string $resource = EvRouteResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function beforeSave(): void
|
||||
{
|
||||
if (
|
||||
($this->data['is_active'] ?? false)
|
||||
&& ! EvRouteResource::hasCompletePricingData($this->data['pricing'] ?? [])
|
||||
) {
|
||||
Notification::make()
|
||||
->warning()
|
||||
->title('Route cannot be activated yet')
|
||||
->body('Set a price above 0 for every non-blocked vehicle option first, then activate the route.')
|
||||
->send();
|
||||
|
||||
$this->halt();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Routing\Filament\Resources\EvRoutes\Pages;
|
||||
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
use Modules\Routing\Filament\Resources\EvRoutes\EvRouteResource;
|
||||
|
||||
class ListEvRoutes extends ListRecords
|
||||
{
|
||||
protected static string $resource = EvRouteResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Routing\Filament\Resources\EvRoutes\Schemas;
|
||||
|
||||
use Filament\Forms\Components\Repeater;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Schemas\Schema;
|
||||
use Modules\Shared\Enums\VehicleOption;
|
||||
|
||||
class EvRouteForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
Select::make('ev_company_id')
|
||||
->label('EV Company')
|
||||
->relationship('company', 'name')
|
||||
->required()
|
||||
->searchable()
|
||||
->preload(),
|
||||
Select::make('from_destination_id')
|
||||
->label('From')
|
||||
->relationship('fromDestination', 'name')
|
||||
->required()
|
||||
->searchable()
|
||||
->preload(),
|
||||
Select::make('to_destination_id')
|
||||
->label('To')
|
||||
->relationship('toDestination', 'name')
|
||||
->required()
|
||||
->searchable()
|
||||
->preload()
|
||||
->different('from_destination_id')
|
||||
->validationMessages([
|
||||
'different' => 'The destination must be different from the origin.',
|
||||
]),
|
||||
Select::make('timeSlots')
|
||||
->label('Departure Time Slots')
|
||||
->relationship('timeSlots', 'label')
|
||||
->multiple()
|
||||
->searchable()
|
||||
->preload(),
|
||||
Toggle::make('is_round_trip')
|
||||
->required()
|
||||
->default(false),
|
||||
Toggle::make('is_active')
|
||||
->required()
|
||||
->default(false)
|
||||
->helperText('Every non-blocked vehicle option must have a price above 0 before a route can be activated.'),
|
||||
Repeater::make('pricing')
|
||||
->relationship()
|
||||
->label('Pricing')
|
||||
->schema([
|
||||
Select::make('vehicle_option')
|
||||
->options(array_combine(
|
||||
array_map(fn (VehicleOption $option) => $option->value, VehicleOption::cases()),
|
||||
array_map(fn (VehicleOption $option) => str($option->value)->headline()->toString(), VehicleOption::cases()),
|
||||
))
|
||||
->disabled()
|
||||
->dehydrated()
|
||||
->required(),
|
||||
TextInput::make('price')
|
||||
->numeric()
|
||||
->minValue(0)
|
||||
->required(),
|
||||
Toggle::make('is_blocked')
|
||||
->label('Blocked')
|
||||
->helperText('Hidden from booking regardless of price.'),
|
||||
])
|
||||
->columns(3)
|
||||
->default(
|
||||
collect(VehicleOption::cases())
|
||||
->map(fn (VehicleOption $option) => [
|
||||
'vehicle_option' => $option->value,
|
||||
'price' => 0,
|
||||
'is_blocked' => false,
|
||||
])
|
||||
->all()
|
||||
)
|
||||
->addable(false)
|
||||
->deletable(false)
|
||||
->reorderable(false)
|
||||
->columnSpanFull(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Routing\Filament\Resources\EvRoutes\Tables;
|
||||
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\TernaryFilter;
|
||||
use Filament\Tables\Table;
|
||||
use Modules\Routing\Models\EvRoute;
|
||||
use Modules\Routing\Models\RoutePricing;
|
||||
|
||||
class EvRoutesTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->modifyQueryUsing(fn ($query) => $query->with('pricing'))
|
||||
->columns([
|
||||
TextColumn::make('company.name')
|
||||
->label('EV Company')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
TextColumn::make('fromDestination.name')
|
||||
->label('From')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
TextColumn::make('toDestination.name')
|
||||
->label('To')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
TextColumn::make('pricing')
|
||||
->label('Pricing')
|
||||
->state(fn (EvRoute $record) => $record->pricing
|
||||
->sortBy(fn (RoutePricing $pricing) => $pricing->vehicle_option->value)
|
||||
->map(fn (RoutePricing $pricing) => str($pricing->vehicle_option->value)->headline()
|
||||
.': '
|
||||
.($pricing->is_blocked ? 'Blocked' : number_format($pricing->price, 0)))
|
||||
->all())
|
||||
->listWithLineBreaks(),
|
||||
IconColumn::make('is_round_trip')
|
||||
->boolean(),
|
||||
IconColumn::make('is_active')
|
||||
->boolean(),
|
||||
TextColumn::make('created_at')
|
||||
->dateTime()
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
])
|
||||
->filters([
|
||||
TernaryFilter::make('is_active'),
|
||||
TernaryFilter::make('is_round_trip'),
|
||||
])
|
||||
->recordActions([
|
||||
EditAction::make(),
|
||||
])
|
||||
->toolbarActions([
|
||||
BulkActionGroup::make([
|
||||
DeleteBulkAction::make(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user