From 30bacfc9f39e8fa6e2b1f571b0484e986c280458 Mon Sep 17 00:00:00 2001 From: George Klincarski Date: Tue, 30 Jan 2024 11:02:35 +0100 Subject: [PATCH 1/4] Add payments relation on wallet This adds functionality to query the database for transfers in the other direction, payments. Lookup what transfers happened from another wallet to this one. Signed-off-by: George Klincarski --- src/Traits/HasWallet.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Traits/HasWallet.php b/src/Traits/HasWallet.php index 927a8ce8f..4d8d70877 100644 --- a/src/Traits/HasWallet.php +++ b/src/Traits/HasWallet.php @@ -227,4 +227,18 @@ 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 payments(): HasMany + { + /** @var Wallet $this */ + return app(CastServiceInterface::class) + ->getWallet($this, false) + ->hasMany(config('wallet.transfer.model', Transfer::class), 'to_id') + ; + } } From f96f12974f0c797b13364ecc69ccae1009ba977a Mon Sep 17 00:00:00 2001 From: George Klincarski Date: Wed, 31 Jan 2024 08:57:09 +0100 Subject: [PATCH 2/4] Update HasWallet.php remove payments Signed-off-by: George Klincarski --- src/Traits/HasWallet.php | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/Traits/HasWallet.php b/src/Traits/HasWallet.php index 4d8d70877..927a8ce8f 100644 --- a/src/Traits/HasWallet.php +++ b/src/Traits/HasWallet.php @@ -227,18 +227,4 @@ 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 payments(): HasMany - { - /** @var Wallet $this */ - return app(CastServiceInterface::class) - ->getWallet($this, false) - ->hasMany(config('wallet.transfer.model', Transfer::class), 'to_id') - ; - } } From cedac1c805ca1c66c0bafb51b8a8ca8295671b4e Mon Sep 17 00:00:00 2001 From: George Klincarski Date: Wed, 31 Jan 2024 09:01:18 +0100 Subject: [PATCH 3/4] Update Wallet.php add receivedTransfers Signed-off-by: George Klincarski --- src/Models/Wallet.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Models/Wallet.php b/src/Models/Wallet.php index 7cb08667b..c83efa258 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; @@ -165,4 +166,17 @@ protected function initializeMorphOneWallet(): void { $this->uuid = app(UuidFactoryServiceInterface::class)->uuid4(); } + + /** + * 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') + ; + } } From 543fc838760613dc1ae16d895a9958ad78c9cd1e Mon Sep 17 00:00:00 2001 From: george Date: Thu, 1 Feb 2024 19:39:24 +0100 Subject: [PATCH 4/4] Add received transfers function for v11 --- src/Interfaces/Wallet.php | 5 +++++ src/Models/Wallet.php | 13 ------------- src/Traits/HasWallet.php | 13 +++++++++++++ tests/Units/Service/AtomicServiceTest.php | 2 ++ 4 files changed, 20 insertions(+), 13 deletions(-) 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 c83efa258..69dedc2bf 100644 --- a/src/Models/Wallet.php +++ b/src/Models/Wallet.php @@ -166,17 +166,4 @@ protected function initializeMorphOneWallet(): void { $this->uuid = app(UuidFactoryServiceInterface::class)->uuid4(); } - - /** - * 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/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());