Skip to content

Commit a8d79a3

Browse files
authored
Merge pull request #822 from bavix/819-need-this-for-multi-tenancy-teams
[10.x] add getDynamicDefaultSlug
2 parents de6d893 + ed81f3f commit a8d79a3

File tree

5 files changed

+158
-13
lines changed

5 files changed

+158
-13
lines changed

changelog.md

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,44 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [10.1.0] - 2023-12-28
10+
### Bump
11+
* build(deps-dev): update laravel/cashier requirement from ^14.12 to ^15.0 by @dependabot in https://github.com/bavix/laravel-wallet/pull/817
12+
* build(deps): bump axios from 1.6.2 to 1.6.3 by @dependabot in https://github.com/bavix/laravel-wallet/pull/821
13+
14+
### Added
15+
* add wallet getDynamicDefaultSlug by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/822
16+
17+
## [10.0.2] - 2023-12-14
18+
### Bump
19+
* Bump bavix/.github from 0.0.5 to 0.0.7 by @dependabot in https://github.com/bavix/laravel-wallet/pull/784
20+
* docs: accessing float amount by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/786
21+
* docs: changing wallet decimal_places by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/785
22+
* remove driftingly/rector-laravel by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/787
23+
* Bump postcss from 8.4.19 to 8.4.31 by @dependabot in https://github.com/bavix/laravel-wallet/pull/788
24+
* Bump webpack from 5.88.2 to 5.89.0 by @dependabot in https://github.com/bavix/laravel-wallet/pull/790
25+
* Bump size-limit from 9.0.0 to 10.0.1 by @dependabot in https://github.com/bavix/laravel-wallet/pull/792
26+
* Bump bavix/.github from 0.0.7 to 0.0.8 by @dependabot in https://github.com/bavix/laravel-wallet/pull/793
27+
* Bump axios from 1.5.1 to 1.6.0 by @dependabot in https://github.com/bavix/laravel-wallet/pull/795
28+
* Bump size-limit from 10.0.1 to 10.0.2 by @dependabot in https://github.com/bavix/laravel-wallet/pull/797
29+
* build(deps): bump actions/checkout from 3 to 4 by @dependabot in https://github.com/bavix/laravel-wallet/pull/799
30+
* build(deps): bump axios from 1.6.0 to 1.6.1 by @dependabot in https://github.com/bavix/laravel-wallet/pull/801
31+
* build(deps): bump size-limit from 10.0.2 to 11.0.0 by @dependabot in https://github.com/bavix/laravel-wallet/pull/802
32+
* build(deps): bump axios from 1.6.1 to 1.6.2 by @dependabot in https://github.com/bavix/laravel-wallet/pull/803
33+
* build(deps): bump bavix/.github from 0.0.8 to 0.0.9 by @dependabot in https://github.com/bavix/laravel-wallet/pull/808
34+
* build(deps): bump size-limit from 11.0.0 to 11.0.1 by @dependabot in https://github.com/bavix/laravel-wallet/pull/810
35+
* build(deps): bump actions/stale from 8 to 9 by @dependabot in https://github.com/bavix/laravel-wallet/pull/811
36+
* build(deps): bump bavix/.github from 0.0.9 to 0.1.0 by @dependabot in https://github.com/bavix/laravel-wallet/pull/812
37+
* build(deps): bump JetBrains/qodana-action from 2023.2 to 2023.3 by @dependabot in https://github.com/bavix/laravel-wallet/pull/813
38+
39+
### Removed
40+
* remove cozyhouse by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/794
41+
42+
### Added
43+
* add support database cache by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/815
44+
* Qodana by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/798
45+
* add support php 8.3 by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/796
46+
947
## [10.0.1] - 2023-09-30
1048
### Bump
1149
* Bump semver from 7.3.8 to 7.5.4 by @dependabot in https://github.com/bavix/laravel-wallet/pull/726
@@ -15,11 +53,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1553
* Updated laravel rector set by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/733
1654
* Update driftingly/rector-laravel requirement from ^0.21 to ^0.22 by @dependabot in https://github.com/bavix/laravel-wallet/pull/735
1755
* Create npm workflow by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/736
18-
* Update docs.yaml by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/737
19-
* Update docs.yaml by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/739
20-
* Update docs.yaml by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/740
21-
* Update docs.yaml by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/741
22-
* Lumen documentation update by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/743
2356
* [Snyk] Upgrade webpack-cli from 5.0.0 to 5.1.4 by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/747
2457
* [Snyk] Upgrade axios from 1.3.4 to 1.4.0 by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/748
2558
* [Snyk] Upgrade size-limit from 8.2.4 to 8.2.6 by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/750
@@ -31,18 +64,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3164
* Bump size-limit from 8.2.6 to 9.0.0 by @dependabot in https://github.com/bavix/laravel-wallet/pull/762
3265
* Bump actions/stale from 6 to 8 by @dependabot in https://github.com/bavix/laravel-wallet/pull/761
3366
* Bump actions/checkout from 3 to 4 by @dependabot in https://github.com/bavix/laravel-wallet/pull/760
34-
* docs: update wording to sound grammatically correct by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/766
35-
* docs: customize name and slug of default wallet by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/765
36-
* docs: fixed grammatical error by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/764
3767
* Bump uuid from 9.0.0 to 9.0.1 by @dependabot in https://github.com/bavix/laravel-wallet/pull/768
3868
* checking the Wallet class extensibility by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/769
69+
* Update driftingly/rector-laravel requirement from ^0.24 to ^0.25 by @dependabot in https://github.com/bavix/laravel-wallet/pull/779
70+
* Update driftingly/rector-laravel requirement from ^0.25 to ^0.26 by @dependabot in https://github.com/bavix/laravel-wallet/pull/780
71+
* Bump axios from 1.5.0 to 1.5.1 by @dependabot in https://github.com/bavix/laravel-wallet/pull/781
72+
73+
### Docs
3974
* docs: Create configuration.md by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/771
4075
* docs: add configuration file to sidebar by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/772
4176
* docs: move configuration details to separate file by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/773
4277
* docs: update for clarity by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/774
43-
* Update driftingly/rector-laravel requirement from ^0.24 to ^0.25 by @dependabot in https://github.com/bavix/laravel-wallet/pull/779
44-
* Update driftingly/rector-laravel requirement from ^0.25 to ^0.26 by @dependabot in https://github.com/bavix/laravel-wallet/pull/780
45-
* Bump axios from 1.5.0 to 1.5.1 by @dependabot in https://github.com/bavix/laravel-wallet/pull/781
78+
* docs: update wording to sound grammatically correct by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/766
79+
* docs: customize name and slug of default wallet by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/765
80+
* docs: fixed grammatical error by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/764
81+
* Update docs.yaml by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/737
82+
* Update docs.yaml by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/739
83+
* Update docs.yaml by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/740
84+
* Update docs.yaml by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/741
85+
* Lumen documentation update by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/743
86+
87+
### Added
4688
* Add semgrep by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/783
4789

4890
## [10.0.0] - 2023-07-08
@@ -1047,7 +1089,9 @@ The operation is now executed in the transaction and updates the new `refund` fi
10471089
- Exceptions: AmountInvalid, BalanceIsEmpty.
10481090
- Models: Transfer, Transaction.
10491091

1050-
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/10.0.1...develop
1092+
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/10.1.0...develop
1093+
[10.1.0]: https://github.com/bavix/laravel-wallet/compare/10.0.2...10.1.0
1094+
[10.0.2]: https://github.com/bavix/laravel-wallet/compare/10.0.1...10.0.2
10511095
[10.0.1]: https://github.com/bavix/laravel-wallet/compare/10.0.0...10.0.1
10521096
[10.0.0]: https://github.com/bavix/laravel-wallet/compare/9.6.2...10.0.0
10531097
[9.6.2]: https://github.com/bavix/laravel-wallet/compare/9.6.1...9.6.2

src/Traits/MorphOneWallet.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ public function wallet(): MorphOne
3434
->where('slug', config('wallet.wallet.default.slug', 'default'))
3535
->withDefault(static function (WalletModel $wallet, object $holder) use ($castService) {
3636
$model = $castService->getModel($holder);
37+
38+
$slug = method_exists($model, 'getDynamicDefaultSlug')
39+
? $model->getDynamicDefaultSlug()
40+
: config('wallet.wallet.default.slug', 'default');
41+
3742
$wallet->forceFill(array_merge(config('wallet.wallet.creating', []), [
3843
'name' => config('wallet.wallet.default.name', 'Default Wallet'),
39-
'slug' => config('wallet.wallet.default.slug', 'default'),
44+
'slug' => $slug,
4045
'meta' => config('wallet.wallet.default.meta', []),
4146
'balance' => 0,
4247
]));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Test\Infra\Factories;
6+
7+
use Bavix\Wallet\Test\Infra\Models\UserDynamic;
8+
use Illuminate\Database\Eloquent\Factories\Factory;
9+
10+
/**
11+
* @extends Factory<UserDynamic>
12+
*/
13+
final class UserDynamicFactory extends Factory
14+
{
15+
protected $model = UserDynamic::class;
16+
17+
public function definition(): array
18+
{
19+
return [
20+
'name' => fake()
21+
->name,
22+
'email' => fake()
23+
->unique()
24+
->safeEmail,
25+
];
26+
}
27+
}

tests/Infra/Models/UserDynamic.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Test\Infra\Models;
6+
7+
use Bavix\Wallet\Interfaces\Wallet;
8+
use Bavix\Wallet\Traits\HasWallet;
9+
use Illuminate\Database\Eloquent\Model;
10+
11+
/**
12+
* @property string $name
13+
* @property string $email
14+
*
15+
* @method int getKey()
16+
*/
17+
final class UserDynamic extends Model implements Wallet
18+
{
19+
use HasWallet;
20+
21+
/**
22+
* @var string[]
23+
*/
24+
protected $fillable = ['name', 'email'];
25+
26+
public function getTable(): string
27+
{
28+
return 'users';
29+
}
30+
31+
public function getDynamicDefaultSlug(): string
32+
{
33+
return 'default-' . $this->email;
34+
}
35+
}

tests/Units/Domain/WalletTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
use Bavix\Wallet\Internal\Service\DatabaseServiceInterface;
1313
use Bavix\Wallet\Models\Transaction;
1414
use Bavix\Wallet\Services\RegulatorServiceInterface;
15+
use Bavix\Wallet\Test\Infra\Factories\UserDynamicFactory;
1516
use Bavix\Wallet\Test\Infra\Factories\UserFactory;
1617
use Bavix\Wallet\Test\Infra\Models\User;
18+
use Bavix\Wallet\Test\Infra\Models\UserDynamic;
19+
use Bavix\Wallet\Test\Infra\PackageModels\Wallet;
1720
use Bavix\Wallet\Test\Infra\TestCase;
1821
use Illuminate\Database\Eloquent\Collection;
1922
use RuntimeException;
@@ -280,6 +283,37 @@ public function testRecalculate(): void
280283
self::assertSame(0, $user->balanceInt);
281284
}
282285

286+
public function testDefaultWalletCustomize(): void
287+
{
288+
/** @var User $user */
289+
$user = UserFactory::new()->create();
290+
291+
self::assertFalse($user->wallet->exists);
292+
293+
$user->wallet->meta = [
294+
'internal' => 1,
295+
];
296+
297+
self::assertNotNull($user->deposit(100));
298+
self::assertSame(100, $user->balanceInt);
299+
300+
$wallet = Wallet::query()->find($user->wallet->getKey());
301+
302+
self::assertNotNull($wallet);
303+
self::assertSame(1, $wallet->meta['internal'] ?? 0);
304+
}
305+
306+
public function testgetDynamicDefaultSlug(): void
307+
{
308+
/** @var UserDynamic $user */
309+
$user = UserDynamicFactory::new()->create();
310+
311+
self::assertNotNull($user->deposit(100));
312+
self::assertSame(100, $user->balanceInt);
313+
314+
self::assertSame('default-' . $user->email, $user->wallet->slug);
315+
}
316+
283317
public function testCrash(): void
284318
{
285319
/** @var User $user */

0 commit comments

Comments
 (0)