feat(orders): implement ApplyPickTimeSlot action and output handling
- Introduced ApplyPickTimeSlotOutput class for structured output. - Updated ApplyPickTimeSlot action to return ApplyPickTimeSlotOutput. - Added ApplyPickTimeSlotStatus enum for status representation. - Modified TunnelController to handle new output structure. - Enhanced tests for ApplyPickTimeSlot to validate new behavior.
This commit is contained in:
parent
8892a55322
commit
0aa4b449b4
6 changed files with 115 additions and 14 deletions
|
|
@ -6,12 +6,14 @@ namespace App\Cqs\Actions\Orders;
|
|||
|
||||
use App\Contracts\Action;
|
||||
use App\Contracts\ActionInput;
|
||||
use Domain\Models\ValueObjects\PickupTimeSlot;
|
||||
use Domain\Services\OrderTunnelService;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/**
|
||||
* @implements \App\Contracts\Action<\App\Cqs\Actions\Orders\ApplyPickTimeSlotInput, ?\Domain\Models\ValueObjects\PickupTimeSlot>
|
||||
* @implements \App\Contracts\Action<
|
||||
* \App\Cqs\Actions\Orders\ApplyPickTimeSlotInput,
|
||||
* \App\Cqs\Actions\Orders\ApplyPickTimeSlotOutput
|
||||
* >
|
||||
*/
|
||||
final class ApplyPickTimeSlot implements Action
|
||||
{
|
||||
|
|
@ -24,7 +26,7 @@ final class ApplyPickTimeSlot implements Action
|
|||
* @param \App\Cqs\Actions\Orders\ApplyPickTimeSlotInput $input
|
||||
*/
|
||||
#[\Override]
|
||||
public function __invoke(ActionInput $input): ?PickupTimeSlot
|
||||
public function __invoke(ActionInput $input): ApplyPickTimeSlotOutput
|
||||
{
|
||||
Assert::isInstanceOf($input, ApplyPickTimeSlotInput::class);
|
||||
|
||||
|
|
@ -32,11 +34,15 @@ final class ApplyPickTimeSlot implements Action
|
|||
$shop = $this->orderTunnelService->getShopForCustomer($input->shopperId);
|
||||
$timeSlots = $this->orderTunnelService->getAvailableTimeSlots($shop);
|
||||
|
||||
return $this->orderTunnelService->applyTimeSlot(
|
||||
$timeSlot = $this->orderTunnelService->applyTimeSlot(
|
||||
$builder,
|
||||
$shop,
|
||||
$input->timeslot,
|
||||
$timeSlots
|
||||
);
|
||||
|
||||
return $timeSlot !== null
|
||||
? ApplyPickTimeSlotOutput::matched($timeSlot)
|
||||
: ApplyPickTimeSlotOutput::unavailable();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
32
app/Cqs/Actions/Orders/ApplyPickTimeSlotOutput.php
Normal file
32
app/Cqs/Actions/Orders/ApplyPickTimeSlotOutput.php
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Cqs\Actions\Orders;
|
||||
|
||||
use App\Enums\ApplyPickTimeSlotStatus;
|
||||
use Domain\Models\ValueObjects\PickupTimeSlot;
|
||||
|
||||
final readonly class ApplyPickTimeSlotOutput
|
||||
{
|
||||
public function __construct(
|
||||
public ApplyPickTimeSlotStatus $status,
|
||||
public ?PickupTimeSlot $timeSlot = null,
|
||||
) {
|
||||
}
|
||||
|
||||
public static function matched(PickupTimeSlot $timeSlot): self
|
||||
{
|
||||
return new self(
|
||||
status: ApplyPickTimeSlotStatus::MATCHED,
|
||||
timeSlot: $timeSlot,
|
||||
);
|
||||
}
|
||||
|
||||
public static function unavailable(): self
|
||||
{
|
||||
return new self(
|
||||
status: ApplyPickTimeSlotStatus::UNAVAILABLE,
|
||||
);
|
||||
}
|
||||
}
|
||||
11
app/Enums/ApplyPickTimeSlotStatus.php
Normal file
11
app/Enums/ApplyPickTimeSlotStatus.php
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
enum ApplyPickTimeSlotStatus
|
||||
{
|
||||
case MATCHED;
|
||||
case UNAVAILABLE;
|
||||
}
|
||||
|
|
@ -17,6 +17,7 @@ use App\Cqs\Queries\Orders\GetPickTimeSlotQuery;
|
|||
use App\Cqs\Queries\Orders\GetPickTimeSlotQueryInput;
|
||||
use App\Cqs\Queries\Orders\GetTunnelOrderConfirmedQuery;
|
||||
use App\Cqs\Queries\Orders\GetTunnelOrderConfirmedQueryInput;
|
||||
use App\Enums\ApplyPickTimeSlotStatus;
|
||||
use App\Enums\FinalizeOrderStatus;
|
||||
use App\Exceptions\CustomerNotEmployedException;
|
||||
use App\Http\Controllers\Controller;
|
||||
|
|
@ -95,18 +96,17 @@ class TunnelController extends Controller
|
|||
): RedirectResponse {
|
||||
$this->authorize('canPickTimeSlot', OrderTunnelStep::class);
|
||||
|
||||
$matchedSlot = $this->executor->execute(
|
||||
$output = $this->executor->execute(
|
||||
$action,
|
||||
$request->toApplyPickTimeSlotInput()
|
||||
);
|
||||
|
||||
if ($matchedSlot === null) {
|
||||
return back()->withErrors([
|
||||
return match ($output->status) {
|
||||
ApplyPickTimeSlotStatus::MATCHED => to_route('fo.sales.confirmation'),
|
||||
ApplyPickTimeSlotStatus::UNAVAILABLE => back()->withErrors([
|
||||
'timeslot' => trans('Invalid or unavailable time slot'),
|
||||
])->withInput();
|
||||
}
|
||||
|
||||
return to_route('fo.sales.confirmation');
|
||||
])->withInput(),
|
||||
};
|
||||
}
|
||||
|
||||
// * -- STEP 3 -- *
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
declare(strict_types=1);
|
||||
|
||||
use App\Contracts\Executor;
|
||||
use App\Cqs\Actions\Orders\ApplyPickTimeSlotOutput;
|
||||
use App\Cqs\Actions\Orders\FinalizeOrderOutput;
|
||||
use App\Cqs\Queries\Orders\GetBasketSummaryQueryOutput;
|
||||
use App\Cqs\Queries\Orders\GetOrderConfirmationQueryOutput;
|
||||
|
|
@ -341,7 +342,7 @@ describe('TunnelController', function (): void {
|
|||
$this->mock(Executor::class, function ($mock) use ($slot): void {
|
||||
$mock->shouldReceive('execute')
|
||||
->once()
|
||||
->andReturn($slot);
|
||||
->andReturn(ApplyPickTimeSlotOutput::matched($slot));
|
||||
});
|
||||
|
||||
// WHEN
|
||||
|
|
@ -401,7 +402,7 @@ describe('TunnelController', function (): void {
|
|||
$this->mock(Executor::class, function ($mock): void {
|
||||
$mock->shouldReceive('execute')
|
||||
->once()
|
||||
->andReturn(null);
|
||||
->andReturn(ApplyPickTimeSlotOutput::unavailable());
|
||||
});
|
||||
|
||||
// WHEN
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||
|
||||
use App\Cqs\Actions\Orders\ApplyPickTimeSlot;
|
||||
use App\Cqs\Actions\Orders\ApplyPickTimeSlotInput;
|
||||
use App\Cqs\Actions\Orders\ApplyPickTimeSlotOutput;
|
||||
use App\Enums\ApplyPickTimeSlotStatus;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Domain\Builders\OrderBuilderWithBasket;
|
||||
use Domain\Models\Entities\Shop;
|
||||
|
|
@ -57,6 +59,55 @@ describe('ApplyPickTimeSlot', function (): void {
|
|||
$result = $action($input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBe($slot);
|
||||
expect($result)->toBeInstanceOf(ApplyPickTimeSlotOutput::class)
|
||||
->and($result->status)->toBe(ApplyPickTimeSlotStatus::MATCHED)
|
||||
->and($result->timeSlot)->toBe($slot);
|
||||
});
|
||||
|
||||
it('returns unavailable output when no time slot matches', function (): void {
|
||||
// GIVEN
|
||||
$shopperId = new ShopperId(Uuid::uuid7());
|
||||
$timeslot = CarbonImmutable::parse('2026-10-15');
|
||||
$input = new ApplyPickTimeSlotInput(
|
||||
shopperId: $shopperId,
|
||||
timeslot: $timeslot,
|
||||
);
|
||||
|
||||
$builder = \Mockery::mock(OrderBuilderWithBasket::class);
|
||||
$shop = new Shop(
|
||||
id: new ShopId(Uuid::uuid7()),
|
||||
name: 'Shop 1',
|
||||
address: '1 rue Test',
|
||||
postcode: '75001',
|
||||
city: 'Paris',
|
||||
);
|
||||
$timeSlots = [];
|
||||
|
||||
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
|
||||
$orderTunnelService->shouldReceive('getBuilderFromBasketOrFail')
|
||||
->once()
|
||||
->andReturn($builder);
|
||||
$orderTunnelService->shouldReceive('getShopForCustomer')
|
||||
->once()
|
||||
->with($shopperId)
|
||||
->andReturn($shop);
|
||||
$orderTunnelService->shouldReceive('getAvailableTimeSlots')
|
||||
->once()
|
||||
->with($shop)
|
||||
->andReturn($timeSlots);
|
||||
$orderTunnelService->shouldReceive('applyTimeSlot')
|
||||
->once()
|
||||
->with($builder, $shop, $timeslot, $timeSlots)
|
||||
->andReturnNull();
|
||||
|
||||
$action = new ApplyPickTimeSlot($orderTunnelService);
|
||||
|
||||
// WHEN
|
||||
$result = $action($input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(ApplyPickTimeSlotOutput::class)
|
||||
->and($result->status)->toBe(ApplyPickTimeSlotStatus::UNAVAILABLE)
|
||||
->and($result->timeSlot)->toBeNull();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue