Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need this for Multi-Tenancy (Teams) #819

Closed
zenepay opened this issue Dec 26, 2023 · 7 comments · Fixed by #822
Closed

Need this for Multi-Tenancy (Teams) #819

zenepay opened this issue Dec 26, 2023 · 7 comments · Fixed by #822
Assignees
Labels
enhancement New feature or request

Comments

@zenepay
Copy link

zenepay commented Dec 26, 2023

When implementing with Teams, a user may belong to many teams, a wallet should belong to a team.
Then the wallet table may have team_id as a forieng key. The model Wallet then should BelongsTo a team.

I am trying to inject the team_id value when calling deposit() by lisening to event Wallet creating() in the boot(), unfortunately, deposit() does not dispatch the event creating to wallet. I cannot set team_id before the transaction creating a new wallet.

Please advise, or have this Team feature for new version.

@zenepay zenepay added the enhancement New feature or request label Dec 26, 2023
@zenepay
Copy link
Author

zenepay commented Dec 26, 2023

Currently work around by creating wallet before calling deposit().

        $user = User::find(1)->first();
        if(!$wallet = $user->getWallet($user->current_team_id.'-my-wallet')){
            $wallet = $user->createWallet([
                'name' => 'Team Wallet',
                'slug' => $user->current_team_id.'-my-wallet',
                'team_id' =>$user->current_team_id
            ]);
        }
        $wallet->deposit(10);

And in the config file config/wallet.php, I set default with team_id, so it won't cause error if wallet was not initially created before use.

    /**
     * Base model 'wallet'.
     */
    'wallet' => [
        'table' => 'wallets',
        'model' => Wallet::class,
        'creating' => [],
        'default' => [
            'name' => 'Default Wallet',
            'slug' => '1-default',
            'team_id'=> 1,
            'meta' => [],
        ],
    ],

I hope there is a better way to hook it in the future version.

@rez1dent3
Copy link
Member

Hello. I didn't use this functionality, there was no need. I'll think about a solution in my free time

@rez1dent3
Copy link
Member

@zenepay
UPD.

I was thinking, then why do you need to link a wallet to a specific user? Link wallets to your team. Then you will have code something like this:

use Bavix\Wallet\Traits\HasWallets;
use Bavix\Wallet\Interfaces\Wallet;

class Team extends \Laravel\Jetstream\Team implements Wallet
{
    use HasWallets;
}

Then:

$user-> currentTeam->createWallet(...);
$user-> currentTeam-> hasWallet('my-wallet');

@zenepay
Copy link
Author

zenepay commented Dec 26, 2023

There is no problem if creating wallet manually using createWallet(), since we can assign team_id with some way. But if using default wallet by just call $user->deposit(10) where the wallet was not initially created, it will fail since it requires team_id to save to database table wallets.

So, I wonder that how I could listen to event creating of the wallet when calling via deposit().
I try static::creating(fn($model)) … it won’t work. It seem does not dispatch this event.

I cannot modify wallets.team_id before it saving to db.

@zenepay
Copy link
Author

zenepay commented Dec 27, 2023

Hello. I didn't use this functionality, there was no need. I'll think about a solution in my free time

Thank you. Please,:) it will be high benefit for SaaS projects.

@rez1dent3
Copy link
Member

@zenepay

But if using default wallet by just call $user->deposit(10) where the wallet was not initially created, it will fail since it requires team_id to save to database table wallets.

The default wallet is only needed in very simple projects. In your case, you need to use multi-wallets.

So, I wonder that how I could listen to event creating of the wallet when calling via deposit().
I try static::creating(fn($model)) … it won’t work. It seem does not dispatch this event.
I cannot modify wallets.team_id before it saving to db.

The package has its own events.
https://bavix.github.io/laravel-wallet/#/wallet-created-event

it will be high benefit for SaaS projects.

You can follow the example from unit tests and add additional fields through meta data and save them in separate fields.

Although the example is for transactions, it is also perfect for wallets:

$this->app?->bind(TransactionDtoTransformerInterface::class, TransactionDtoTransformerCustom::class);
}
public function testCustomAttribute(): void
{
/** @var Buyer $buyer */
$buyer = BuyerFactory::new()->create();
self::assertFalse($buyer->relationLoaded('wallet'));
$transaction = $buyer->deposit(1000, [
'bank_method' => 'VietComBank',
]);

@rez1dent3
Copy link
Member

rez1dent3 commented Dec 28, 2023

@zenepay starting from tag 10.1.0 you can used method getDynamicDefaultSlug in model.

final class User extends Model implements Wallet
{
    use HasWallet;

    public function getDynamicDefaultSlug(): string
    {
        return $this->current_team_id.'-my-wallet';
    }
}

Release today, but later.

@rez1dent3 rez1dent3 linked a pull request Dec 28, 2023 that will close this issue
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants