From 85a6212ef5d91e7714fa0785c24f461a964fce50 Mon Sep 17 00:00:00 2001 From: Kacper Donat Date: Sun, 14 Apr 2024 21:25:43 +0200 Subject: [PATCH] TASK-39: Add ability to top-up balance of given client This commit also refactors a bit logic behind deducing from balance to have better separation of concerns. --- features/crm-user-sync.feature | 11 +++++++++++ src/Contract/TopUpRequestDto.php | 8 ++++++++ src/Controller/ClientController.php | 12 ++++++++++++ src/Entity/Client.php | 5 +++++ src/Service/ClientService.php | 10 +++++++++- src/Service/OrderService.php | 6 +----- tests/App/Service/OrderServiceTest.php | 27 +------------------------- 7 files changed, 47 insertions(+), 32 deletions(-) create mode 100644 src/Contract/TopUpRequestDto.php diff --git a/features/crm-user-sync.feature b/features/crm-user-sync.feature index 95d4aea..8003d09 100644 --- a/features/crm-user-sync.feature +++ b/features/crm-user-sync.feature @@ -20,6 +20,17 @@ Feature: And client with id "018edd37-c145-7143-ba91-c191084e4fba" should exist And client with id "018edd37-c145-7143-ba91-c191084e4fba" should have balance of 100000 + Scenario: CRM is able to top up existing client + Given the request has the following body: + """ + { + "amount": 5000 + } + """ + When I send a POST request to "/clients/018edd2e-894a-78d7-b10c-16e05ca933a3/_top-up" + Then the response status should be 200 + And client with id "018edd2e-894a-78d7-b10c-16e05ca933a3" should have balance of 10000 + Scenario: CRM should not be able to override existing user Given the request has the following body: """ diff --git a/src/Contract/TopUpRequestDto.php b/src/Contract/TopUpRequestDto.php new file mode 100644 index 0000000..db115d2 --- /dev/null +++ b/src/Contract/TopUpRequestDto.php @@ -0,0 +1,8 @@ +clientService->topUpBalance($client, $topUpRequestDto->amount); + + return new Response(); + } } diff --git a/src/Entity/Client.php b/src/Entity/Client.php index 23e17f1..ed15926 100644 --- a/src/Entity/Client.php +++ b/src/Entity/Client.php @@ -61,4 +61,9 @@ class Client { $this->currentBalance -= $amount; } + + public function topUpBalance(int $amount): void + { + $this->currentBalance += $amount; + } } diff --git a/src/Service/ClientService.php b/src/Service/ClientService.php index f83ba26..82bebe2 100644 --- a/src/Service/ClientService.php +++ b/src/Service/ClientService.php @@ -44,9 +44,17 @@ class ClientService return $this->clientRepository->save($client); } - public function save(Client $client): void + public function deduceFromBalance(Client $client, int $amount) { + $client->deduceFromBalance($amount); + $this->clientRepository->save($client); } + public function topUpBalance(Client $client, int $amount) + { + $client->topUpBalance($amount); + + $this->clientRepository->save($client); + } } diff --git a/src/Service/OrderService.php b/src/Service/OrderService.php index fcf2f70..3bd33c6 100644 --- a/src/Service/OrderService.php +++ b/src/Service/OrderService.php @@ -24,10 +24,6 @@ readonly class OrderService $totalPrice = $command->order->getTotalPrice($this->pricingStrategy); - $command->client->deduceFromBalance($totalPrice); - - $this->clientService->save($command->client); + $this->clientService->deduceFromBalance($command->client, $totalPrice); } - - } diff --git a/tests/App/Service/OrderServiceTest.php b/tests/App/Service/OrderServiceTest.php index 7c806ea..bc3c914 100644 --- a/tests/App/Service/OrderServiceTest.php +++ b/tests/App/Service/OrderServiceTest.php @@ -69,31 +69,6 @@ class OrderServiceTest extends TestCase $sut->acceptOrder($order); } - public function testTotalPriceIsDeducedFromClientBalance() - { - $initialBalance = 10000_00; - $price = 6900_00; - - $command = new AcceptOrderCommand( - $order = $this->createValidOrderDto(), - $client = $this->createClient($initialBalance), - ); - - $pricingStrategyMock = $this->createMock(PricingStrategy::class); - - $pricingStrategyMock - ->expects($this->atLeastOnce()) - ->method('calculateTotalPriceOfOrder') - ->with($order) - ->willReturn($price); - - $sut = $this->createOrderService(pricingStrategyMock: $pricingStrategyMock); - $sut->acceptOrder($command); - - $this->assertEquals($initialBalance, $client->getInitialBalance(), 'Initial balance got changed after placing order'); - $this->assertEquals($initialBalance - $price, $client->getBalance(), 'Current balance not changed after placing order'); - } - public function testClientIsUpdatedAfterPlacingOrder() { $command = new AcceptOrderCommand( @@ -102,7 +77,7 @@ class OrderServiceTest extends TestCase ); $clientServiceMock = $this->createMock(ClientService::class); - $clientServiceMock->expects($this->once())->method('save')->with($client); + $clientServiceMock->expects($this->once())->method('deduceFromBalance'); $sut = $this->createOrderService(clientServiceMock: $clientServiceMock); $sut->acceptOrder($command);