RickDBCN / filament-email

Log emails in your Filament project
https://filament-email-demo.marcogermani.it/
MIT License
77 stars 23 forks source link

[Bug]: Team id is not getting stored on production #64

Closed nilede-swapneal closed 2 months ago

nilede-swapneal commented 3 months ago

What happened?

I can't see email logs in tenant, when I checked DB team id is null.

How to reproduce the bug

Package Version

1.4.5

PHP Version

8.2

Laravel Version

10.48.9

Which operating systems does with happen with?

Linux

Notes

No response

marcogermani87 commented 3 months ago

Hi @nilede-swapneal, take a look to our demo https://filament-email-demo.marcogermani.it/ that's support multi-tenant and works well.

You can check the tenant configuration to https://github.com/marcogermani87/filament-email-demo

nilede-swapneal commented 3 months ago

@marcogermani87 I used Laravel Forge to setup a production site. Do I need to configure in order to work this package?

marcogermani87 commented 3 months ago

@marcogermani87 I used Laravel Forge to setup a production site. Do I need to configure in order to work this package?

You can provide your AdminPanelProvider configuration?

nilede-swapneal commented 3 months ago

@marcogermani87 here it is:

<?php

namespace App\Providers\Filament;

use App\Filament\App\Pages\Dashboard;
use App\Filament\App\Pages\EditProfile;
use App\Filament\App\Pages\Tenancy\EditTeamProfile;
use App\Filament\App\Pages\Tenancy\RegisterTeam;
use App\Http\Middleware\AllowRegisterTeam;
use App\Http\Middleware\UpdateLastVisit;
use App\Models\Team;
use EightyNine\Reports\ReportsPlugin;
use Filament\Http\Middleware\Authenticate;
use Filament\Http\Middleware\DisableBladeIconComponents;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use Filament\Navigation\MenuItem;
use Filament\Navigation\NavigationGroup;
use Filament\Pages;
use Filament\Panel;
use Filament\PanelProvider;
use Filament\Support\Colors\Color;
use Filament\Support\Facades\FilamentView;
use Filament\View\PanelsRenderHook;
use Filament\Widgets;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Session\Middleware\AuthenticateSession;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;

class AppPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->id('app')
            ->path('/')
            ->domain(app_domain())
            ->login()
            ->colors([
                'primary' => Color::hex('#004099'),
                'danger' => Color::Red,
                'gray' => Color::Zinc,
                'info' => Color::Blue,
                'success' => Color::Green,
                'warning' => Color::Amber,
            ])
            ->discoverResources(in: app_path('Filament/App/Resources'), for: 'App\\Filament\\App\\Resources')
            ->discoverPages(in: app_path('Filament/App/Pages'), for: 'App\\Filament\\App\\Pages')
            ->pages([
                // Pages\Dashboard::class,
                Dashboard::class,
            ])
            ->discoverWidgets(in: app_path('Filament/App/Widgets'), for: 'App\\Filament\\App\\Widgets')
            ->widgets([
                // Widgets\AccountWidget::class,
                // Widgets\FilamentInfoWidget::class,
            ])
            ->userMenuItems(self::menuItems())
            ->middleware([
                EncryptCookies::class,
                AddQueuedCookiesToResponse::class,
                StartSession::class,
                AuthenticateSession::class,
                ShareErrorsFromSession::class,
                VerifyCsrfToken::class,
                SubstituteBindings::class,
                DisableBladeIconComponents::class,
                DispatchServingFilamentEvent::class,
                UpdateLastVisit::class,
            ])
            ->authMiddleware([
                Authenticate::class,
                AllowRegisterTeam::class,
            ])
            ->navigationGroups([
                NavigationGroup::make()
                    ->label('Sales')
                    ->icon('heroicon-o-shopping-cart'),
                NavigationGroup::make()
                    ->label('Purchases')
                    ->icon('heroicon-o-credit-card'),
                NavigationGroup::make()
                    ->label('Banking')
                    ->icon('heroicon-o-banknotes'),
                NavigationGroup::make()
                    ->label('Settings')
                    ->icon('heroicon-o-cog-6-tooth'),
                NavigationGroup::make()
                    ->label('Reports')
                    ->icon('heroicon-o-clipboard-document-list'),
            ])
            ->tenant(Team::class, 'uuid', 'team')
            ->tenantRegistration(RegisterTeam::class)
            ->tenantProfile(EditTeamProfile::class)
            ->tenantMenuItems([
                'register' => MenuItem::make()->label('Register Company')->hidden(),
            ])
            ->plugins([
                \RickDBCN\FilamentEmail\FilamentEmail::make(),
                ReportsPlugin::make(),
            ])
            ->font('Sora')
            ->brandLogo('/logo.svg')
            ->darkModeBrandLogo('/logo-dark.svg')
            ->brandLogoHeight('64px')
            ->viteTheme('resources/css/filament/app/theme.css');
    }

    public static function menuItems(): array
    {
        return [
            'profile' => MenuItem::make()->url(function () {
                $route = '';
                try {
                    $route = EditProfile::getUrl();
                } catch (\Exception $e) {
                }

                return $route;
            })->icon(function () {
                return auth()?->user()->profile_url ?? 'heroicon-o-user-circle';
            }),
        ];
    }

    public function boot()
    {
        FilamentView::registerRenderHook(
            PanelsRenderHook::HEAD_START,
            fn (): string => view('pwa.header')
        );
    }
}
marcogermani87 commented 3 months ago

Hi @nilede-swapneal, i think the problem is in the Team class.

You can to use this standard code for a try?

AdminPanelProvider.php:

->tenant(Team::class)

app/Models/Team.php:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Team extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
    ];

    public function members(): BelongsToMany
    {
        return $this->belongsToMany(User::class);
    }
}

Also you can share you Team class code?

nilede-swapneal commented 3 months ago

@marcogermani87 I don't think its a team class issue, Also, I checked the team class; it's the same as yours with additional relations.

marcogermani87 commented 3 months ago

@nilede-swapneal and what about app/Models/User.php?

You have implemented HasTentants and all this methods?

class User extends Authenticatable implements FilamentUser, HasTenants
{

...

public function teams(): BelongsToMany
{
    return $this->belongsToMany(Team::class);
}

public function getTenants(Panel $panel): Collection
{
    return $this->teams;
}

public function canAccessTenant(Model $tenant): bool
{
    return $this->teams()->whereKey($tenant)->exists();
}

I've tried your configurations on my demo site and all work correctly!

If there's a misconfiguration the problem is in src/vendor/rickdbcn/filament-email/src/Listeners/FilamentEmailLogger.php:

$model::create([
    'team_id' => Filament::getTenant()?->id ?? null,
...

Filament::getTenant()?->id for some reason is null, there's somenthing wrong in some place...

RickDBCN commented 2 months ago

Closing due to inactivity. Feel free to reopen.