Add Departure Time Slot and Catalog read API (T2.4, T2.5)
Adds the shared DepartureTimeSlot catalog entity with its Filament resource, and public GET /api/v1/companies + /api/v1/destinations endpoints (auth:sanctum + throttled) for the mini app/mobile/agent clients to consume.
This commit is contained in:
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Database\Factories;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
use Modules\Catalog\Models\DepartureTimeSlot;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends Factory<DepartureTimeSlot>
|
||||||
|
*/
|
||||||
|
class DepartureTimeSlotFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
$time = Carbon::createFromTime(fake()->numberBetween(0, 23), fake()->randomElement([0, 15, 30, 45]));
|
||||||
|
|
||||||
|
return [
|
||||||
|
'label' => $time->format('h:i A'),
|
||||||
|
'time' => $time->format('H:i'),
|
||||||
|
'is_active' => true,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
+30
@@ -0,0 +1,30 @@
|
|||||||
|
<?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('departure_time_slots', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('label');
|
||||||
|
$table->time('time');
|
||||||
|
$table->boolean('is_active')->default(true);
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('departure_time_slots');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -1 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Route;
|
||||||
|
use Modules\Catalog\Http\Controllers\DestinationController;
|
||||||
|
use Modules\Catalog\Http\Controllers\EvCompanyController;
|
||||||
|
|
||||||
|
Route::prefix('api/v1')->middleware(['api', 'auth:sanctum', 'throttle:60,1'])->group(function () {
|
||||||
|
Route::get('/companies', [EvCompanyController::class, 'index'])->name('catalog.companies.index');
|
||||||
|
Route::get('/destinations', [DestinationController::class, 'index'])->name('catalog.destinations.index');
|
||||||
|
});
|
||||||
|
|||||||
+51
@@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Filament\Resources\DepartureTimeSlots;
|
||||||
|
|
||||||
|
use BackedEnum;
|
||||||
|
use Filament\Resources\Resource;
|
||||||
|
use Filament\Schemas\Schema;
|
||||||
|
use Filament\Support\Icons\Heroicon;
|
||||||
|
use Filament\Tables\Table;
|
||||||
|
use Modules\Catalog\Filament\Resources\DepartureTimeSlots\Pages\CreateDepartureTimeSlot;
|
||||||
|
use Modules\Catalog\Filament\Resources\DepartureTimeSlots\Pages\EditDepartureTimeSlot;
|
||||||
|
use Modules\Catalog\Filament\Resources\DepartureTimeSlots\Pages\ListDepartureTimeSlots;
|
||||||
|
use Modules\Catalog\Filament\Resources\DepartureTimeSlots\Schemas\DepartureTimeSlotForm;
|
||||||
|
use Modules\Catalog\Filament\Resources\DepartureTimeSlots\Tables\DepartureTimeSlotsTable;
|
||||||
|
use Modules\Catalog\Models\DepartureTimeSlot;
|
||||||
|
use UnitEnum;
|
||||||
|
|
||||||
|
class DepartureTimeSlotResource extends Resource
|
||||||
|
{
|
||||||
|
protected static ?string $model = DepartureTimeSlot::class;
|
||||||
|
|
||||||
|
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedClock;
|
||||||
|
|
||||||
|
protected static string|UnitEnum|null $navigationGroup = 'Catalog';
|
||||||
|
|
||||||
|
public static function form(Schema $schema): Schema
|
||||||
|
{
|
||||||
|
return DepartureTimeSlotForm::configure($schema);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function table(Table $table): Table
|
||||||
|
{
|
||||||
|
return DepartureTimeSlotsTable::configure($table);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getRelations(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
//
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getPages(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'index' => ListDepartureTimeSlots::route('/'),
|
||||||
|
'create' => CreateDepartureTimeSlot::route('/create'),
|
||||||
|
'edit' => EditDepartureTimeSlot::route('/{record}/edit'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Filament\Resources\DepartureTimeSlots\Pages;
|
||||||
|
|
||||||
|
use Filament\Resources\Pages\CreateRecord;
|
||||||
|
use Modules\Catalog\Filament\Resources\DepartureTimeSlots\DepartureTimeSlotResource;
|
||||||
|
|
||||||
|
class CreateDepartureTimeSlot extends CreateRecord
|
||||||
|
{
|
||||||
|
protected static string $resource = DepartureTimeSlotResource::class;
|
||||||
|
}
|
||||||
+19
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Filament\Resources\DepartureTimeSlots\Pages;
|
||||||
|
|
||||||
|
use Filament\Actions\DeleteAction;
|
||||||
|
use Filament\Resources\Pages\EditRecord;
|
||||||
|
use Modules\Catalog\Filament\Resources\DepartureTimeSlots\DepartureTimeSlotResource;
|
||||||
|
|
||||||
|
class EditDepartureTimeSlot extends EditRecord
|
||||||
|
{
|
||||||
|
protected static string $resource = DepartureTimeSlotResource::class;
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
DeleteAction::make(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
+19
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Filament\Resources\DepartureTimeSlots\Pages;
|
||||||
|
|
||||||
|
use Filament\Actions\CreateAction;
|
||||||
|
use Filament\Resources\Pages\ListRecords;
|
||||||
|
use Modules\Catalog\Filament\Resources\DepartureTimeSlots\DepartureTimeSlotResource;
|
||||||
|
|
||||||
|
class ListDepartureTimeSlots extends ListRecords
|
||||||
|
{
|
||||||
|
protected static string $resource = DepartureTimeSlotResource::class;
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
CreateAction::make(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
+26
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Filament\Resources\DepartureTimeSlots\Schemas;
|
||||||
|
|
||||||
|
use Filament\Forms\Components\TextInput;
|
||||||
|
use Filament\Forms\Components\TimePicker;
|
||||||
|
use Filament\Forms\Components\Toggle;
|
||||||
|
use Filament\Schemas\Schema;
|
||||||
|
|
||||||
|
class DepartureTimeSlotForm
|
||||||
|
{
|
||||||
|
public static function configure(Schema $schema): Schema
|
||||||
|
{
|
||||||
|
return $schema
|
||||||
|
->components([
|
||||||
|
TextInput::make('label')
|
||||||
|
->required()
|
||||||
|
->maxLength(255),
|
||||||
|
TimePicker::make('time')
|
||||||
|
->required(),
|
||||||
|
Toggle::make('is_active')
|
||||||
|
->required()
|
||||||
|
->default(true),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
+45
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Filament\Resources\DepartureTimeSlots\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;
|
||||||
|
|
||||||
|
class DepartureTimeSlotsTable
|
||||||
|
{
|
||||||
|
public static function configure(Table $table): Table
|
||||||
|
{
|
||||||
|
return $table
|
||||||
|
->columns([
|
||||||
|
TextColumn::make('label')
|
||||||
|
->searchable()
|
||||||
|
->sortable(),
|
||||||
|
TextColumn::make('time')
|
||||||
|
->time('H:i')
|
||||||
|
->sortable(),
|
||||||
|
IconColumn::make('is_active')
|
||||||
|
->boolean(),
|
||||||
|
TextColumn::make('created_at')
|
||||||
|
->dateTime()
|
||||||
|
->sortable()
|
||||||
|
->toggleable(isToggledHiddenByDefault: true),
|
||||||
|
])
|
||||||
|
->defaultSort('time')
|
||||||
|
->filters([
|
||||||
|
TernaryFilter::make('is_active'),
|
||||||
|
])
|
||||||
|
->recordActions([
|
||||||
|
EditAction::make(),
|
||||||
|
])
|
||||||
|
->toolbarActions([
|
||||||
|
BulkActionGroup::make([
|
||||||
|
DeleteBulkAction::make(),
|
||||||
|
]),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Http\Controllers;
|
||||||
|
|
||||||
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||||
|
use Illuminate\Routing\Controller;
|
||||||
|
use Modules\Catalog\Http\Resources\DestinationResource;
|
||||||
|
use Modules\Catalog\Models\Destination;
|
||||||
|
|
||||||
|
class DestinationController extends Controller
|
||||||
|
{
|
||||||
|
public function index(): AnonymousResourceCollection
|
||||||
|
{
|
||||||
|
return DestinationResource::collection(
|
||||||
|
Destination::query()->where('is_active', true)->get()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Http\Controllers;
|
||||||
|
|
||||||
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||||
|
use Illuminate\Routing\Controller;
|
||||||
|
use Modules\Catalog\Http\Resources\EvCompanyResource;
|
||||||
|
use Modules\Catalog\Models\EvCompany;
|
||||||
|
|
||||||
|
class EvCompanyController extends Controller
|
||||||
|
{
|
||||||
|
public function index(): AnonymousResourceCollection
|
||||||
|
{
|
||||||
|
return EvCompanyResource::collection(
|
||||||
|
EvCompany::query()->where('is_active', true)->get()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class DestinationResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'name' => $this->name,
|
||||||
|
'mm_name' => $this->mm_name,
|
||||||
|
'region' => $this->region,
|
||||||
|
'description' => $this->description,
|
||||||
|
'mm_description' => $this->mm_description,
|
||||||
|
'latitude' => $this->latitude,
|
||||||
|
'longitude' => $this->longitude,
|
||||||
|
'popular' => $this->popular,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class EvCompanyResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'name' => $this->name,
|
||||||
|
'mm_name' => $this->mm_name,
|
||||||
|
'slug' => $this->slug,
|
||||||
|
'description' => $this->description,
|
||||||
|
'mm_description' => $this->mm_description,
|
||||||
|
'contact' => $this->contact,
|
||||||
|
'address' => $this->address,
|
||||||
|
'logo' => $this->logo,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Modules\Catalog\Database\Factories\DepartureTimeSlotFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A shared catalog of departure times, attached to routes via a pivot in the
|
||||||
|
* Routing module — not owned by any single route.
|
||||||
|
*/
|
||||||
|
class DepartureTimeSlot extends Model
|
||||||
|
{
|
||||||
|
/** @use HasFactory<DepartureTimeSlotFactory> */
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var list<string>
|
||||||
|
*/
|
||||||
|
protected $fillable = [
|
||||||
|
'label',
|
||||||
|
'time',
|
||||||
|
'is_active',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, string>
|
||||||
|
*/
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'time' => 'datetime:H:i',
|
||||||
|
'is_active' => 'boolean',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Modules\Catalog\Models\Destination;
|
||||||
|
use Modules\Catalog\Models\EvCompany;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
$this->token = User::factory()->create()->createToken('test-token')->plainTextToken;
|
||||||
|
});
|
||||||
|
|
||||||
|
test('lists active ev companies', function () {
|
||||||
|
$active = EvCompany::factory()->create(['is_active' => true]);
|
||||||
|
EvCompany::factory()->create(['is_active' => false]);
|
||||||
|
|
||||||
|
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||||
|
->getJson('/api/v1/companies')
|
||||||
|
->assertSuccessful()
|
||||||
|
->assertJsonCount(1, 'data')
|
||||||
|
->assertJsonFragment(['id' => $active->id]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('lists active destinations', function () {
|
||||||
|
$active = Destination::factory()->create(['is_active' => true]);
|
||||||
|
Destination::factory()->create(['is_active' => false]);
|
||||||
|
|
||||||
|
$this->withHeader('Authorization', "Bearer {$this->token}")
|
||||||
|
->getJson('/api/v1/destinations')
|
||||||
|
->assertSuccessful()
|
||||||
|
->assertJsonCount(1, 'data')
|
||||||
|
->assertJsonFragment(['id' => $active->id]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('companies endpoint rejects unauthenticated requests', function () {
|
||||||
|
$this->getJson('/api/v1/companies')->assertUnauthorized();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('destinations endpoint rejects unauthenticated requests', function () {
|
||||||
|
$this->getJson('/api/v1/destinations')->assertUnauthorized();
|
||||||
|
});
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Livewire\Livewire;
|
||||||
|
use Modules\Catalog\Filament\Resources\DepartureTimeSlots\Pages\CreateDepartureTimeSlot;
|
||||||
|
use Modules\Catalog\Filament\Resources\DepartureTimeSlots\Pages\EditDepartureTimeSlot;
|
||||||
|
use Modules\Catalog\Filament\Resources\DepartureTimeSlots\Pages\ListDepartureTimeSlots;
|
||||||
|
use Modules\Catalog\Models\DepartureTimeSlot;
|
||||||
|
use Spatie\Permission\Models\Role;
|
||||||
|
|
||||||
|
use function Pest\Laravel\assertDatabaseHas;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
Role::findOrCreate('admin', 'web');
|
||||||
|
|
||||||
|
$this->admin = User::factory()->create()->assignRole('admin');
|
||||||
|
$this->actingAs($this->admin);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can list departure time slots', function () {
|
||||||
|
$timeSlots = DepartureTimeSlot::factory()->count(3)->create();
|
||||||
|
|
||||||
|
Livewire::test(ListDepartureTimeSlots::class)
|
||||||
|
->assertOk()
|
||||||
|
->assertCanSeeTableRecords($timeSlots);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can create a departure time slot', function () {
|
||||||
|
$timeSlot = DepartureTimeSlot::factory()->make();
|
||||||
|
|
||||||
|
Livewire::test(CreateDepartureTimeSlot::class)
|
||||||
|
->fillForm([
|
||||||
|
'label' => $timeSlot->label,
|
||||||
|
'time' => $timeSlot->time,
|
||||||
|
'is_active' => true,
|
||||||
|
])
|
||||||
|
->call('create')
|
||||||
|
->assertNotified()
|
||||||
|
->assertRedirect();
|
||||||
|
|
||||||
|
assertDatabaseHas(DepartureTimeSlot::class, [
|
||||||
|
'label' => $timeSlot->label,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can edit a departure time slot', function () {
|
||||||
|
$timeSlot = DepartureTimeSlot::factory()->create();
|
||||||
|
|
||||||
|
Livewire::test(EditDepartureTimeSlot::class, ['record' => $timeSlot->getRouteKey()])
|
||||||
|
->assertOk()
|
||||||
|
->fillForm(['label' => 'Updated Label'])
|
||||||
|
->call('save')
|
||||||
|
->assertNotified();
|
||||||
|
|
||||||
|
assertDatabaseHas(DepartureTimeSlot::class, [
|
||||||
|
'id' => $timeSlot->id,
|
||||||
|
'label' => 'Updated Label',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a departure time slot is a shared catalog entity not owned by a single route', function () {
|
||||||
|
$timeSlot = DepartureTimeSlot::factory()->create();
|
||||||
|
|
||||||
|
expect($timeSlot->getFillable())->not->toContain('ev_route_id');
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user