From 29fa8a1b7defe74bd410303dceeec45b8fdeefd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Wed, 26 Aug 2026 11:09:27 +0200 Subject: [PATCH] fix(order): revalidate basket prices and discounts during order finalization [AUD-DSA-001] --- domain/Models/Entities/Basket.php | 2 +- domain/Services/BasketService.php | 14 +++ domain/Services/OrderTunnelService.php | 6 +- .../Services/OrderTunnelServiceTest.php | 118 +++++++++++++++++- 4 files changed, 134 insertions(+), 6 deletions(-) diff --git a/domain/Models/Entities/Basket.php b/domain/Models/Entities/Basket.php index fb753d7b..b9708919 100644 --- a/domain/Models/Entities/Basket.php +++ b/domain/Models/Entities/Basket.php @@ -14,7 +14,7 @@ use Shared\DDD\Entity; final class Basket implements Entity { /** - * @param array $items + * @param list<\Domain\Models\ValueObjects\BasketProduct> $items */ public function __construct( public readonly BasketId $id, diff --git a/domain/Services/BasketService.php b/domain/Services/BasketService.php index e6a81a8a..a581136a 100644 --- a/domain/Services/BasketService.php +++ b/domain/Services/BasketService.php @@ -53,6 +53,20 @@ class BasketService $this->repository->persist($basket); } + public function revalidateBasket(Basket $basket): Basket + { + $refreshedItems = []; + + foreach ($basket->items() as $item) { + $refreshedItems[] = $this->productRepository->getBasketProduct($item->id, $item->qty); + } + + $refreshedBasket = new Basket($basket->id, $refreshedItems); + $this->repository->persist($refreshedBasket); + + return $refreshedBasket; + } + public function empty(): void { $this->repository->delete($this->getBasket()); diff --git a/domain/Services/OrderTunnelService.php b/domain/Services/OrderTunnelService.php index ced969cd..7fcb7cef 100644 --- a/domain/Services/OrderTunnelService.php +++ b/domain/Services/OrderTunnelService.php @@ -192,7 +192,7 @@ class OrderTunnelService public function finalizeOrder(OrderBuilderCompleted $builder): Order { $shopperId = $builder->getShopperId(); - $basket = $builder->getBasket(); + $basket = $this->basketService->revalidateBasket($builder->getBasket()); $pickupTimeSlot = $builder->getPickupTimeSlot(); $shopId = $builder->getShopId(); $shopper = $this->shopperRepository->getByIdOrFail($shopperId); @@ -205,9 +205,9 @@ class OrderTunnelService ); return $this->transactionManager->execute(function () use ($basket, $shopId, $shopper, $pickupTimeSlot) { - $shop = $this->shopRepository->find($shopId); + $shop = $this->shopRepository->find($shopId); if (! $shop) { - throw new \DomainException("Shopwith id '{$shopId->value}' not found."); + throw new \DomainException("Shop with id '{$shopId->value}' not found."); } $order = $this->orderService->createOrderFromBasket( diff --git a/tests/Unit/Domain/Services/OrderTunnelServiceTest.php b/tests/Unit/Domain/Services/OrderTunnelServiceTest.php index 4a948cfd..4ead4f48 100644 --- a/tests/Unit/Domain/Services/OrderTunnelServiceTest.php +++ b/tests/Unit/Domain/Services/OrderTunnelServiceTest.php @@ -106,8 +106,33 @@ function createOrderTunnelService( ?DomainEventDispatcher $eventDispatcher = null, ?UuidConverter $uuidConverter = null, ): OrderTunnelService { - $basketRepository ??= mock(BasketRepository::class); - $productRepository ??= mock(ProductRepository::class); + if ($basketRepository === null) { + $basketRepository = mock(BasketRepository::class); + $basketRepository->shouldReceive('persist')->byDefault(); + } + if ($productRepository === null) { + $productRepository = mock(ProductRepository::class); + $productRepository->shouldReceive('getBasketProduct')->andReturnUsing(function ($id, $qty) { + return new BasketProduct( + id: $id, + name: 'Test Product', + qty: $qty, + unitPriceHt: Money::EUR(100), + unitDiscount: Money::EUR(0), + taxRate: TaxRate::TVA_20, + weightGrams: 100, + brand: null, + incoId: new IncoId(123), + orderUnit: OrderUnit::PCE, + slug: 'test-product', + picture: null, + description: null, + weightVariable: false, + discountRate: 0.0, + priceKgTtc: Money::EUR(120) + ); + })->byDefault(); + } $shopRepository ??= mock(ShopRepository::class); $shopperRepository ??= mock(ShopperRepository::class); $pickupExceptionRepository ??= mock(PickupExceptionRepository::class); @@ -512,6 +537,7 @@ describe('OrderTunnelService', function (): void { // GIVEN $basketRepo = mock(BasketRepository::class); $basketRepo->shouldReceive('find')->once()->andReturn(createTestBasket(1)); + $basketRepo->shouldReceive('persist')->once(); $basketRepo->shouldReceive('delete')->once(); $builder = createTestBuilder(completed: true); @@ -616,4 +642,92 @@ describe('OrderTunnelService', function (): void { // THEN expect($action)->toThrow(\DomainException::class); }); + + it('revalidates basket prices from product repository when finalizing order', function (): void { + // GIVEN + $builder = createTestBuilder(completed: true); + $shopperId = $builder->getShopperId(); + $shopper = new Shopper( + id: $shopperId, + email: new Email('test@test.com'), + matricule: new Matricule('1234567'), + nom: 'Doe', + prenom: 'John', + startDate: CarbonImmutable::now()->subYear(), + endDate: null, + etablissement: 'Etab', + site: 'Site', + service: 'Service' + ); + $shopperRepo = mock(ShopperRepository::class); + $shopperRepo->shouldReceive('getByIdOrFail')->with(\Mockery::type(ShopperId::class))->andReturn($shopper); + + $shopId = clone $builder->getShopId(); + $shop = new Shop($shopId, 'Shop', 'Address', '75000', 'Paris'); + $shopRepo = mock(ShopRepository::class); + $shopRepo->shouldReceive('find')->andReturn($shop); + + $orderRepo = mock(OrderRepository::class); + $orderRepo->shouldReceive('getWeeklyTotalForEmployee')->andReturn(Money::EUR(5000)); + $orderRepo->shouldReceive('persist')->once(); + + $transactionManager = mock(TransactionManager::class); + $transactionManager->shouldReceive('execute')->once()->andReturnUsing(function ($callback) { + return $callback(); + }); + + $eventDispatcher = mock(DomainEventDispatcher::class); + $eventDispatcher->shouldReceive('dispatch')->once(); + + $builderRepo = mock(OrderBuilderSessionManager::class); + $builderRepo->shouldReceive('clear')->once(); + + $uuidConverter = mock(UuidConverter::class); + $uuidConverter->shouldReceive('toBase58')->andReturn(\Symfony\Component\Uid\Uuid::v7()->toBase58()); + + $basketRepo = mock(BasketRepository::class); + $basketRepo->shouldReceive('find')->once()->andReturn(createTestBasket(1)); + $basketRepo->shouldReceive('persist')->once(); + $basketRepo->shouldReceive('delete')->once(); + + $productRepo = mock(ProductRepository::class); + $productRepo->shouldReceive('getBasketProduct')->once()->andReturnUsing(function ($id, $qty) { + return new BasketProduct( + id: $id, + name: 'Updated Product Name', + qty: $qty, + unitPriceHt: Money::EUR(250), + unitDiscount: Money::EUR(50), + taxRate: TaxRate::TVA_20, + weightGrams: 200, + brand: null, + incoId: new IncoId(123), + orderUnit: OrderUnit::PCE, + slug: 'updated-product', + picture: null, + description: null, + weightVariable: false, + discountRate: 0.0, + priceKgTtc: Money::EUR(240) + ); + }); + + $service = createOrderTunnelService( + basketRepository: $basketRepo, + productRepository: $productRepo, + shopRepository: $shopRepo, + shopperRepository: $shopperRepo, + orderBuilderSessionManager: $builderRepo, + orderRepository: $orderRepo, + transactionManager: $transactionManager, + eventDispatcher: $eventDispatcher, + uuidConverter: $uuidConverter + ); + + // WHEN + $order = $service->finalizeOrder($builder); + + // THEN + expect($order)->toBeInstanceOf(Order::class); + }); });