From 253c9acaf8bda4212c2acb888300f25d945ba672 Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Fri, 22 Apr 2022 22:12:39 +0300 Subject: [PATCH 1/2] An old bug that prevented full uuid support. --- src/Services/PrepareService.php | 4 ++-- src/Services/PurchaseService.php | 11 +++++++---- src/Traits/HasGift.php | 2 +- tests/Infra/Models/Item.php | 4 +++- tests/Units/Domain/DiscountTaxTest.php | 5 +++-- tests/Units/Domain/DiscountTest.php | 5 +++-- tests/Units/Domain/MultiWalletTest.php | 4 ++-- tests/Units/Domain/ProductTest.php | 5 +++-- 8 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/Services/PrepareService.php b/src/Services/PrepareService.php index 75008e67a..06951888a 100644 --- a/src/Services/PrepareService.php +++ b/src/Services/PrepareService.php @@ -97,11 +97,11 @@ public function transferLazy( return $this->transferLazyDtoAssembler->create( $from, - $to, + $toWallet, $discount, $fee, $this->withdraw($from, $withdrawAmount, $withdrawOption->getMeta(), $withdrawOption->isConfirmed()), - $this->deposit($to, $depositAmount, $depositOption->getMeta(), $depositOption->isConfirmed()), + $this->deposit($toWallet, $depositAmount, $depositOption->getMeta(), $depositOption->isConfirmed()), $status ); } diff --git a/src/Services/PurchaseService.php b/src/Services/PurchaseService.php index ad5066495..0a9d5fc45 100644 --- a/src/Services/PurchaseService.php +++ b/src/Services/PurchaseService.php @@ -11,6 +11,10 @@ final class PurchaseService implements PurchaseServiceInterface { + public function __construct(private CastServiceInterface $castService) + { + } + public function already(Customer $customer, BasketDtoInterface $basketDto, bool $gifts = false): array { $status = $gifts @@ -20,8 +24,7 @@ public function already(Customer $customer, BasketDtoInterface $basketDto, bool $arrays = []; $query = $customer->transfers(); foreach ($basketDto->items() as $itemDto) { - /** @var Model $product */ - $product = $itemDto->product(); + $wallet = $this->castService->getWallet($itemDto->product()); /** * As part of my work, "with" was added, it gives a 50x boost for a huge number of returns. In this case, @@ -30,8 +33,8 @@ public function already(Customer $customer, BasketDtoInterface $basketDto, bool */ $arrays[] = (clone $query) ->with(['deposit', 'withdraw.wallet']) - ->where('to_type', $product->getMorphClass()) - ->where('to_id', $product->getKey()) + ->where('to_type', $wallet->getMorphClass()) + ->where('to_id', $wallet->getKey()) ->whereIn('status', $status) ->orderBy('id', 'desc') ->limit(count($itemDto)) diff --git a/src/Traits/HasGift.php b/src/Traits/HasGift.php index 8dfcacc8d..58dd566ce 100644 --- a/src/Traits/HasGift.php +++ b/src/Traits/HasGift.php @@ -84,7 +84,7 @@ public function gift(Wallet $to, ProductInterface $product, bool $force = false) $withdraw->getKey(), Transfer::STATUS_GIFT, $castService->getWallet($to), - $castService->getModel($product), + $castService->getWallet($product), $discount, $fee ); diff --git a/tests/Infra/Models/Item.php b/tests/Infra/Models/Item.php index 0a20cf480..51e479b5c 100644 --- a/tests/Infra/Models/Item.php +++ b/tests/Infra/Models/Item.php @@ -9,6 +9,7 @@ use Bavix\Wallet\Models\Transfer; use Bavix\Wallet\Models\Wallet; use Bavix\Wallet\Services\CastService; +use Bavix\Wallet\Services\CastServiceInterface; use Bavix\Wallet\Traits\HasWallet; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphMany; @@ -55,7 +56,8 @@ public function getMetaProduct(): ?array */ public function boughtGoods(array $walletIds): MorphMany { - return $this + return app(CastServiceInterface::class) + ->getWallet($this) ->morphMany(config('wallet.transfer.model', Transfer::class), 'to') ->where('status', Transfer::STATUS_PAID) ->where('from_type', config('wallet.wallet.model', Wallet::class)) diff --git a/tests/Units/Domain/DiscountTaxTest.php b/tests/Units/Domain/DiscountTaxTest.php index e8bb8e6da..56e616b25 100644 --- a/tests/Units/Domain/DiscountTaxTest.php +++ b/tests/Units/Domain/DiscountTaxTest.php @@ -65,12 +65,13 @@ public function testPay(): void self::assertInstanceOf(Buyer::class, $transfer->from->holder); self::assertInstanceOf(Wallet::class, $transfer->from); - self::assertInstanceOf(Item::class, $transfer->to); + self::assertInstanceOf(Item::class, $transfer->to->holder); self::assertInstanceOf(Wallet::class, $transfer->to->wallet); self::assertSame($buyer->wallet->getKey(), $transfer->from->getKey()); self::assertSame($buyer->getKey(), $transfer->from->holder->getKey()); - self::assertSame($product->getKey(), $transfer->to->getKey()); + self::assertSame($product->wallet->getKey(), $transfer->to->getKey()); + self::assertSame($product->getKey(), $transfer->to->holder->getKey()); } public function testRefundPersonalDiscountAndTax(): void diff --git a/tests/Units/Domain/DiscountTest.php b/tests/Units/Domain/DiscountTest.php index f5ab87eb2..897662749 100644 --- a/tests/Units/Domain/DiscountTest.php +++ b/tests/Units/Domain/DiscountTest.php @@ -60,12 +60,13 @@ public function testPay(): void self::assertInstanceOf(Buyer::class, $transfer->from->holder); self::assertInstanceOf(Wallet::class, $transfer->from); - self::assertInstanceOf(Item::class, $transfer->to); + self::assertInstanceOf(Item::class, $transfer->to->holder); self::assertInstanceOf(Wallet::class, $transfer->to->wallet); self::assertSame($buyer->wallet->getKey(), $transfer->from->getKey()); self::assertSame($buyer->getKey(), $transfer->from->holder->getKey()); - self::assertSame($product->getKey(), $transfer->to->getKey()); + self::assertSame($product->wallet->getKey(), $transfer->to->getKey()); + self::assertSame($product->getKey(), $transfer->to->holder->getKey()); } public function testItemTransactions(): void diff --git a/tests/Units/Domain/MultiWalletTest.php b/tests/Units/Domain/MultiWalletTest.php index 6c9c32f46..0671a92d4 100644 --- a/tests/Units/Domain/MultiWalletTest.php +++ b/tests/Units/Domain/MultiWalletTest.php @@ -500,7 +500,7 @@ public function testPay(): void self::assertInstanceOf(UserMulti::class, $paidTransfer->withdraw->payable); self::assertSame($user->getKey(), $paidTransfer->withdraw->payable->getKey()); self::assertSame($transfer->from->id, $a->id); - self::assertSame($transfer->to->id, $product->id); + self::assertSame($transfer->to->id, $product->wallet->id); self::assertSame($transfer->status, Transfer::STATUS_PAID); self::assertSame($a->balanceInt, 0); self::assertSame($product->balanceInt, $product->getAmountProduct($a)); @@ -512,7 +512,7 @@ public function testPay(): void self::assertInstanceOf(UserMulti::class, $paidTransfer->withdraw->payable); self::assertSame($user->getKey(), $paidTransfer->withdraw->payable->getKey()); self::assertSame($transfer->from->id, $b->id); - self::assertSame($transfer->to->id, $product->id); + self::assertSame($transfer->to->id, $product->wallet->id); self::assertSame($transfer->status, Transfer::STATUS_PAID); self::assertSame($b->balanceInt, 0); self::assertSame($product->balanceInt, $product->getAmountProduct($b) * 2); diff --git a/tests/Units/Domain/ProductTest.php b/tests/Units/Domain/ProductTest.php index e87f55f2a..4a0c4bb67 100644 --- a/tests/Units/Domain/ProductTest.php +++ b/tests/Units/Domain/ProductTest.php @@ -60,12 +60,13 @@ public function testPay(): void self::assertInstanceOf(Buyer::class, $transfer->from->holder); self::assertInstanceOf(Wallet::class, $transfer->from); - self::assertInstanceOf(Item::class, $transfer->to); + self::assertInstanceOf(Item::class, $transfer->to->holder); self::assertInstanceOf(Wallet::class, $transfer->to->wallet); self::assertSame($buyer->wallet->getKey(), $transfer->from->getKey()); self::assertSame($buyer->getKey(), $transfer->from->holder->getKey()); - self::assertSame($product->getKey(), $transfer->to->getKey()); + self::assertSame($product->wallet->getKey(), $transfer->to->getKey()); + self::assertSame($product->getKey(), $transfer->to->holder->getKey()); self::assertSame(0, $buyer->balanceInt); self::assertNull($buyer->safePay($product)); From e35d855a26db2be06995d9646c878ccc8a74deb2 Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Fri, 22 Apr 2022 22:14:20 +0300 Subject: [PATCH 2/2] ecs-fix --- src/Services/PurchaseService.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Services/PurchaseService.php b/src/Services/PurchaseService.php index 0a9d5fc45..4b9cd8bed 100644 --- a/src/Services/PurchaseService.php +++ b/src/Services/PurchaseService.php @@ -7,7 +7,6 @@ use Bavix\Wallet\Interfaces\Customer; use Bavix\Wallet\Internal\Dto\BasketDtoInterface; use Bavix\Wallet\Models\Transfer; -use Illuminate\Database\Eloquent\Model; final class PurchaseService implements PurchaseServiceInterface {