From cf2c84595e628b5f1b37377ebc5d49d9808a2edf Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Mon, 8 Jul 2024 08:49:38 +0300 Subject: [PATCH 1/3] allow deletion of unconfirmed operations without forceDelete --- src/Internal/Observers/TransactionObserver.php | 12 +++++++++++- src/Internal/Observers/TransferObserver.php | 15 +++++++++++++-- tests/Units/Domain/SoftDeletesTest.php | 5 ----- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/Internal/Observers/TransactionObserver.php b/src/Internal/Observers/TransactionObserver.php index 56ca2bf7b..27aa2a29a 100644 --- a/src/Internal/Observers/TransactionObserver.php +++ b/src/Internal/Observers/TransactionObserver.php @@ -6,6 +6,7 @@ use Bavix\Wallet\Exceptions\UnconfirmedInvalid; use Bavix\Wallet\Exceptions\WalletOwnerInvalid; +use Bavix\Wallet\Interfaces\Wallet; use Bavix\Wallet\Internal\Exceptions\ExceptionInterface; use Bavix\Wallet\Internal\Exceptions\RecordNotFoundException; use Bavix\Wallet\Internal\Exceptions\TransactionFailedException; @@ -24,6 +25,15 @@ final class TransactionObserver */ public function deleting(Transaction $model): bool { - return $model->wallet->resetConfirm($model); + return $this->safeResetConfirm($model->wallet, $model); + } + + private function safeResetConfirm(Wallet $model, Transaction $transaction): bool + { + try { + return $model->resetConfirm($transaction); + } catch (UnconfirmedInvalid) { + return true; + } } } diff --git a/src/Internal/Observers/TransferObserver.php b/src/Internal/Observers/TransferObserver.php index 136670302..3515b6db6 100644 --- a/src/Internal/Observers/TransferObserver.php +++ b/src/Internal/Observers/TransferObserver.php @@ -6,9 +6,11 @@ use Bavix\Wallet\Exceptions\UnconfirmedInvalid; use Bavix\Wallet\Exceptions\WalletOwnerInvalid; +use Bavix\Wallet\Interfaces\Wallet; use Bavix\Wallet\Internal\Exceptions\ExceptionInterface; use Bavix\Wallet\Internal\Exceptions\RecordNotFoundException; use Bavix\Wallet\Internal\Exceptions\TransactionFailedException; +use Bavix\Wallet\Models\Transaction; use Bavix\Wallet\Models\Transfer; use Bavix\Wallet\Services\AtomicServiceInterface; use Illuminate\Database\RecordsNotFoundException; @@ -31,8 +33,17 @@ public function __construct( public function deleting(Transfer $model): bool { return $this->atomicService->blocks([$model->from, $model->to], function () use ($model) { - return $model->from->resetConfirm($model->withdraw) - && $model->to->resetConfirm($model->deposit); + return $this->safeResetConfirm($model->from, $model->withdraw) + && $this->safeResetConfirm($model->to, $model->deposit); }); } + + private function safeResetConfirm(Wallet $model, Transaction $transaction): bool + { + try { + return $model->resetConfirm($transaction); + } catch (UnconfirmedInvalid) { + return true; + } + } } diff --git a/tests/Units/Domain/SoftDeletesTest.php b/tests/Units/Domain/SoftDeletesTest.php index cfbc7ed74..5d73bd510 100644 --- a/tests/Units/Domain/SoftDeletesTest.php +++ b/tests/Units/Domain/SoftDeletesTest.php @@ -4,7 +4,6 @@ namespace Bavix\Wallet\Test\Units\Domain; -use Bavix\Wallet\Exceptions\UnconfirmedInvalid; use Bavix\Wallet\Test\Infra\Factories\BuyerFactory; use Bavix\Wallet\Test\Infra\Models\Buyer; use Bavix\Wallet\Test\Infra\TestCase; @@ -87,8 +86,6 @@ public function testTransactionDelete(): void public function testTransactionDeleteIfNotConfirmed(): void { - $this->expectException(UnconfirmedInvalid::class); - /** @var Buyer $buyer */ $buyer = BuyerFactory::new()->create(); self::assertFalse($buyer->relationLoaded('wallet')); @@ -141,8 +138,6 @@ public function testTransferDelete(): void public function testTransferDeleteIfNotConfirmed(): void { - $this->expectException(UnconfirmedInvalid::class); - /** @var Buyer $user1 */ /** @var Buyer $user2 */ [$user1, $user2] = BuyerFactory::times(2)->create(); From f4d66aa3279e2bf47b9148f8933d8ddddedb9aaa Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Mon, 8 Jul 2024 14:06:39 +0300 Subject: [PATCH 2/3] fix safeResetConfirm --- src/Internal/Observers/TransactionObserver.php | 12 +----------- src/Internal/Observers/TransferObserver.php | 15 ++------------- src/Traits/CanConfirm.php | 2 ++ tests/Units/Domain/ConfirmTest.php | 2 +- 4 files changed, 6 insertions(+), 25 deletions(-) diff --git a/src/Internal/Observers/TransactionObserver.php b/src/Internal/Observers/TransactionObserver.php index 27aa2a29a..0ac0e7911 100644 --- a/src/Internal/Observers/TransactionObserver.php +++ b/src/Internal/Observers/TransactionObserver.php @@ -6,7 +6,6 @@ use Bavix\Wallet\Exceptions\UnconfirmedInvalid; use Bavix\Wallet\Exceptions\WalletOwnerInvalid; -use Bavix\Wallet\Interfaces\Wallet; use Bavix\Wallet\Internal\Exceptions\ExceptionInterface; use Bavix\Wallet\Internal\Exceptions\RecordNotFoundException; use Bavix\Wallet\Internal\Exceptions\TransactionFailedException; @@ -25,15 +24,6 @@ final class TransactionObserver */ public function deleting(Transaction $model): bool { - return $this->safeResetConfirm($model->wallet, $model); - } - - private function safeResetConfirm(Wallet $model, Transaction $transaction): bool - { - try { - return $model->resetConfirm($transaction); - } catch (UnconfirmedInvalid) { - return true; - } + return $model->wallet->safeResetConfirm($model); } } diff --git a/src/Internal/Observers/TransferObserver.php b/src/Internal/Observers/TransferObserver.php index 3515b6db6..3b2517c94 100644 --- a/src/Internal/Observers/TransferObserver.php +++ b/src/Internal/Observers/TransferObserver.php @@ -6,11 +6,9 @@ use Bavix\Wallet\Exceptions\UnconfirmedInvalid; use Bavix\Wallet\Exceptions\WalletOwnerInvalid; -use Bavix\Wallet\Interfaces\Wallet; use Bavix\Wallet\Internal\Exceptions\ExceptionInterface; use Bavix\Wallet\Internal\Exceptions\RecordNotFoundException; use Bavix\Wallet\Internal\Exceptions\TransactionFailedException; -use Bavix\Wallet\Models\Transaction; use Bavix\Wallet\Models\Transfer; use Bavix\Wallet\Services\AtomicServiceInterface; use Illuminate\Database\RecordsNotFoundException; @@ -33,17 +31,8 @@ public function __construct( public function deleting(Transfer $model): bool { return $this->atomicService->blocks([$model->from, $model->to], function () use ($model) { - return $this->safeResetConfirm($model->from, $model->withdraw) - && $this->safeResetConfirm($model->to, $model->deposit); + return $model->from->safeResetConfirm($model->withdraw) + && $model->to->safeResetConfirm($model->deposit); }); } - - private function safeResetConfirm(Wallet $model, Transaction $transaction): bool - { - try { - return $model->resetConfirm($transaction); - } catch (UnconfirmedInvalid) { - return true; - } - } } diff --git a/src/Traits/CanConfirm.php b/src/Traits/CanConfirm.php index 9a5c4b744..5886d5acf 100644 --- a/src/Traits/CanConfirm.php +++ b/src/Traits/CanConfirm.php @@ -99,6 +99,8 @@ public function safeResetConfirm(Transaction $transaction): bool { try { return $this->resetConfirm($transaction); + } catch (UnconfirmedInvalid) { + return true; } catch (ExceptionInterface) { return false; } diff --git a/tests/Units/Domain/ConfirmTest.php b/tests/Units/Domain/ConfirmTest.php index 58168bfd3..9533ec53a 100644 --- a/tests/Units/Domain/ConfirmTest.php +++ b/tests/Units/Domain/ConfirmTest.php @@ -195,7 +195,7 @@ public function testSafeUnconfirmed(): void $transaction = $wallet->deposit(1000, null, false); self::assertSame(0, $wallet->balanceInt); self::assertFalse($transaction->confirmed); - self::assertFalse($wallet->safeResetConfirm($transaction)); + self::assertTrue($wallet->safeResetConfirm($transaction)); } public function testWalletOwnerInvalid(): void From 270d34851397a1b8f945d76073f09cb27749da67 Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Mon, 8 Jul 2024 14:13:46 +0300 Subject: [PATCH 3/3] fix safeConfirm --- src/Traits/CanConfirm.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Traits/CanConfirm.php b/src/Traits/CanConfirm.php index 5886d5acf..e49377d71 100644 --- a/src/Traits/CanConfirm.php +++ b/src/Traits/CanConfirm.php @@ -54,6 +54,8 @@ public function safeConfirm(Transaction $transaction): bool { try { return $this->confirm($transaction); + } catch (ConfirmedInvalid) { + return true; } catch (ExceptionInterface) { return false; }