fix(order): revalidate basket prices and discounts during order finalization [AUD-DSA-001]
This commit is contained in:
parent
ad36fd4596
commit
29fa8a1b7d
4 changed files with 134 additions and 6 deletions
|
|
@ -14,7 +14,7 @@ use Shared\DDD\Entity;
|
|||
final class Basket implements Entity
|
||||
{
|
||||
/**
|
||||
* @param array<string,\Domain\Models\ValueObjects\BasketProduct> $items
|
||||
* @param list<\Domain\Models\ValueObjects\BasketProduct> $items
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly BasketId $id,
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue