diff --git a/app/src/Controller/Wallets/Charges/ChargesController.php b/app/src/Controller/Wallets/Charges/ChargesController.php index 35da86a6..b16285fb 100644 --- a/app/src/Controller/Wallets/Charges/ChargesController.php +++ b/app/src/Controller/Wallets/Charges/ChargesController.php @@ -147,6 +147,10 @@ public function update(string $walletId, string $chargeId, CreateRequest $reques $charge->title = $request->title; $charge->description = $request->description; + if (($dateTime = $request->getDateTime()) !== null) { + $charge->createdAt = $dateTime; + } + $charge->tags->clear(); $tags = $this->tagRepository->findAllByPKsAndUserPKs($request->tags, $wallet->getUserIDs()); diff --git a/app/src/Request/Charge/CreateRequest.php b/app/src/Request/Charge/CreateRequest.php index 1a6f7e70..f23ad8a6 100644 --- a/app/src/Request/Charge/CreateRequest.php +++ b/app/src/Request/Charge/CreateRequest.php @@ -15,6 +15,8 @@ class CreateRequest extends Filter implements HasFilterDefinition { + const DATE_FORMAT = 'Y-m-d H:i:s'; + // TODO. Support custom currency with custom rate #[Data] @@ -36,6 +38,9 @@ class CreateRequest extends Filter implements HasFilterDefinition #[Data] public array $tags = []; + #[Data] + public string $dateTime = ''; + public function filterDefinition(): FilterDefinitionInterface { return new FilterDefinition(validationRules: [ @@ -58,6 +63,16 @@ public function filterDefinition(): FilterDefinitionInterface 'tags' => [ ['array::of', ['entity:exists', Tag::class, 'id']], ], + 'dateTime' => [ + 'is_string', + ['datetime:format', self::DATE_FORMAT], + ['datetime:past', true], + ], ]); } + + public function getDateTime(): ?\DateTimeImmutable + { + return \DateTimeImmutable::createFromFormat(self::DATE_FORMAT, $this->dateTime) ?: null; + } } diff --git a/app/src/View/ChargeView.php b/app/src/View/ChargeView.php index b204b24a..4a874232 100644 --- a/app/src/View/ChargeView.php +++ b/app/src/View/ChargeView.php @@ -47,6 +47,7 @@ public function map(?Charge $charge): ?array 'description' => $charge->description, 'userId' => $charge->userId, 'walletId' => $charge->walletId, + 'dateTime' => $charge->createdAt->format(DATE_W3C), 'createdAt' => $charge->createdAt->format(DATE_W3C), 'updatedAt' => $charge->updatedAt->format(DATE_W3C), diff --git a/tests/Feature/Controller/Wallets/Charges/ChargesControllerTest.php b/tests/Feature/Controller/Wallets/Charges/ChargesControllerTest.php index 9879e556..4bfedf55 100644 --- a/tests/Feature/Controller/Wallets/Charges/ChargesControllerTest.php +++ b/tests/Feature/Controller/Wallets/Charges/ChargesControllerTest.php @@ -6,6 +6,7 @@ use App\Database\Charge; use App\Database\Tag; +use App\Request\Charge\CreateRequest; use App\Service\ChargeWalletService; use Doctrine\Common\Collections\ArrayCollection; use PHPUnit\Framework\MockObject\MockObject; @@ -765,16 +766,19 @@ public function updateValidationFailsDataProvider(): 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']], ]; } @@ -828,6 +832,7 @@ public function testUpdateStoreCharge(): void 'amount' => $updatedCharge->amount, 'title' => $updatedCharge->title, 'description' => $updatedCharge->description, + 'dateTime' => $updatedCharge->createdAt->format(CreateRequest::DATE_FORMAT), ]); $response->assertOk(); @@ -884,6 +889,7 @@ public function testUpdateStoreChargeWithTag(): void 'title' => $updatedCharge->title, 'description' => $updatedCharge->description, 'tags' => $newTags->map(fn(Tag $tag) => $tag->id)->getValues(), + 'dateTime' => $updatedCharge->createdAt->format(CreateRequest::DATE_FORMAT), ]); $response->assertOk(); @@ -929,6 +935,7 @@ public function testUpdateThrownException(): void 'amount' => $updatedCharge->amount, 'title' => $updatedCharge->title, 'description' => $updatedCharge->description, + 'dateTime' => $updatedCharge->createdAt->format(CreateRequest::DATE_FORMAT), ]); $response->assertStatus(500);