Skip to content

Commit c0f7b1d

Browse files
committed
Improved eager loading. Quick refund goods
1 parent 7c49d66 commit c0f7b1d

File tree

11 files changed

+86
-17
lines changed

11 files changed

+86
-17
lines changed

config/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use Bavix\Wallet\Services\PurchaseService;
4444
use Bavix\Wallet\Services\RegulatorService;
4545
use Bavix\Wallet\Services\TaxService;
46+
use Bavix\Wallet\Services\TransferService;
4647
use Bavix\Wallet\Services\WalletService;
4748

4849
return [
@@ -102,6 +103,7 @@
102103
'prepare' => PrepareService::class,
103104
'purchase' => PurchaseService::class,
104105
'tax' => TaxService::class,
106+
'transfer' => TransferService::class,
105107
'wallet' => WalletService::class,
106108
],
107109

src/Internal/Repository/TransferRepository.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Bavix\Wallet\Internal\Query\TransferQueryInterface;
99
use Bavix\Wallet\Internal\Transform\TransferDtoTransformerInterface;
1010
use Bavix\Wallet\Models\Transfer;
11+
use Illuminate\Support\Facades\DB;
1112

1213
final class TransferRepository implements TransferRepositoryInterface
1314
{
@@ -48,4 +49,18 @@ public function findBy(TransferQueryInterface $query): array
4849
->all()
4950
;
5051
}
52+
53+
/**
54+
* @param non-empty-array<int> $ids
55+
*/
56+
public function updateStatusByIds(string $status, array $ids): int
57+
{
58+
return $this->transfer->newQuery()
59+
->whereKey($ids)
60+
->update([
61+
'status' => $status,
62+
'status_last' => DB::raw('status'),
63+
])
64+
;
65+
}
5166
}

src/Internal/Repository/TransferRepositoryInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ public function insertOne(TransferDtoInterface $dto): Transfer;
2121
* @return Transfer[]
2222
*/
2323
public function findBy(TransferQueryInterface $query): array;
24+
25+
/**
26+
* @param non-empty-array<int> $ids
27+
*/
28+
public function updateStatusByIds(string $status, array $ids): int;
2429
}

src/Models/Transfer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Class Transfer.
1414
*
1515
* @property string $status
16+
* @property string $status_last
1617
* @property string $discount
1718
* @property int $deposit_id
1819
* @property int $withdraw_id

src/Services/TransferService.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Services;
6+
7+
use Bavix\Wallet\Internal\Repository\TransferRepositoryInterface;
8+
9+
final class TransferService implements TransferServiceInterface
10+
{
11+
public function __construct(private TransferRepositoryInterface $repository)
12+
{
13+
}
14+
15+
/**
16+
* @param int[] $ids
17+
*/
18+
public function updateStatusByIds(string $status, array $ids): bool
19+
{
20+
return count($ids) !== 0 && count($ids) === $this->repository->updateStatusByIds($status, $ids);
21+
}
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Services;
6+
7+
interface TransferServiceInterface
8+
{
9+
/**
10+
* @param int[] $ids
11+
*/
12+
public function updateStatusByIds(string $status, array $ids): bool;
13+
}

src/Traits/CartPay.php

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

55
namespace Bavix\Wallet\Traits;
66

7-
use function array_unique;
87
use Bavix\Wallet\Exceptions\BalanceIsEmpty;
98
use Bavix\Wallet\Exceptions\InsufficientFunds;
109
use Bavix\Wallet\Exceptions\ProductEnded;
@@ -26,6 +25,7 @@
2625
use Bavix\Wallet\Services\MetaServiceLegacy;
2726
use Bavix\Wallet\Services\PrepareServiceInterface;
2827
use Bavix\Wallet\Services\PurchaseServiceInterface;
28+
use Bavix\Wallet\Services\TransferServiceInterface;
2929
use function count;
3030
use Illuminate\Database\RecordsNotFoundException;
3131

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

187186
$index = 0;
188187
$objects = [];
188+
$transferIds = [];
189189
$transfers = array_values($transfers);
190190
$prepareService = app(PrepareServiceInterface::class);
191191
foreach ($cart->getBasketDto()->cursor() as $product) {
192+
$transferIds[] = $transfers[$index]->getKey();
192193
$objects[] = $prepareService->transferLazy(
193194
$product,
194195
$transfers[$index]->withdraw->wallet,
@@ -206,15 +207,9 @@ public function refundCart(CartInterface $cart, bool $force = false, bool $gifts
206207

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

209-
// fixme: one query update for
210-
foreach ($transfers as $transfer) {
211-
$results[] = $transfer->update([
212-
'status' => Transfer::STATUS_REFUND,
213-
'status_last' => $transfer->status,
214-
]);
215-
}
216-
217-
return count(array_unique($results)) === 1;
210+
return app(TransferServiceInterface::class)
211+
->updateStatusByIds(Transfer::STATUS_REFUND, $transferIds)
212+
;
218213
});
219214
}
220215

src/Traits/MorphOneWallet.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,18 @@ public function wallet(): MorphOne
2525
->getHolder($this)
2626
->morphOne(config('wallet.wallet.model', WalletModel::class), 'holder')
2727
->where('slug', config('wallet.wallet.default.slug', 'default'))
28-
->withDefault(array_merge(config('wallet.wallet.creating', []), [
29-
'name' => config('wallet.wallet.default.name', 'Default Wallet'),
30-
'slug' => config('wallet.wallet.default.slug', 'default'),
31-
'meta' => config('wallet.wallet.default.meta', []),
32-
'balance' => 0,
33-
]))
28+
->withDefault(static function (WalletModel $wallet, object $holder) {
29+
$wallet->forceFill(array_merge(config('wallet.wallet.creating', []), [
30+
'name' => config('wallet.wallet.default.name', 'Default Wallet'),
31+
'slug' => config('wallet.wallet.default.slug', 'default'),
32+
'meta' => config('wallet.wallet.default.meta', []),
33+
'balance' => 0,
34+
]));
35+
36+
if (property_exists($holder, 'exists') && $holder->exists) {
37+
$wallet->setRelation('holder', $holder);
38+
}
39+
})
3440
;
3541
}
3642
}

src/WalletServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@
8787
use Bavix\Wallet\Services\RegulatorServiceInterface;
8888
use Bavix\Wallet\Services\TaxService;
8989
use Bavix\Wallet\Services\TaxServiceInterface;
90+
use Bavix\Wallet\Services\TransferService;
91+
use Bavix\Wallet\Services\TransferServiceInterface;
9092
use Bavix\Wallet\Services\WalletService;
9193
use Bavix\Wallet\Services\WalletServiceInterface;
9294
use function config;
@@ -199,6 +201,7 @@ private function services(array $configure, array $cache): void
199201
$this->app->singleton(PrepareServiceInterface::class, $configure['prepare'] ?? PrepareService::class);
200202
$this->app->singleton(PurchaseServiceInterface::class, $configure['purchase'] ?? PurchaseService::class);
201203
$this->app->singleton(TaxServiceInterface::class, $configure['tax'] ?? TaxService::class);
204+
$this->app->singleton(TransferServiceInterface::class, $configure['transfer'] ?? TransferService::class);
202205
$this->app->singleton(WalletServiceInterface::class, $configure['wallet'] ?? WalletService::class);
203206

204207
$this->app->singleton(BookkeeperServiceInterface::class, fn () => $this->app->make(

tests/Units/Domain/CartTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public function testPay(): void
133133

134134
foreach ($transfers as $transfer) {
135135
self::assertSame(Transfer::STATUS_PAID, $transfer->status);
136+
self::assertNull($transfer->status_last);
136137
}
137138

138139
foreach ($cart->getItems() as $product) {
@@ -143,6 +144,7 @@ public function testPay(): void
143144
foreach ($transfers as $transfer) {
144145
$transfer->refresh();
145146
self::assertSame(Transfer::STATUS_REFUND, $transfer->status);
147+
self::assertSame(Transfer::STATUS_PAID, $transfer->status_last);
146148
}
147149
}
148150

0 commit comments

Comments
 (0)