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:
@@ -4,7 +4,9 @@ namespace Modules\Catalog\Models;
|
|||||||
|
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
use Modules\Catalog\Database\Factories\DepartureTimeSlotFactory;
|
use Modules\Catalog\Database\Factories\DepartureTimeSlotFactory;
|
||||||
|
use Modules\Routing\Models\EvRoute;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A shared catalog of departure times, attached to routes via a pivot in the
|
* A shared catalog of departure times, attached to routes via a pivot in the
|
||||||
@@ -34,4 +36,11 @@ class DepartureTimeSlot extends Model
|
|||||||
'is_active' => 'boolean',
|
'is_active' => 'boolean',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function routes(): BelongsToMany
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(EvRoute::class, 'ev_route_time_slots')
|
||||||
|
->withPivot('is_active')
|
||||||
|
->withTimestamps();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Routing\Database\Factories;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
use Modules\Catalog\Models\Destination;
|
||||||
|
use Modules\Catalog\Models\EvCompany;
|
||||||
|
use Modules\Routing\Models\EvRoute;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends Factory<EvRoute>
|
||||||
|
*/
|
||||||
|
class EvRouteFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'ev_company_id' => EvCompany::factory(),
|
||||||
|
'from_destination_id' => Destination::factory(),
|
||||||
|
'to_destination_id' => Destination::factory(),
|
||||||
|
'is_round_trip' => false,
|
||||||
|
'is_active' => true,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Routing\Database\Factories;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
use Modules\Routing\Models\EvRoute;
|
||||||
|
use Modules\Routing\Models\RoutePricing;
|
||||||
|
use Modules\Shared\Enums\VehicleOption;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends Factory<RoutePricing>
|
||||||
|
*/
|
||||||
|
class RoutePricingFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'ev_route_id' => EvRoute::factory(),
|
||||||
|
'vehicle_option' => fake()->randomElement(VehicleOption::cases()),
|
||||||
|
'price' => fake()->randomFloat(2, 5000, 50000),
|
||||||
|
'is_blocked' => false,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('ev_routes', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('ev_company_id')->constrained('ev_companies')->cascadeOnDelete();
|
||||||
|
$table->foreignId('from_destination_id')->constrained('destinations')->cascadeOnDelete();
|
||||||
|
$table->foreignId('to_destination_id')->constrained('destinations')->cascadeOnDelete();
|
||||||
|
$table->boolean('is_round_trip')->default(false);
|
||||||
|
$table->boolean('is_active')->default(true);
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('ev_routes');
|
||||||
|
}
|
||||||
|
};
|
||||||
+32
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('ev_route_time_slots', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('ev_route_id')->constrained('ev_routes')->cascadeOnDelete();
|
||||||
|
$table->foreignId('departure_time_slot_id')->constrained('departure_time_slots')->cascadeOnDelete();
|
||||||
|
$table->boolean('is_active')->default(true);
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['ev_route_id', 'departure_time_slot_id']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('ev_route_time_slots');
|
||||||
|
}
|
||||||
|
};
|
||||||
+32
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('route_pricing', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('ev_route_id')->constrained('ev_routes')->cascadeOnDelete();
|
||||||
|
$table->string('vehicle_option');
|
||||||
|
$table->decimal('price', 10, 2);
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['ev_route_id', 'vehicle_option']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('route_pricing');
|
||||||
|
}
|
||||||
|
};
|
||||||
+28
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('route_pricing', function (Blueprint $table) {
|
||||||
|
$table->boolean('is_blocked')->default(false)->after('price');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('route_pricing', function (Blueprint $table) {
|
||||||
|
$table->dropColumn('is_blocked');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -1 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Route;
|
||||||
|
use Modules\Routing\Http\Controllers\EvRouteController;
|
||||||
|
|
||||||
|
Route::prefix('api/v1')->middleware(['api', 'auth:sanctum', 'throttle:60,1'])->group(function () {
|
||||||
|
Route::get('/routes', [EvRouteController::class, 'index'])->name('routing.routes.index');
|
||||||
|
Route::get('/routes/{route}', [EvRouteController::class, 'show'])->name('routing.routes.show');
|
||||||
|
Route::get('/routes/{route}/pricing', [EvRouteController::class, 'pricing'])->name('routing.routes.pricing');
|
||||||
|
Route::get('/routes/{route}/time-slots', [EvRouteController::class, 'timeSlots'])->name('routing.routes.time-slots');
|
||||||
|
});
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Routing\Data;
|
||||||
|
|
||||||
|
use Modules\Shared\Enums\VehicleOption;
|
||||||
|
|
||||||
|
readonly class PriceQuoteData
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public int $evRouteId,
|
||||||
|
public VehicleOption $vehicleOption,
|
||||||
|
public string $price,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Routing\Exceptions;
|
||||||
|
|
||||||
|
use Modules\Routing\Models\EvRoute;
|
||||||
|
use Modules\Shared\Enums\VehicleOption;
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
class RoutePricingNotFoundException extends RuntimeException
|
||||||
|
{
|
||||||
|
public static function forRouteAndOption(EvRoute $route, VehicleOption $vehicleOption): self
|
||||||
|
{
|
||||||
|
return new self("No pricing found for route [{$route->id}] and vehicle option [{$vehicleOption->value}].");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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(),
|
||||||
|
]),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Routing\Http\Controllers;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||||
|
use Illuminate\Routing\Controller;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
use Modules\Routing\Http\Resources\EvRouteResource;
|
||||||
|
use Modules\Routing\Http\Resources\RoutePricingResource;
|
||||||
|
use Modules\Routing\Http\Resources\RouteTimeSlotResource;
|
||||||
|
use Modules\Routing\Models\EvRoute;
|
||||||
|
|
||||||
|
class EvRouteController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var list<string>
|
||||||
|
*/
|
||||||
|
private const EAGER_LOADS = ['company', 'fromDestination', 'toDestination', 'timeSlots', 'pricing'];
|
||||||
|
|
||||||
|
private const CACHE_TAG = 'routes';
|
||||||
|
|
||||||
|
private const CACHE_TTL_MINUTES = 5;
|
||||||
|
|
||||||
|
public function index(Request $request): AnonymousResourceCollection
|
||||||
|
{
|
||||||
|
$filters = $request->only(['company', 'from', 'to', 'date']);
|
||||||
|
|
||||||
|
$routes = Cache::tags(self::CACHE_TAG)->remember(
|
||||||
|
'routes:index:'.md5(json_encode($filters)),
|
||||||
|
now()->addMinutes(self::CACHE_TTL_MINUTES),
|
||||||
|
fn () => EvRoute::query()
|
||||||
|
->where('is_active', true)
|
||||||
|
->when($request->filled('company'), fn ($query) => $query->where('ev_company_id', $request->integer('company')))
|
||||||
|
->when($request->filled('from'), fn ($query) => $query->where('from_destination_id', $request->integer('from')))
|
||||||
|
->when($request->filled('to'), fn ($query) => $query->where('to_destination_id', $request->integer('to')))
|
||||||
|
// `date` is accepted for forward-compatibility with future per-date capacity
|
||||||
|
// checks (domain.md §7), but v1 has no route-level calendar to filter against.
|
||||||
|
->with(self::EAGER_LOADS)
|
||||||
|
->get(),
|
||||||
|
);
|
||||||
|
|
||||||
|
return EvRouteResource::collection($routes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function show(EvRoute $route): EvRouteResource
|
||||||
|
{
|
||||||
|
abort_unless($route->is_active, 404);
|
||||||
|
|
||||||
|
$route = Cache::tags(self::CACHE_TAG)->remember(
|
||||||
|
"routes:show:{$route->id}",
|
||||||
|
now()->addMinutes(self::CACHE_TTL_MINUTES),
|
||||||
|
fn () => $route->load(self::EAGER_LOADS),
|
||||||
|
);
|
||||||
|
|
||||||
|
return new EvRouteResource($route);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function pricing(EvRoute $route): AnonymousResourceCollection
|
||||||
|
{
|
||||||
|
abort_unless($route->is_active, 404);
|
||||||
|
|
||||||
|
$pricing = Cache::tags(self::CACHE_TAG)->remember(
|
||||||
|
"routes:pricing:{$route->id}",
|
||||||
|
now()->addMinutes(self::CACHE_TTL_MINUTES),
|
||||||
|
fn () => $route->pricing()->get(),
|
||||||
|
);
|
||||||
|
|
||||||
|
return RoutePricingResource::collection($pricing);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function timeSlots(EvRoute $route): AnonymousResourceCollection
|
||||||
|
{
|
||||||
|
abort_unless($route->is_active, 404);
|
||||||
|
|
||||||
|
$timeSlots = Cache::tags(self::CACHE_TAG)->remember(
|
||||||
|
"routes:time-slots:{$route->id}",
|
||||||
|
now()->addMinutes(self::CACHE_TTL_MINUTES),
|
||||||
|
fn () => $route->timeSlots()->get(),
|
||||||
|
);
|
||||||
|
|
||||||
|
return RouteTimeSlotResource::collection($timeSlots);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Routing\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
use Modules\Catalog\Http\Resources\DestinationResource;
|
||||||
|
use Modules\Catalog\Http\Resources\EvCompanyResource;
|
||||||
|
|
||||||
|
class EvRouteResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'is_round_trip' => $this->is_round_trip,
|
||||||
|
'is_active' => $this->is_active,
|
||||||
|
'company' => new EvCompanyResource($this->whenLoaded('company')),
|
||||||
|
'from_destination' => new DestinationResource($this->whenLoaded('fromDestination')),
|
||||||
|
'to_destination' => new DestinationResource($this->whenLoaded('toDestination')),
|
||||||
|
'time_slots' => RouteTimeSlotResource::collection($this->whenLoaded('timeSlots')),
|
||||||
|
'pricing' => RoutePricingResource::collection($this->whenLoaded('pricing')),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Routing\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class RoutePricingResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'vehicle_option' => $this->vehicle_option->value,
|
||||||
|
'price' => (string) $this->price,
|
||||||
|
'is_blocked' => $this->is_blocked,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Routing\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class RouteTimeSlotResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'label' => $this->label,
|
||||||
|
'time' => $this->time?->format('H:i'),
|
||||||
|
'is_active' => $this->whenPivotLoaded('ev_route_time_slots', fn () => (bool) $this->pivot->is_active),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Routing\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use Modules\Catalog\Models\DepartureTimeSlot;
|
||||||
|
use Modules\Catalog\Models\Destination;
|
||||||
|
use Modules\Catalog\Models\EvCompany;
|
||||||
|
use Modules\Routing\Database\Factories\EvRouteFactory;
|
||||||
|
|
||||||
|
class EvRoute extends Model
|
||||||
|
{
|
||||||
|
/** @use HasFactory<EvRouteFactory> */
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var list<string>
|
||||||
|
*/
|
||||||
|
protected $fillable = [
|
||||||
|
'ev_company_id',
|
||||||
|
'from_destination_id',
|
||||||
|
'to_destination_id',
|
||||||
|
'is_round_trip',
|
||||||
|
'is_active',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, string>
|
||||||
|
*/
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'is_round_trip' => 'boolean',
|
||||||
|
'is_active' => 'boolean',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function booted(): void
|
||||||
|
{
|
||||||
|
static::saving(function (self $route): void {
|
||||||
|
if ($route->from_destination_id === $route->to_destination_id) {
|
||||||
|
throw new InvalidArgumentException("A route's from and to destinations must be different.");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function company(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(EvCompany::class, 'ev_company_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function fromDestination(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Destination::class, 'from_destination_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toDestination(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Destination::class, 'to_destination_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function timeSlots(): BelongsToMany
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(DepartureTimeSlot::class, 'ev_route_time_slots')
|
||||||
|
->withPivot('is_active')
|
||||||
|
->withTimestamps();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function pricing(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(RoutePricing::class, 'ev_route_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Routing\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Modules\Routing\Database\Factories\RoutePricingFactory;
|
||||||
|
use Modules\Shared\Enums\VehicleOption;
|
||||||
|
|
||||||
|
class RoutePricing extends Model
|
||||||
|
{
|
||||||
|
/** @use HasFactory<RoutePricingFactory> */
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
protected $table = 'route_pricing';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var list<string>
|
||||||
|
*/
|
||||||
|
protected $fillable = [
|
||||||
|
'ev_route_id',
|
||||||
|
'vehicle_option',
|
||||||
|
'price',
|
||||||
|
'is_blocked',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, string>
|
||||||
|
*/
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'vehicle_option' => VehicleOption::class,
|
||||||
|
'price' => 'decimal:2',
|
||||||
|
'is_blocked' => 'boolean',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function route(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(EvRoute::class, 'ev_route_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Routing\Observers;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
use Modules\Routing\Models\EvRoute;
|
||||||
|
|
||||||
|
class EvRouteObserver
|
||||||
|
{
|
||||||
|
public function saved(EvRoute $route): void
|
||||||
|
{
|
||||||
|
Cache::tags('routes')->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deleted(EvRoute $route): void
|
||||||
|
{
|
||||||
|
Cache::tags('routes')->flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Routing\Observers;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
use Modules\Routing\Models\RoutePricing;
|
||||||
|
|
||||||
|
class RoutePricingObserver
|
||||||
|
{
|
||||||
|
public function saved(RoutePricing $pricing): void
|
||||||
|
{
|
||||||
|
Cache::tags('routes')->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deleted(RoutePricing $pricing): void
|
||||||
|
{
|
||||||
|
Cache::tags('routes')->flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,10 @@ namespace Modules\Routing\Providers;
|
|||||||
|
|
||||||
use Illuminate\Contracts\Auth\Access\Gate;
|
use Illuminate\Contracts\Auth\Access\Gate;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
use Modules\Routing\Models\EvRoute;
|
||||||
|
use Modules\Routing\Models\RoutePricing;
|
||||||
|
use Modules\Routing\Observers\EvRouteObserver;
|
||||||
|
use Modules\Routing\Observers\RoutePricingObserver;
|
||||||
use Modules\Routing\Policies\RoutePolicy;
|
use Modules\Routing\Policies\RoutePolicy;
|
||||||
|
|
||||||
class RoutingServiceProvider extends ServiceProvider
|
class RoutingServiceProvider extends ServiceProvider
|
||||||
@@ -12,6 +16,9 @@ class RoutingServiceProvider extends ServiceProvider
|
|||||||
|
|
||||||
public function boot(Gate $gate): void
|
public function boot(Gate $gate): void
|
||||||
{
|
{
|
||||||
$gate->policy('Modules\Routing\Models\EvRoute', RoutePolicy::class);
|
$gate->policy(EvRoute::class, RoutePolicy::class);
|
||||||
|
|
||||||
|
EvRoute::observe(EvRouteObserver::class);
|
||||||
|
RoutePricing::observe(RoutePricingObserver::class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Routing;
|
||||||
|
|
||||||
|
use Filament\Contracts\Plugin;
|
||||||
|
use Filament\Panel;
|
||||||
|
|
||||||
|
class RoutingPlugin implements Plugin
|
||||||
|
{
|
||||||
|
public function getId(): string
|
||||||
|
{
|
||||||
|
return 'routing';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function register(Panel $panel): void
|
||||||
|
{
|
||||||
|
$panel
|
||||||
|
->discoverResources(
|
||||||
|
in: __DIR__.'/Filament/Resources',
|
||||||
|
for: 'Modules\Routing\Filament\Resources',
|
||||||
|
)
|
||||||
|
->discoverPages(
|
||||||
|
in: __DIR__.'/Filament/Pages',
|
||||||
|
for: 'Modules\Routing\Filament\Pages',
|
||||||
|
)
|
||||||
|
->discoverWidgets(
|
||||||
|
in: __DIR__.'/Filament/Widgets',
|
||||||
|
for: 'Modules\Routing\Filament\Widgets',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function boot(Panel $panel): void {}
|
||||||
|
|
||||||
|
public static function make(): static
|
||||||
|
{
|
||||||
|
return app(static::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Routing\Services;
|
||||||
|
|
||||||
|
use Modules\Routing\Data\PriceQuoteData;
|
||||||
|
use Modules\Routing\Exceptions\RoutePricingNotFoundException;
|
||||||
|
use Modules\Routing\Models\EvRoute;
|
||||||
|
use Modules\Shared\Enums\VehicleOption;
|
||||||
|
|
||||||
|
class PricingService
|
||||||
|
{
|
||||||
|
public function quote(EvRoute $route, VehicleOption $vehicleOption): PriceQuoteData
|
||||||
|
{
|
||||||
|
$pricing = $route->pricing()
|
||||||
|
->where('vehicle_option', $vehicleOption)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if (! $pricing) {
|
||||||
|
throw RoutePricingNotFoundException::forRouteAndOption($route, $vehicleOption);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new PriceQuoteData(
|
||||||
|
evRouteId: $route->id,
|
||||||
|
vehicleOption: $vehicleOption,
|
||||||
|
price: (string) $pricing->price,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,234 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Livewire\Livewire;
|
||||||
|
use Modules\Catalog\Models\Destination;
|
||||||
|
use Modules\Catalog\Models\EvCompany;
|
||||||
|
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\Models\EvRoute;
|
||||||
|
use Modules\Routing\Models\RoutePricing;
|
||||||
|
use Modules\Shared\Enums\VehicleOption;
|
||||||
|
use Spatie\Permission\Models\Permission;
|
||||||
|
|
||||||
|
use function Pest\Laravel\assertDatabaseHas;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
Permission::findOrCreate('manage_routes', 'web');
|
||||||
|
|
||||||
|
$this->admin = User::factory()->create()->givePermissionTo('manage_routes');
|
||||||
|
$this->actingAs($this->admin);
|
||||||
|
});
|
||||||
|
|
||||||
|
function pricingPayload(array $overrides = []): array
|
||||||
|
{
|
||||||
|
return collect(VehicleOption::cases())
|
||||||
|
->map(fn (VehicleOption $option) => array_merge([
|
||||||
|
'vehicle_option' => $option->value,
|
||||||
|
'price' => 0,
|
||||||
|
'is_blocked' => false,
|
||||||
|
], $overrides[$option->value] ?? []))
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
test('can list ev routes', function () {
|
||||||
|
$routes = EvRoute::factory()->count(3)->create();
|
||||||
|
|
||||||
|
Livewire::test(ListEvRoutes::class)
|
||||||
|
->assertOk()
|
||||||
|
->assertCanSeeTableRecords($routes);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('list shows each vehicle option price stacked, and blocked options instead of a price', function () {
|
||||||
|
$route = EvRoute::factory()->create();
|
||||||
|
|
||||||
|
RoutePricing::factory()->create(['ev_route_id' => $route->id, 'vehicle_option' => VehicleOption::FrontSeat, 'price' => 12000]);
|
||||||
|
RoutePricing::factory()->create(['ev_route_id' => $route->id, 'vehicle_option' => VehicleOption::BackSeat, 'price' => 9000]);
|
||||||
|
RoutePricing::factory()->create(['ev_route_id' => $route->id, 'vehicle_option' => VehicleOption::WholeVehicle, 'is_blocked' => true]);
|
||||||
|
|
||||||
|
Livewire::test(ListEvRoutes::class)
|
||||||
|
->assertOk()
|
||||||
|
->assertTableColumnStateSet('pricing', ['Back Seat: 9,000', 'Front Seat: 12,000', 'Whole Vehicle: Blocked'], $route);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('creating a route also creates all three vehicle option pricing rows, defaulting to 0', function () {
|
||||||
|
$company = EvCompany::factory()->create();
|
||||||
|
$from = Destination::factory()->create();
|
||||||
|
$to = Destination::factory()->create();
|
||||||
|
|
||||||
|
Livewire::test(CreateEvRoute::class)
|
||||||
|
->fillForm([
|
||||||
|
'ev_company_id' => $company->id,
|
||||||
|
'from_destination_id' => $from->id,
|
||||||
|
'to_destination_id' => $to->id,
|
||||||
|
'is_round_trip' => false,
|
||||||
|
'is_active' => false,
|
||||||
|
'pricing' => pricingPayload(),
|
||||||
|
])
|
||||||
|
->call('create')
|
||||||
|
->assertNotified()
|
||||||
|
->assertRedirect();
|
||||||
|
|
||||||
|
$route = EvRoute::where('ev_company_id', $company->id)->firstOrFail();
|
||||||
|
|
||||||
|
expect($route->pricing)->toHaveCount(3);
|
||||||
|
|
||||||
|
foreach (VehicleOption::cases() as $option) {
|
||||||
|
assertDatabaseHas(RoutePricing::class, [
|
||||||
|
'ev_route_id' => $route->id,
|
||||||
|
'vehicle_option' => $option->value,
|
||||||
|
'price' => '0.00',
|
||||||
|
'is_blocked' => false,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can price all three vehicle options directly on the create form', function () {
|
||||||
|
$company = EvCompany::factory()->create();
|
||||||
|
$from = Destination::factory()->create();
|
||||||
|
$to = Destination::factory()->create();
|
||||||
|
|
||||||
|
Livewire::test(CreateEvRoute::class)
|
||||||
|
->fillForm([
|
||||||
|
'ev_company_id' => $company->id,
|
||||||
|
'from_destination_id' => $from->id,
|
||||||
|
'to_destination_id' => $to->id,
|
||||||
|
'is_active' => true,
|
||||||
|
'pricing' => pricingPayload([
|
||||||
|
'front_seat' => ['price' => 12000],
|
||||||
|
'back_seat' => ['price' => 9000],
|
||||||
|
'whole_vehicle' => ['price' => 30000],
|
||||||
|
]),
|
||||||
|
])
|
||||||
|
->call('create')
|
||||||
|
->assertNotified()
|
||||||
|
->assertRedirect();
|
||||||
|
|
||||||
|
$route = EvRoute::where('ev_company_id', $company->id)->firstOrFail();
|
||||||
|
|
||||||
|
expect($route->is_active)->toBeTrue();
|
||||||
|
|
||||||
|
assertDatabaseHas(RoutePricing::class, ['ev_route_id' => $route->id, 'vehicle_option' => 'front_seat', 'price' => 12000]);
|
||||||
|
assertDatabaseHas(RoutePricing::class, ['ev_route_id' => $route->id, 'vehicle_option' => 'back_seat', 'price' => 9000]);
|
||||||
|
assertDatabaseHas(RoutePricing::class, ['ev_route_id' => $route->id, 'vehicle_option' => 'whole_vehicle', 'price' => 30000]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('cannot create a route with the same from and to destination', function () {
|
||||||
|
$company = EvCompany::factory()->create();
|
||||||
|
$destination = Destination::factory()->create();
|
||||||
|
|
||||||
|
Livewire::test(CreateEvRoute::class)
|
||||||
|
->fillForm([
|
||||||
|
'ev_company_id' => $company->id,
|
||||||
|
'from_destination_id' => $destination->id,
|
||||||
|
'to_destination_id' => $destination->id,
|
||||||
|
'pricing' => pricingPayload(),
|
||||||
|
])
|
||||||
|
->call('create')
|
||||||
|
->assertHasFormErrors(['to_destination_id' => 'different'])
|
||||||
|
->assertNotNotified()
|
||||||
|
->assertNoRedirect();
|
||||||
|
|
||||||
|
$this->assertDatabaseCount(EvRoute::class, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('cannot create a route as active while any non-blocked vehicle option is priced at 0', function () {
|
||||||
|
$company = EvCompany::factory()->create();
|
||||||
|
$from = Destination::factory()->create();
|
||||||
|
$to = Destination::factory()->create();
|
||||||
|
|
||||||
|
Livewire::test(CreateEvRoute::class)
|
||||||
|
->fillForm([
|
||||||
|
'ev_company_id' => $company->id,
|
||||||
|
'from_destination_id' => $from->id,
|
||||||
|
'to_destination_id' => $to->id,
|
||||||
|
'is_active' => true,
|
||||||
|
'pricing' => pricingPayload([
|
||||||
|
'front_seat' => ['price' => 12000],
|
||||||
|
// back_seat and whole_vehicle left at 0.
|
||||||
|
]),
|
||||||
|
])
|
||||||
|
->call('create')
|
||||||
|
->assertNotified();
|
||||||
|
|
||||||
|
$this->assertDatabaseCount(EvRoute::class, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a blocked vehicle option does not need a price to create an active route', function () {
|
||||||
|
$company = EvCompany::factory()->create();
|
||||||
|
$from = Destination::factory()->create();
|
||||||
|
$to = Destination::factory()->create();
|
||||||
|
|
||||||
|
Livewire::test(CreateEvRoute::class)
|
||||||
|
->fillForm([
|
||||||
|
'ev_company_id' => $company->id,
|
||||||
|
'from_destination_id' => $from->id,
|
||||||
|
'to_destination_id' => $to->id,
|
||||||
|
'is_active' => true,
|
||||||
|
'pricing' => pricingPayload([
|
||||||
|
'front_seat' => ['price' => 12000],
|
||||||
|
'back_seat' => ['price' => 9000],
|
||||||
|
'whole_vehicle' => ['is_blocked' => true],
|
||||||
|
]),
|
||||||
|
])
|
||||||
|
->call('create')
|
||||||
|
->assertNotified()
|
||||||
|
->assertRedirect();
|
||||||
|
|
||||||
|
$route = EvRoute::where('ev_company_id', $company->id)->firstOrFail();
|
||||||
|
|
||||||
|
expect($route->is_active)->toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can update pricing and toggle blocked from the edit page', function () {
|
||||||
|
$route = EvRoute::factory()->create();
|
||||||
|
|
||||||
|
foreach (VehicleOption::cases() as $option) {
|
||||||
|
RoutePricing::factory()->create([
|
||||||
|
'ev_route_id' => $route->id,
|
||||||
|
'vehicle_option' => $option,
|
||||||
|
'price' => 5000,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Livewire::test(EditEvRoute::class, ['record' => $route->getRouteKey()])
|
||||||
|
->assertOk()
|
||||||
|
->fillForm([
|
||||||
|
'pricing' => pricingPayload([
|
||||||
|
'front_seat' => ['price' => 15000],
|
||||||
|
'back_seat' => ['price' => 8000],
|
||||||
|
'whole_vehicle' => ['is_blocked' => true],
|
||||||
|
]),
|
||||||
|
])
|
||||||
|
->call('save')
|
||||||
|
->assertNotified();
|
||||||
|
|
||||||
|
assertDatabaseHas(RoutePricing::class, ['ev_route_id' => $route->id, 'vehicle_option' => 'front_seat', 'price' => 15000]);
|
||||||
|
assertDatabaseHas(RoutePricing::class, ['ev_route_id' => $route->id, 'vehicle_option' => 'back_seat', 'price' => 8000]);
|
||||||
|
assertDatabaseHas(RoutePricing::class, ['ev_route_id' => $route->id, 'vehicle_option' => 'whole_vehicle', 'is_blocked' => true]);
|
||||||
|
|
||||||
|
expect($route->pricing()->count())->toBe(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('cannot activate a route from the edit page while any non-blocked vehicle option is priced at 0', function () {
|
||||||
|
$route = EvRoute::factory()->create(['is_active' => false]);
|
||||||
|
foreach (VehicleOption::cases() as $option) {
|
||||||
|
RoutePricing::factory()->create(['ev_route_id' => $route->id, 'vehicle_option' => $option]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Livewire::test(EditEvRoute::class, ['record' => $route->getRouteKey()])
|
||||||
|
->assertOk()
|
||||||
|
->fillForm([
|
||||||
|
'is_active' => true,
|
||||||
|
'pricing' => pricingPayload([
|
||||||
|
'front_seat' => ['price' => 12000],
|
||||||
|
// back_seat and whole_vehicle left at 0.
|
||||||
|
]),
|
||||||
|
])
|
||||||
|
->call('save')
|
||||||
|
->assertNotified();
|
||||||
|
|
||||||
|
expect($route->fresh()->is_active)->toBeFalse();
|
||||||
|
});
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\QueryException;
|
||||||
|
use Modules\Catalog\Models\DepartureTimeSlot;
|
||||||
|
use Modules\Catalog\Models\Destination;
|
||||||
|
use Modules\Catalog\Models\EvCompany;
|
||||||
|
use Modules\Routing\Models\EvRoute;
|
||||||
|
|
||||||
|
test('an ev route belongs to a company and two destinations', function () {
|
||||||
|
$company = EvCompany::factory()->create();
|
||||||
|
$from = Destination::factory()->create();
|
||||||
|
$to = Destination::factory()->create();
|
||||||
|
|
||||||
|
$route = EvRoute::factory()->create([
|
||||||
|
'ev_company_id' => $company->id,
|
||||||
|
'from_destination_id' => $from->id,
|
||||||
|
'to_destination_id' => $to->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect($route->company)->toBeInstanceOf(EvCompany::class)
|
||||||
|
->and($route->company->is($company))->toBeTrue()
|
||||||
|
->and($route->fromDestination)->toBeInstanceOf(Destination::class)
|
||||||
|
->and($route->fromDestination->is($from))->toBeTrue()
|
||||||
|
->and($route->toDestination)->toBeInstanceOf(Destination::class)
|
||||||
|
->and($route->toDestination->is($to))->toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('is_round_trip and is_active cast to boolean', function () {
|
||||||
|
$route = EvRoute::factory()->create([
|
||||||
|
'is_round_trip' => 1,
|
||||||
|
'is_active' => 0,
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect($route->is_round_trip)->toBeTrue()
|
||||||
|
->and($route->is_active)->toBeFalse();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a route can be attached to time slots via the pivot, carrying its own is_active flag', function () {
|
||||||
|
$route = EvRoute::factory()->create();
|
||||||
|
$morningSlot = DepartureTimeSlot::factory()->create();
|
||||||
|
$eveningSlot = DepartureTimeSlot::factory()->create();
|
||||||
|
|
||||||
|
$route->timeSlots()->attach([
|
||||||
|
$morningSlot->id => ['is_active' => true],
|
||||||
|
$eveningSlot->id => ['is_active' => false],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$route->refresh();
|
||||||
|
|
||||||
|
expect($route->timeSlots)->toHaveCount(2);
|
||||||
|
|
||||||
|
$attachedMorning = $route->timeSlots->firstWhere('id', $morningSlot->id);
|
||||||
|
$attachedEvening = $route->timeSlots->firstWhere('id', $eveningSlot->id);
|
||||||
|
|
||||||
|
expect($attachedMorning->pivot->is_active)->toBeTrue()
|
||||||
|
->and($attachedEvening->pivot->is_active)->toBeFalse();
|
||||||
|
|
||||||
|
expect($morningSlot->routes)->toHaveCount(1)
|
||||||
|
->and($morningSlot->routes->first()->is($route))->toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a route cannot have the same from and to destination', function () {
|
||||||
|
$destination = Destination::factory()->create();
|
||||||
|
|
||||||
|
expect(fn () => EvRoute::factory()->create([
|
||||||
|
'from_destination_id' => $destination->id,
|
||||||
|
'to_destination_id' => $destination->id,
|
||||||
|
]))->toThrow(InvalidArgumentException::class);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a route cannot be attached to the same time slot twice', function () {
|
||||||
|
$route = EvRoute::factory()->create();
|
||||||
|
$slot = DepartureTimeSlot::factory()->create();
|
||||||
|
|
||||||
|
$route->timeSlots()->attach($slot->id, ['is_active' => true]);
|
||||||
|
|
||||||
|
expect(fn () => $route->timeSlots()->attach($slot->id, ['is_active' => true]))
|
||||||
|
->toThrow(QueryException::class);
|
||||||
|
});
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\QueryException;
|
||||||
|
use Modules\Routing\Models\EvRoute;
|
||||||
|
use Modules\Routing\Models\RoutePricing;
|
||||||
|
use Modules\Shared\Enums\VehicleOption;
|
||||||
|
|
||||||
|
test('route pricing belongs to a route and casts its vehicle option, price and blocked flag', function () {
|
||||||
|
$route = EvRoute::factory()->create();
|
||||||
|
|
||||||
|
$pricing = RoutePricing::factory()->create([
|
||||||
|
'ev_route_id' => $route->id,
|
||||||
|
'vehicle_option' => VehicleOption::FrontSeat,
|
||||||
|
'price' => 12000,
|
||||||
|
'is_blocked' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect($pricing->route)->toBeInstanceOf(EvRoute::class)
|
||||||
|
->and($pricing->route->is($route))->toBeTrue()
|
||||||
|
->and($pricing->vehicle_option)->toBe(VehicleOption::FrontSeat)
|
||||||
|
->and($pricing->price)->toEqual('12000.00')
|
||||||
|
->and($pricing->is_blocked)->toBeTrue();
|
||||||
|
|
||||||
|
expect($route->pricing()->first()->is($pricing))->toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a route can only have one price per vehicle option', function () {
|
||||||
|
$route = EvRoute::factory()->create();
|
||||||
|
|
||||||
|
RoutePricing::factory()->create([
|
||||||
|
'ev_route_id' => $route->id,
|
||||||
|
'vehicle_option' => VehicleOption::WholeVehicle,
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(fn () => RoutePricing::factory()->create([
|
||||||
|
'ev_route_id' => $route->id,
|
||||||
|
'vehicle_option' => VehicleOption::WholeVehicle,
|
||||||
|
]))->toThrow(QueryException::class);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a route can have all three vehicle options priced', function () {
|
||||||
|
$route = EvRoute::factory()->create();
|
||||||
|
|
||||||
|
foreach (VehicleOption::cases() as $option) {
|
||||||
|
RoutePricing::factory()->create([
|
||||||
|
'ev_route_id' => $route->id,
|
||||||
|
'vehicle_option' => $option,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
expect($route->pricing()->count())->toBe(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('pricing defaults to unblocked', function () {
|
||||||
|
$pricing = RoutePricing::factory()->create();
|
||||||
|
|
||||||
|
expect($pricing->is_blocked)->toBeFalse();
|
||||||
|
});
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
use Modules\Routing\Models\EvRoute;
|
||||||
|
use Modules\Routing\Models\RoutePricing;
|
||||||
|
use Modules\Shared\Enums\VehicleOption;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
$this->token = User::factory()->create()->createToken('test-token')->plainTextToken;
|
||||||
|
});
|
||||||
|
|
||||||
|
test('the pricing endpoint serves a cached response until invalidated', function () {
|
||||||
|
$route = EvRoute::factory()->create(['is_active' => true]);
|
||||||
|
$pricing = RoutePricing::factory()->create([
|
||||||
|
'ev_route_id' => $route->id,
|
||||||
|
'vehicle_option' => VehicleOption::FrontSeat,
|
||||||
|
'price' => 10000,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||||
|
->getJson("/api/v1/routes/{$route->id}/pricing")
|
||||||
|
->assertJsonFragment(['price' => '10000.00']);
|
||||||
|
|
||||||
|
// Bypass Eloquent (no 'saved' event) so a stale cached response proves caching is active.
|
||||||
|
RoutePricing::query()->where('id', $pricing->id)->update(['price' => 99999]);
|
||||||
|
|
||||||
|
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||||
|
->getJson("/api/v1/routes/{$route->id}/pricing")
|
||||||
|
->assertJsonFragment(['price' => '10000.00']);
|
||||||
|
|
||||||
|
// A genuine Eloquent save fires the observer and flushes the 'routes' tag.
|
||||||
|
$pricing->refresh()->save();
|
||||||
|
|
||||||
|
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||||
|
->getJson("/api/v1/routes/{$route->id}/pricing")
|
||||||
|
->assertJsonFragment(['price' => '99999.00']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('saving an ev route invalidates the routes cache tag', function () {
|
||||||
|
$route = EvRoute::factory()->create(['is_active' => true]);
|
||||||
|
|
||||||
|
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||||
|
->getJson('/api/v1/routes')
|
||||||
|
->assertJsonCount(1, 'data');
|
||||||
|
|
||||||
|
// Bypass Eloquent so the change wouldn't be visible without invalidation.
|
||||||
|
EvRoute::query()->where('id', $route->id)->update(['is_active' => false]);
|
||||||
|
|
||||||
|
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||||
|
->getJson('/api/v1/routes')
|
||||||
|
->assertJsonCount(1, 'data');
|
||||||
|
|
||||||
|
$route->refresh()->save();
|
||||||
|
|
||||||
|
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||||
|
->getJson('/api/v1/routes')
|
||||||
|
->assertJsonCount(0, 'data');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('deleting an ev route invalidates the routes cache tag', function () {
|
||||||
|
$route = EvRoute::factory()->create(['is_active' => true]);
|
||||||
|
|
||||||
|
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||||
|
->getJson('/api/v1/routes')
|
||||||
|
->assertJsonCount(1, 'data');
|
||||||
|
|
||||||
|
$route->delete();
|
||||||
|
|
||||||
|
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||||
|
->getJson('/api/v1/routes')
|
||||||
|
->assertJsonCount(0, 'data');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('flushing the routes cache tag does not affect other cached data', function () {
|
||||||
|
Cache::put('unrelated-key', 'still here', now()->addMinutes(5));
|
||||||
|
|
||||||
|
$route = EvRoute::factory()->create(['is_active' => true]);
|
||||||
|
$route->update(['is_round_trip' => true]);
|
||||||
|
|
||||||
|
expect(Cache::get('unrelated-key'))->toBe('still here');
|
||||||
|
});
|
||||||
@@ -0,0 +1,145 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Modules\Catalog\Models\DepartureTimeSlot;
|
||||||
|
use Modules\Catalog\Models\Destination;
|
||||||
|
use Modules\Catalog\Models\EvCompany;
|
||||||
|
use Modules\Routing\Models\EvRoute;
|
||||||
|
use Modules\Routing\Models\RoutePricing;
|
||||||
|
use Modules\Shared\Enums\VehicleOption;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
$this->token = User::factory()->create()->createToken('test-token')->plainTextToken;
|
||||||
|
});
|
||||||
|
|
||||||
|
test('lists active routes with nested company, destinations, time slots and pricing', function () {
|
||||||
|
$route = EvRoute::factory()->create(['is_active' => true]);
|
||||||
|
EvRoute::factory()->create(['is_active' => false]);
|
||||||
|
|
||||||
|
$slot = DepartureTimeSlot::factory()->create();
|
||||||
|
$route->timeSlots()->attach($slot->id, ['is_active' => true]);
|
||||||
|
|
||||||
|
RoutePricing::factory()->create([
|
||||||
|
'ev_route_id' => $route->id,
|
||||||
|
'vehicle_option' => VehicleOption::FrontSeat,
|
||||||
|
'price' => 12000,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||||
|
->getJson('/api/v1/routes')
|
||||||
|
->assertSuccessful()
|
||||||
|
->assertJsonCount(1, 'data')
|
||||||
|
->assertJsonPath('data.0.id', $route->id)
|
||||||
|
->assertJsonPath('data.0.company.id', $route->ev_company_id)
|
||||||
|
->assertJsonPath('data.0.from_destination.id', $route->from_destination_id)
|
||||||
|
->assertJsonPath('data.0.to_destination.id', $route->to_destination_id)
|
||||||
|
->assertJsonPath('data.0.time_slots.0.id', $slot->id)
|
||||||
|
->assertJsonPath('data.0.time_slots.0.is_active', true)
|
||||||
|
->assertJsonPath('data.0.pricing.0.vehicle_option', 'front_seat')
|
||||||
|
->assertJsonPath('data.0.pricing.0.price', '12000.00');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('filters routes by company, from, and to', function () {
|
||||||
|
$companyA = EvCompany::factory()->create();
|
||||||
|
$companyB = EvCompany::factory()->create();
|
||||||
|
$yangon = Destination::factory()->create();
|
||||||
|
$mandalay = Destination::factory()->create();
|
||||||
|
$bagan = Destination::factory()->create();
|
||||||
|
|
||||||
|
$matching = EvRoute::factory()->create([
|
||||||
|
'ev_company_id' => $companyA->id,
|
||||||
|
'from_destination_id' => $yangon->id,
|
||||||
|
'to_destination_id' => $mandalay->id,
|
||||||
|
'is_active' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
EvRoute::factory()->create([
|
||||||
|
'ev_company_id' => $companyB->id,
|
||||||
|
'from_destination_id' => $yangon->id,
|
||||||
|
'to_destination_id' => $mandalay->id,
|
||||||
|
'is_active' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
EvRoute::factory()->create([
|
||||||
|
'ev_company_id' => $companyA->id,
|
||||||
|
'from_destination_id' => $yangon->id,
|
||||||
|
'to_destination_id' => $bagan->id,
|
||||||
|
'is_active' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||||
|
->getJson('/api/v1/routes?'.http_build_query([
|
||||||
|
'company' => $companyA->id,
|
||||||
|
'from' => $yangon->id,
|
||||||
|
'to' => $mandalay->id,
|
||||||
|
]))
|
||||||
|
->assertSuccessful()
|
||||||
|
->assertJsonCount(1, 'data')
|
||||||
|
->assertJsonPath('data.0.id', $matching->id);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('shows a single active route', function () {
|
||||||
|
$route = EvRoute::factory()->create(['is_active' => true]);
|
||||||
|
|
||||||
|
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||||
|
->getJson("/api/v1/routes/{$route->id}")
|
||||||
|
->assertSuccessful()
|
||||||
|
->assertJsonPath('data.id', $route->id);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('an inactive route is not found via show, pricing, or time-slots', function () {
|
||||||
|
$route = EvRoute::factory()->create(['is_active' => false]);
|
||||||
|
|
||||||
|
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||||
|
->getJson("/api/v1/routes/{$route->id}")
|
||||||
|
->assertNotFound();
|
||||||
|
|
||||||
|
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||||
|
->getJson("/api/v1/routes/{$route->id}/pricing")
|
||||||
|
->assertNotFound();
|
||||||
|
|
||||||
|
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||||
|
->getJson("/api/v1/routes/{$route->id}/time-slots")
|
||||||
|
->assertNotFound();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('lists a route\'s pricing including blocked options', function () {
|
||||||
|
$route = EvRoute::factory()->create(['is_active' => true]);
|
||||||
|
|
||||||
|
RoutePricing::factory()->create(['ev_route_id' => $route->id, 'vehicle_option' => VehicleOption::FrontSeat, 'price' => 12000]);
|
||||||
|
RoutePricing::factory()->create(['ev_route_id' => $route->id, 'vehicle_option' => VehicleOption::WholeVehicle, 'is_blocked' => true]);
|
||||||
|
|
||||||
|
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||||
|
->getJson("/api/v1/routes/{$route->id}/pricing")
|
||||||
|
->assertSuccessful()
|
||||||
|
->assertJsonCount(2, 'data')
|
||||||
|
->assertJsonFragment(['vehicle_option' => 'front_seat', 'price' => '12000.00', 'is_blocked' => false])
|
||||||
|
->assertJsonFragment(['vehicle_option' => 'whole_vehicle', 'is_blocked' => true]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('lists a route\'s time slots with the pivot active flag', function () {
|
||||||
|
$route = EvRoute::factory()->create(['is_active' => true]);
|
||||||
|
$active = DepartureTimeSlot::factory()->create();
|
||||||
|
$inactive = DepartureTimeSlot::factory()->create();
|
||||||
|
|
||||||
|
$route->timeSlots()->attach([
|
||||||
|
$active->id => ['is_active' => true],
|
||||||
|
$inactive->id => ['is_active' => false],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||||
|
->getJson("/api/v1/routes/{$route->id}/time-slots")
|
||||||
|
->assertSuccessful()
|
||||||
|
->assertJsonCount(2, 'data')
|
||||||
|
->assertJsonFragment(['id' => $active->id, 'is_active' => true])
|
||||||
|
->assertJsonFragment(['id' => $inactive->id, 'is_active' => false]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('routes endpoints reject unauthenticated requests', function () {
|
||||||
|
$route = EvRoute::factory()->create(['is_active' => true]);
|
||||||
|
|
||||||
|
$this->getJson('/api/v1/routes')->assertUnauthorized();
|
||||||
|
$this->getJson("/api/v1/routes/{$route->id}")->assertUnauthorized();
|
||||||
|
$this->getJson("/api/v1/routes/{$route->id}/pricing")->assertUnauthorized();
|
||||||
|
$this->getJson("/api/v1/routes/{$route->id}/time-slots")->assertUnauthorized();
|
||||||
|
});
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Modules\Routing\Data\PriceQuoteData;
|
||||||
|
use Modules\Routing\Exceptions\RoutePricingNotFoundException;
|
||||||
|
use Modules\Routing\Models\EvRoute;
|
||||||
|
use Modules\Routing\Models\RoutePricing;
|
||||||
|
use Modules\Routing\Services\PricingService;
|
||||||
|
use Modules\Shared\Enums\VehicleOption;
|
||||||
|
|
||||||
|
test('quote returns a price for each priced vehicle option on a route', function () {
|
||||||
|
$route = EvRoute::factory()->create();
|
||||||
|
|
||||||
|
RoutePricing::factory()->create([
|
||||||
|
'ev_route_id' => $route->id,
|
||||||
|
'vehicle_option' => VehicleOption::FrontSeat,
|
||||||
|
'price' => 12000,
|
||||||
|
]);
|
||||||
|
RoutePricing::factory()->create([
|
||||||
|
'ev_route_id' => $route->id,
|
||||||
|
'vehicle_option' => VehicleOption::BackSeat,
|
||||||
|
'price' => 9000,
|
||||||
|
]);
|
||||||
|
RoutePricing::factory()->create([
|
||||||
|
'ev_route_id' => $route->id,
|
||||||
|
'vehicle_option' => VehicleOption::WholeVehicle,
|
||||||
|
'price' => 30000,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$service = new PricingService;
|
||||||
|
|
||||||
|
$frontSeatQuote = $service->quote($route, VehicleOption::FrontSeat);
|
||||||
|
$backSeatQuote = $service->quote($route, VehicleOption::BackSeat);
|
||||||
|
$wholeVehicleQuote = $service->quote($route, VehicleOption::WholeVehicle);
|
||||||
|
|
||||||
|
expect($frontSeatQuote)->toBeInstanceOf(PriceQuoteData::class)
|
||||||
|
->and($frontSeatQuote->evRouteId)->toBe($route->id)
|
||||||
|
->and($frontSeatQuote->vehicleOption)->toBe(VehicleOption::FrontSeat)
|
||||||
|
->and($frontSeatQuote->price)->toEqual('12000.00');
|
||||||
|
|
||||||
|
expect($backSeatQuote->price)->toEqual('9000.00');
|
||||||
|
expect($wholeVehicleQuote->price)->toEqual('30000.00');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('quote throws when the route has no pricing for the requested vehicle option', function () {
|
||||||
|
$route = EvRoute::factory()->create();
|
||||||
|
|
||||||
|
$service = new PricingService;
|
||||||
|
|
||||||
|
expect(fn () => $service->quote($route, VehicleOption::WholeVehicle))
|
||||||
|
->toThrow(RoutePricingNotFoundException::class);
|
||||||
|
});
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Shared\Enums;
|
||||||
|
|
||||||
|
enum VehicleOption: string
|
||||||
|
{
|
||||||
|
case FrontSeat = 'front_seat';
|
||||||
|
case BackSeat = 'back_seat';
|
||||||
|
case WholeVehicle = 'whole_vehicle';
|
||||||
|
}
|
||||||
@@ -20,6 +20,7 @@ use Illuminate\Routing\Middleware\SubstituteBindings;
|
|||||||
use Illuminate\Session\Middleware\StartSession;
|
use Illuminate\Session\Middleware\StartSession;
|
||||||
use Illuminate\View\Middleware\ShareErrorsFromSession;
|
use Illuminate\View\Middleware\ShareErrorsFromSession;
|
||||||
use Modules\Catalog\CatalogPlugin;
|
use Modules\Catalog\CatalogPlugin;
|
||||||
|
use Modules\Routing\RoutingPlugin;
|
||||||
|
|
||||||
class AdminPanelProvider extends PanelProvider
|
class AdminPanelProvider extends PanelProvider
|
||||||
{
|
{
|
||||||
@@ -41,6 +42,7 @@ class AdminPanelProvider extends PanelProvider
|
|||||||
])
|
])
|
||||||
->plugins([
|
->plugins([
|
||||||
CatalogPlugin::make(),
|
CatalogPlugin::make(),
|
||||||
|
RoutingPlugin::make(),
|
||||||
])
|
])
|
||||||
->discoverResources(in: app_path('Filament/Resources'), for: 'App\Filament\Resources')
|
->discoverResources(in: app_path('Filament/Resources'), for: 'App\Filament\Resources')
|
||||||
->discoverPages(in: app_path('Filament/Pages'), for: 'App\Filament\Pages')
|
->discoverPages(in: app_path('Filament/Pages'), for: 'App\Filament\Pages')
|
||||||
|
|||||||
+1
-1
@@ -20,7 +20,7 @@ pest()->extend(TestCase::class)
|
|||||||
|
|
||||||
pest()->extend(TestCase::class)
|
pest()->extend(TestCase::class)
|
||||||
->use(RefreshDatabase::class)
|
->use(RefreshDatabase::class)
|
||||||
->in('../app-modules/*/tests/Feature');
|
->in('../app-modules/*/tests/Feature', '../app-modules/*/tests/Unit');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user