Skip to content

Commit cf2c845

Browse files
committed
allow deletion of unconfirmed operations without forceDelete
1 parent bda3ca1 commit cf2c845

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

src/Internal/Observers/TransactionObserver.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Bavix\Wallet\Exceptions\UnconfirmedInvalid;
88
use Bavix\Wallet\Exceptions\WalletOwnerInvalid;
9+
use Bavix\Wallet\Interfaces\Wallet;
910
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
1011
use Bavix\Wallet\Internal\Exceptions\RecordNotFoundException;
1112
use Bavix\Wallet\Internal\Exceptions\TransactionFailedException;
@@ -24,6 +25,15 @@ final class TransactionObserver
2425
*/
2526
public function deleting(Transaction $model): bool
2627
{
27-
return $model->wallet->resetConfirm($model);
28+
return $this->safeResetConfirm($model->wallet, $model);
29+
}
30+
31+
private function safeResetConfirm(Wallet $model, Transaction $transaction): bool
32+
{
33+
try {
34+
return $model->resetConfirm($transaction);
35+
} catch (UnconfirmedInvalid) {
36+
return true;
37+
}
2838
}
2939
}

src/Internal/Observers/TransferObserver.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
use Bavix\Wallet\Exceptions\UnconfirmedInvalid;
88
use Bavix\Wallet\Exceptions\WalletOwnerInvalid;
9+
use Bavix\Wallet\Interfaces\Wallet;
910
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
1011
use Bavix\Wallet\Internal\Exceptions\RecordNotFoundException;
1112
use Bavix\Wallet\Internal\Exceptions\TransactionFailedException;
13+
use Bavix\Wallet\Models\Transaction;
1214
use Bavix\Wallet\Models\Transfer;
1315
use Bavix\Wallet\Services\AtomicServiceInterface;
1416
use Illuminate\Database\RecordsNotFoundException;
@@ -31,8 +33,17 @@ public function __construct(
3133
public function deleting(Transfer $model): bool
3234
{
3335
return $this->atomicService->blocks([$model->from, $model->to], function () use ($model) {
34-
return $model->from->resetConfirm($model->withdraw)
35-
&& $model->to->resetConfirm($model->deposit);
36+
return $this->safeResetConfirm($model->from, $model->withdraw)
37+
&& $this->safeResetConfirm($model->to, $model->deposit);
3638
});
3739
}
40+
41+
private function safeResetConfirm(Wallet $model, Transaction $transaction): bool
42+
{
43+
try {
44+
return $model->resetConfirm($transaction);
45+
} catch (UnconfirmedInvalid) {
46+
return true;
47+
}
48+
}
3849
}

tests/Units/Domain/SoftDeletesTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Bavix\Wallet\Test\Units\Domain;
66

7-
use Bavix\Wallet\Exceptions\UnconfirmedInvalid;
87
use Bavix\Wallet\Test\Infra\Factories\BuyerFactory;
98
use Bavix\Wallet\Test\Infra\Models\Buyer;
109
use Bavix\Wallet\Test\Infra\TestCase;
@@ -87,8 +86,6 @@ public function testTransactionDelete(): void
8786

8887
public function testTransactionDeleteIfNotConfirmed(): void
8988
{
90-
$this->expectException(UnconfirmedInvalid::class);
91-
9289
/** @var Buyer $buyer */
9390
$buyer = BuyerFactory::new()->create();
9491
self::assertFalse($buyer->relationLoaded('wallet'));
@@ -141,8 +138,6 @@ public function testTransferDelete(): void
141138

142139
public function testTransferDeleteIfNotConfirmed(): void
143140
{
144-
$this->expectException(UnconfirmedInvalid::class);
145-
146141
/** @var Buyer $user1 */
147142
/** @var Buyer $user2 */
148143
[$user1, $user2] = BuyerFactory::times(2)->create();

0 commit comments

Comments
 (0)