Skip to content

Commit

Permalink
Documentation update and minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Apr 26, 2022
1 parent a6dfd91 commit 0c4a3fc
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 5 deletions.
15 changes: 10 additions & 5 deletions docs/cart.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Item extends Model implements ProductInterface

public function getAmountProduct(Customer $customer)
{
return 100;
return round($this->price * 100);
}

public function getMetaProduct(): ?array
Expand Down Expand Up @@ -70,7 +70,7 @@ class Item extends Model implements ProductLimitedInterface

public function getAmountProduct(Customer $customer)
{
return 100;
return round($this->price * 100);
}

public function getMetaProduct(): ?array
Expand Down Expand Up @@ -112,14 +112,19 @@ $products = Item::query()

$cart = app(Cart::class);
foreach ($products as $product) {
// add product's
$cart->addItem($product, $list[$product->slug]);
$cart = $cart->withItem($product, quantity: $list[$product->slug]);
}

$user->deposit($cart->getTotal());
$cartTotal = $cart->getTotal($user); // 15127
$user->deposit($cartTotal);
$user->balanceInt; // 15127
$user->balanceFloat; // 151.27

$cart = $cart->withItem(current($products), pricePerItem: 500); // 15127+500
$user->deposit(500);
$user->balanceInt; // 15627
$user->balanceFloat; // 156.27

(bool)$user->payCart($cart); // true
$user->balanceFloat; // 0
```
Expand Down
29 changes: 29 additions & 0 deletions docs/upgrade-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,32 @@ $cart = app(\Bavix\Wallet\Objects\Cart::class)

$cart = $cart->withItem($product);
```

## 8.1.x+ → 9.0.x

> The logic of storing transfers between accounts has changed.
> Previously, money could be credited to the user directly, but starting from version nine, all transactions go strictly between wallets.
> Thanks to this approach, finally, there will be full-fledged work with uuid identifiers in the project.
To migrate to the correct structure, you need to run the command:
```
artisan bx:transfer:fix
```

If the command fails, then the command must be restarted.
Continue until the command starts executing immediately (no bad entries left).

---

The product has been divided into two interfaces:
- `ProductLimitedInterface`. Needed to create limited goods;
- `ProductInterface`. Needed for an infinite number of products;

The old Product interface should be replaced with one of these.

Replace `Bavix\Wallet\Interfaces\Product` to `Bavix\Wallet\Interfaces\ProductLimitedInterface`.


---


33 changes: 33 additions & 0 deletions docs/wallet-transfer.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,37 @@ $firstWallet->balance; // -400
$lastWallet->balance; // 500
```

It worked!

## Change the meta and confirmation.

Check the user's balance.

```php
$firstWallet->balanceInt; // 1_000
$secondWallet->balanceInt; // 0
```

We will execute the transfer, but without confirmation of the withdrawal of funds.

```php
use Bavix\Wallet\External\Dto\Extra;
use Bavix\Wallet\External\Dto\Option;

/** @var $firstWallet \Bavix\Wallet\Interfaces\Wallet */
$transfer = $firstWallet->transfer($secondWallet, 500, new Extra(
deposit: ['message' => 'Hello, secondWallet!'],
withdraw: new Option(meta: ['something' => 'anything'], confirmed: false)
));

$transfer->withdraw->meta; // ['something' => 'anything']
$transfer->withdraw->confirmed; // false

$transfer->deposit->meta; // ['message' => 'Hello, secondWallet!']
$transfer->deposit->confirmed; // true

$firstWallet->balanceInt; // 1_000
$secondWallet->balanceInt; // 500
```

It worked!
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ parameters:
# laravel
- Illuminate\Support\ServiceProvider
- Illuminate\Database\Eloquent\Model
- Illuminate\Console\Command

# php exceptions
- LogicException
Expand Down
45 changes: 45 additions & 0 deletions src/Commands/TransferFixCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Commands;

use Bavix\Wallet\Models\Transfer;
use Bavix\Wallet\Models\Wallet;
use Bavix\Wallet\Services\CastServiceInterface;
use Illuminate\Console\Command;

final class TransferFixCommand extends Command
{
protected $signature = 'bx:transfer:fix';
protected $description = 'Brings transfers to the correct form.';

public function handle(Wallet $wallet, Transfer $transfer, CastServiceInterface $castService): void
{
$query = $transfer::with(['from', 'to'])
->orWhere('from_type', '<>', $wallet->getMorphClass())
->orWhere('to_type', '<>', $wallet->getMorphClass())
;

$query->each(fn (Transfer $object) => $this->fix($castService, $wallet, $object));
}

private function fix(CastServiceInterface $castService, Wallet $wallet, Transfer $transfer): void
{
if ($transfer->from_type !== $wallet->getMorphClass()) {
$fromWallet = $castService->getWallet($transfer->from);
$transfer->from_type = $fromWallet->getMorphClass();
$transfer->from_id = $fromWallet->getKey();
}

if ($transfer->to_type !== $wallet->getMorphClass()) {
$toWallet = $castService->getWallet($transfer->to);
$transfer->to_type = $toWallet->getMorphClass();
$transfer->to_id = $toWallet->getKey();
}

if ($transfer->isDirty()) {
$transfer->save();
}
}
}
2 changes: 2 additions & 0 deletions src/Models/Transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
* @property string $discount
* @property int $deposit_id
* @property int $withdraw_id
* @property Wallet $from
* @property string $from_type
* @property int $from_id
* @property Wallet $to
* @property string $to_type
* @property int $to_id
* @property string $uuid
Expand Down
3 changes: 3 additions & 0 deletions src/Objects/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public function withMeta(array $meta): self
return $self;
}

/**
* @param positive-int $quantity
*/
public function withItem(ProductInterface $product, int $quantity = 1, int|string|null $pricePerItem = null): self
{
$self = clone $this;
Expand Down
2 changes: 2 additions & 0 deletions src/WalletServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Bavix\Wallet;

use Bavix\Wallet\Commands\TransferFixCommand;
use Bavix\Wallet\Internal\Assembler\AvailabilityDtoAssembler;
use Bavix\Wallet\Internal\Assembler\AvailabilityDtoAssemblerInterface;
use Bavix\Wallet\Internal\Assembler\BalanceUpdatedEventAssembler;
Expand Down Expand Up @@ -133,6 +134,7 @@ public function boot(): void
public function register(): void
{
$this->mergeConfigFrom(dirname(__DIR__).'/config/config.php', 'wallet');
$this->commands([TransferFixCommand::class]);

$configure = config('wallet', []);

Expand Down
63 changes: 63 additions & 0 deletions tests/Units/Upgrade/TransferFixTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Test\Units\Upgrade;

use Bavix\Wallet\Test\Infra\Factories\BuyerFactory;
use Bavix\Wallet\Test\Infra\Models\Buyer;
use Bavix\Wallet\Test\Infra\TestCase;

/**
* @internal
*/
final class TransferFixTest extends TestCase
{
public function testTransferFixCommand(): void
{
/** @var Buyer $buyer1 */
/** @var Buyer $buyer2 */
[$buyer1, $buyer2] = BuyerFactory::times(2)->create();
$transfer = $buyer1->forceTransfer($buyer2, 1_000_000);

self::assertSame($buyer1->wallet->getMorphClass(), $transfer->from->getMorphClass());
self::assertSame($buyer1->wallet->getKey(), $transfer->from->getKey());

self::assertSame($buyer2->wallet->getMorphClass(), $transfer->to->getMorphClass());
self::assertSame($buyer2->wallet->getKey(), $transfer->to->getKey());

$buyer1->wallet->transfers()
->update([
'from_type' => $buyer1->getMorphClass(),
'from_id' => $buyer1->getKey(),

'to_type' => $buyer2->getMorphClass(),
'to_id' => $buyer2->getKey(),
])
;

$transfer->refresh();
$transfer->from->refresh();
$transfer->to->refresh();

self::assertSame($buyer1->getMorphClass(), $transfer->from->getMorphClass());
self::assertSame($buyer1->getKey(), $transfer->from->getKey());

self::assertSame($buyer2->getMorphClass(), $transfer->to->getMorphClass());
self::assertSame($buyer2->getKey(), $transfer->to->getKey());

$this->artisan('bx:transfer:fix')
->assertSuccessful()
;

$transfer->refresh();
$transfer->from->refresh();
$transfer->to->refresh();

self::assertSame($buyer1->wallet->getMorphClass(), $transfer->from->getMorphClass());
self::assertSame($buyer1->wallet->getKey(), $transfer->from->getKey());

self::assertSame($buyer2->wallet->getMorphClass(), $transfer->to->getMorphClass());
self::assertSame($buyer2->wallet->getKey(), $transfer->to->getKey());
}
}

0 comments on commit 0c4a3fc

Please sign in to comment.