From ceac74ce4d63477b4c72fc76ea67dd6405335584 Mon Sep 17 00:00:00 2001 From: Volodymyr Komarov Date: Sun, 8 Oct 2023 14:01:29 +0300 Subject: [PATCH] Allow to set charge date on creating --- app/src/Controller/Wallets/Charges/ChargesController.php | 4 ++++ .../Controller/Wallets/Charges/ChargesControllerTest.php | 9 +++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/src/Controller/Wallets/Charges/ChargesController.php b/app/src/Controller/Wallets/Charges/ChargesController.php index 422c52a..f594f30 100644 --- a/app/src/Controller/Wallets/Charges/ChargesController.php +++ b/app/src/Controller/Wallets/Charges/ChargesController.php @@ -97,6 +97,10 @@ public function create(string $walletId, CreateRequest $request): ResponseInterf $charge->setWallet($wallet); $charge->setUser($this->user); + if (($dateTime = $request->getDateTime()) !== null) { + $charge->createdAt = $dateTime; + } + $tags = $this->tagRepository->findAllByPKsAndUserPKs($request->tags, $wallet->getUserIDs()); foreach ($tags as $tag) { diff --git a/tests/Feature/Controller/Wallets/Charges/ChargesControllerTest.php b/tests/Feature/Controller/Wallets/Charges/ChargesControllerTest.php index 42ffb81..c82a3a6 100644 --- a/tests/Feature/Controller/Wallets/Charges/ChargesControllerTest.php +++ b/tests/Feature/Controller/Wallets/Charges/ChargesControllerTest.php @@ -479,16 +479,19 @@ public function createValidationFailsDataProvider(): array 'amount' => 'false', 'title' => false, 'description' => false, - ], ['type', 'amount', 'title', 'description']], + 'dateTime' => 123, + ], ['type', 'amount', 'title', 'description', 'dateTime']], [[ 'type' => '+', 'amount' => 0, 'title' => 'Title', - ], ['amount']], + 'dateTime' => '123' + ], ['amount', 'dateTime']], [[ 'type' => '+', 'amount' => -1, 'title' => 'Title', + 'dateTime' => (new \DateTimeImmutable())->add(\DateInterval::createFromDateString('1 minute'))->format(CreateRequest::DATE_FORMAT), ], ['amount']], ]; } @@ -531,6 +534,7 @@ public function testCreateStoreCharge(): void 'amount' => $charge->amount, 'title' => $charge->title, 'description' => $charge->description, + 'dateTime' => $charge->createdAt->format(CreateRequest::DATE_FORMAT), ]); $response->assertOk(); @@ -541,6 +545,7 @@ public function testCreateStoreCharge(): void $this->assertArrayContains($charge->type, $body, 'data.operation'); $this->assertArrayContains($charge->amount, $body, 'data.amount'); $this->assertArrayContains($charge->description, $body, 'data.description'); + $this->assertArrayContains($charge->createdAt->format(DATE_W3C), $body, 'data.dateTime'); $this->assertDatabaseHas('charges', [ 'title' => $charge->title,