diff --git a/src/Interfaces/Wallet.php b/src/Interfaces/Wallet.php index 026542d4b..6ecc37eaa 100644 --- a/src/Interfaces/Wallet.php +++ b/src/Interfaces/Wallet.php @@ -105,4 +105,9 @@ public function transactions(): MorphMany; * @return HasMany */ public function transfers(): HasMany; + + /** + * @return HasMany + */ + public function receivedTransfers(): HasMany; } diff --git a/src/Models/Wallet.php b/src/Models/Wallet.php index 7cb08667b..69dedc2bf 100644 --- a/src/Models/Wallet.php +++ b/src/Models/Wallet.php @@ -13,6 +13,7 @@ use Bavix\Wallet\Internal\Service\MathServiceInterface; use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface; use Bavix\Wallet\Services\AtomicServiceInterface; +use Bavix\Wallet\Services\CastServiceInterface; use Bavix\Wallet\Services\RegulatorServiceInterface; use Bavix\Wallet\Traits\CanConfirm; use Bavix\Wallet\Traits\CanExchange; diff --git a/src/Traits/HasWallet.php b/src/Traits/HasWallet.php index 927a8ce8f..a035e3ca2 100644 --- a/src/Traits/HasWallet.php +++ b/src/Traits/HasWallet.php @@ -227,4 +227,17 @@ public function transfers(): HasMany ->hasMany(config('wallet.transfer.model', Transfer::class), 'from_id') ; } + + /** + * returns all the receiving transfers to this wallet. + * + * @return HasMany + */ + public function receivedTransfers(): HasMany + { + return app(CastServiceInterface::class) + ->getWallet($this, false) + ->hasMany(config('wallet.transfer.model', Transfer::class), 'to_id') + ; + } } diff --git a/tests/Units/Service/AtomicServiceTest.php b/tests/Units/Service/AtomicServiceTest.php index 523ccf5ae..8c29cc82e 100644 --- a/tests/Units/Service/AtomicServiceTest.php +++ b/tests/Units/Service/AtomicServiceTest.php @@ -35,7 +35,9 @@ public function testBlock(): void ); self::assertSame(1, $user2->transfers()->count()); + self::assertSame(2, $user2->receivedTransfers()->count()); self::assertSame(2, $user1->transfers()->count()); + self::assertSame(1, $user1->receivedTransfers()->count()); self::assertSame(3, $user2->transactions()->count()); self::assertSame(4, $user1->transactions()->count());