Skip to content

Commit

Permalink
#438 UUID Duplicate entry fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Feb 18, 2022
1 parent 66be2dd commit 4fb0941
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/Models/Wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Bavix\Wallet\Internal\Exceptions\LockProviderNotFoundException;
use Bavix\Wallet\Internal\Exceptions\TransactionFailedException;
use Bavix\Wallet\Internal\Service\MathServiceInterface;
use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface;
use Bavix\Wallet\Services\AtomicServiceInterface;
use Bavix\Wallet\Services\RegulatorServiceInterface;
use Bavix\Wallet\Traits\CanConfirm;
Expand Down Expand Up @@ -167,4 +168,9 @@ public function getCurrencyAttribute(): string
{
return $this->meta['currency'] ?? Str::upper($this->slug);
}

protected function initializeMorphOneWallet(): void
{
$this->uuid = app(UuidFactoryServiceInterface::class)->uuid4();
}
}
2 changes: 0 additions & 2 deletions src/Traits/MorphOneWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Bavix\Wallet\Traits;

use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface;
use Bavix\Wallet\Models\Wallet as WalletModel;
use Bavix\Wallet\Services\CastServiceInterface;
use Illuminate\Database\Eloquent\Relations\MorphOne;
Expand All @@ -31,7 +30,6 @@ public function wallet(): MorphOne
'name' => config('wallet.wallet.default.name', 'Default Wallet'),
'slug' => config('wallet.wallet.default.slug', 'default'),
'meta' => config('wallet.wallet.default.meta', []),
'uuid' => app(UuidFactoryServiceInterface::class)->uuid4(),
'balance' => 0,
]))
;
Expand Down
33 changes: 33 additions & 0 deletions tests/Units/Domain/EagerLoadingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Test\Units\Domain;

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

/**
* @internal
*/
class EagerLoadingTest extends TestCase
{
public function testUuidDuplicate(): void
{
BuyerFactory::times(10)->create();

/** @var Buyer[] $buyers */
$buyers = Buyer::with('wallet')->paginate(10);

$uuids = [];
$balances = [];
foreach ($buyers as $buyer) {
$uuids[] = $buyer->wallet->uuid;
$balances[] = $buyer->wallet->balanceInt;
}

self::assertCount(10, array_unique($uuids));
self::assertCount(1, array_unique($balances));
}
}

0 comments on commit 4fb0941

Please sign in to comment.