Skip to content

Commit

Permalink
Improved eager loading. Quick refund goods
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed May 2, 2022
1 parent 7c49d66 commit c0f7b1d
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 17 deletions.
2 changes: 2 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use Bavix\Wallet\Services\PurchaseService;
use Bavix\Wallet\Services\RegulatorService;
use Bavix\Wallet\Services\TaxService;
use Bavix\Wallet\Services\TransferService;
use Bavix\Wallet\Services\WalletService;

return [
Expand Down Expand Up @@ -102,6 +103,7 @@
'prepare' => PrepareService::class,
'purchase' => PurchaseService::class,
'tax' => TaxService::class,
'transfer' => TransferService::class,
'wallet' => WalletService::class,
],

Expand Down
15 changes: 15 additions & 0 deletions src/Internal/Repository/TransferRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Bavix\Wallet\Internal\Query\TransferQueryInterface;
use Bavix\Wallet\Internal\Transform\TransferDtoTransformerInterface;
use Bavix\Wallet\Models\Transfer;
use Illuminate\Support\Facades\DB;

final class TransferRepository implements TransferRepositoryInterface
{
Expand Down Expand Up @@ -48,4 +49,18 @@ public function findBy(TransferQueryInterface $query): array
->all()
;
}

/**
* @param non-empty-array<int> $ids
*/
public function updateStatusByIds(string $status, array $ids): int
{
return $this->transfer->newQuery()
->whereKey($ids)
->update([
'status' => $status,
'status_last' => DB::raw('status'),
])
;
}
}
5 changes: 5 additions & 0 deletions src/Internal/Repository/TransferRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public function insertOne(TransferDtoInterface $dto): Transfer;
* @return Transfer[]
*/
public function findBy(TransferQueryInterface $query): array;

/**
* @param non-empty-array<int> $ids
*/
public function updateStatusByIds(string $status, array $ids): int;
}
1 change: 1 addition & 0 deletions src/Models/Transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Class Transfer.
*
* @property string $status
* @property string $status_last
* @property string $discount
* @property int $deposit_id
* @property int $withdraw_id
Expand Down
22 changes: 22 additions & 0 deletions src/Services/TransferService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Services;

use Bavix\Wallet\Internal\Repository\TransferRepositoryInterface;

final class TransferService implements TransferServiceInterface
{
public function __construct(private TransferRepositoryInterface $repository)
{
}

/**
* @param int[] $ids
*/
public function updateStatusByIds(string $status, array $ids): bool
{
return count($ids) !== 0 && count($ids) === $this->repository->updateStatusByIds($status, $ids);
}
}
13 changes: 13 additions & 0 deletions src/Services/TransferServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Services;

interface TransferServiceInterface
{
/**
* @param int[] $ids
*/
public function updateStatusByIds(string $status, array $ids): bool;
}
17 changes: 6 additions & 11 deletions src/Traits/CartPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Bavix\Wallet\Traits;

use function array_unique;
use Bavix\Wallet\Exceptions\BalanceIsEmpty;
use Bavix\Wallet\Exceptions\InsufficientFunds;
use Bavix\Wallet\Exceptions\ProductEnded;
Expand All @@ -26,6 +25,7 @@
use Bavix\Wallet\Services\MetaServiceLegacy;
use Bavix\Wallet\Services\PrepareServiceInterface;
use Bavix\Wallet\Services\PurchaseServiceInterface;
use Bavix\Wallet\Services\TransferServiceInterface;
use function count;
use Illuminate\Database\RecordsNotFoundException;

Expand Down Expand Up @@ -174,7 +174,6 @@ public function safeRefundCart(CartInterface $cart, bool $force = false, bool $g
public function refundCart(CartInterface $cart, bool $force = false, bool $gifts = false): bool
{
return app(AtomicServiceInterface::class)->block($this, function () use ($cart, $force, $gifts) {
$results = [];
$transfers = app(PurchaseServiceInterface::class)->already($this, $cart->getBasketDto(), $gifts);
if (count($transfers) !== $cart->getBasketDto()->total()) {
throw new ModelNotFoundException(
Expand All @@ -186,9 +185,11 @@ public function refundCart(CartInterface $cart, bool $force = false, bool $gifts

$index = 0;
$objects = [];
$transferIds = [];
$transfers = array_values($transfers);
$prepareService = app(PrepareServiceInterface::class);
foreach ($cart->getBasketDto()->cursor() as $product) {
$transferIds[] = $transfers[$index]->getKey();
$objects[] = $prepareService->transferLazy(
$product,
$transfers[$index]->withdraw->wallet,
Expand All @@ -206,15 +207,9 @@ public function refundCart(CartInterface $cart, bool $force = false, bool $gifts

app(CommonServiceLegacy::class)->applyTransfers($objects);

// fixme: one query update for
foreach ($transfers as $transfer) {
$results[] = $transfer->update([
'status' => Transfer::STATUS_REFUND,
'status_last' => $transfer->status,
]);
}

return count(array_unique($results)) === 1;
return app(TransferServiceInterface::class)
->updateStatusByIds(Transfer::STATUS_REFUND, $transferIds)
;
});
}

Expand Down
18 changes: 12 additions & 6 deletions src/Traits/MorphOneWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ public function wallet(): MorphOne
->getHolder($this)
->morphOne(config('wallet.wallet.model', WalletModel::class), 'holder')
->where('slug', config('wallet.wallet.default.slug', 'default'))
->withDefault(array_merge(config('wallet.wallet.creating', []), [
'name' => config('wallet.wallet.default.name', 'Default Wallet'),
'slug' => config('wallet.wallet.default.slug', 'default'),
'meta' => config('wallet.wallet.default.meta', []),
'balance' => 0,
]))
->withDefault(static function (WalletModel $wallet, object $holder) {
$wallet->forceFill(array_merge(config('wallet.wallet.creating', []), [
'name' => config('wallet.wallet.default.name', 'Default Wallet'),
'slug' => config('wallet.wallet.default.slug', 'default'),
'meta' => config('wallet.wallet.default.meta', []),
'balance' => 0,
]));

if (property_exists($holder, 'exists') && $holder->exists) {
$wallet->setRelation('holder', $holder);
}
})
;
}
}
3 changes: 3 additions & 0 deletions src/WalletServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@
use Bavix\Wallet\Services\RegulatorServiceInterface;
use Bavix\Wallet\Services\TaxService;
use Bavix\Wallet\Services\TaxServiceInterface;
use Bavix\Wallet\Services\TransferService;
use Bavix\Wallet\Services\TransferServiceInterface;
use Bavix\Wallet\Services\WalletService;
use Bavix\Wallet\Services\WalletServiceInterface;
use function config;
Expand Down Expand Up @@ -199,6 +201,7 @@ private function services(array $configure, array $cache): void
$this->app->singleton(PrepareServiceInterface::class, $configure['prepare'] ?? PrepareService::class);
$this->app->singleton(PurchaseServiceInterface::class, $configure['purchase'] ?? PurchaseService::class);
$this->app->singleton(TaxServiceInterface::class, $configure['tax'] ?? TaxService::class);
$this->app->singleton(TransferServiceInterface::class, $configure['transfer'] ?? TransferService::class);
$this->app->singleton(WalletServiceInterface::class, $configure['wallet'] ?? WalletService::class);

$this->app->singleton(BookkeeperServiceInterface::class, fn () => $this->app->make(
Expand Down
2 changes: 2 additions & 0 deletions tests/Units/Domain/CartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public function testPay(): void

foreach ($transfers as $transfer) {
self::assertSame(Transfer::STATUS_PAID, $transfer->status);
self::assertNull($transfer->status_last);
}

foreach ($cart->getItems() as $product) {
Expand All @@ -143,6 +144,7 @@ public function testPay(): void
foreach ($transfers as $transfer) {
$transfer->refresh();
self::assertSame(Transfer::STATUS_REFUND, $transfer->status);
self::assertSame(Transfer::STATUS_PAID, $transfer->status_last);
}
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Units/Domain/EagerLoadingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function testUuidDuplicate(): void
/** @var Buyer[]|Collection $buyerTimes */
$buyerTimes = BuyerFactory::times(10)->create();
foreach ($buyerTimes as $buyerTime) {
self::assertTrue($buyerTime->wallet->relationLoaded('holder'));
$buyerTime->deposit(100);
}

Expand All @@ -34,6 +35,8 @@ public function testUuidDuplicate(): void
$balances = [];
foreach ($buyers as $buyer) {
self::assertTrue($buyer->relationLoaded('wallet'));
// self::assertTrue($buyer->wallet->relationLoaded('holder'));
// fixme: I did not find a way to load the buyer, maybe someday I will get there.

$uuids[] = $buyer->wallet->uuid;
$balances[] = $buyer->wallet->balanceInt;
Expand Down Expand Up @@ -74,5 +77,7 @@ public function testMultiWallets(): void
self::assertTrue($user->relationLoaded('wallets'));
self::assertNotNull($user->getWallet('hello'));
self::assertNotNull($user->getWallet('world'));
self::assertTrue($user->getWallet('hello')->relationLoaded('holder'));
self::assertSame($user, $user->getWallet('hello')->holder);
}
}

0 comments on commit c0f7b1d

Please sign in to comment.