Skip to content

Commit

Permalink
feat: created observer for customer resource
Browse files Browse the repository at this point in the history
  • Loading branch information
icarojobs committed Jan 18, 2024
1 parent 906fe31 commit 2fdd0ed
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 159 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# freezer-control
Sistema de gestão de freezers

---
### LINK DO ANTEPROJETO
[https://www.youtube.com/watch?v=-Jf9hgt-Fj4&list=PLbjKo3xK3gjcOz9Ocn3H6aTtTRBypCAaA&index=2](https://www.youtube.com/watch?v=-Jf9hgt-Fj4&list=PLbjKo3xK3gjcOz9Ocn3H6aTtTRBypCAaA&index=2)

---
### PENDENCIAS
1. Adicionar campos CPF, Celular, Data de Nascimento na tela de cadastro `http://laravel.test/app/register`
2. Ao cadastrar, verificar pela data de nascimento informada, se o caboclo é maior de 18 anos.
43 changes: 29 additions & 14 deletions app/Filament/Resources/CustomerResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,51 @@
use App\Filament\Resources\CustomerResource\RelationManagers;
use App\Models\Customer;
use Filament\Forms;
use Filament\Forms\Components\Select;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class CustomerResource extends Resource
{
protected static ?string $model = Customer::class;

protected static ?string $modelLabel = 'Cliente';

protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('user_id')
->required()
->numeric(),
// Select::make('user_id')
// ->label('Usuário')
// ->searchable()
// ->relationship('user', 'name')
// ->required(),

Forms\Components\TextInput::make('name')
->label('Nome Completo')
->required()
->maxLength(255),

Forms\Components\TextInput::make('document')
->label('Documento')
->required()
->maxLength(255),
Forms\Components\DatePicker::make('birthdate')
->required(),

Forms\Components\TextInput::make('email')
->email()
->required()
->maxLength(255),

Forms\Components\DatePicker::make('birthdate')
->label('Data Nascimento')
->required(),

Forms\Components\TextInput::make('mobile')
->label('Celular')
->required()
->maxLength(255),
]);
Expand All @@ -48,24 +60,27 @@ public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('user_id')
->numeric()
Tables\Columns\TextColumn::make('user.name')
->label('Usuário')
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('name')
->searchable(),

Tables\Columns\TextColumn::make('document')
->label('Documento')
->searchable(),
Tables\Columns\TextColumn::make('birthdate')
->date()
->sortable(),

Tables\Columns\TextColumn::make('email')
->searchable(),

Tables\Columns\TextColumn::make('mobile')
->label('Celular')
->searchable(),

Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),

Tables\Columns\TextColumn::make('updated_at')
->dateTime()
->sortable()
Expand Down
2 changes: 2 additions & 0 deletions app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class UserResource extends Resource
{
protected static ?string $model = User::class;

protected static ?string $modelLabel = 'Usuário';

protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

public static function form(Form $form): Form
Expand Down
5 changes: 4 additions & 1 deletion app/Http/Middleware/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Middleware;

use Filament\Facades\Filament;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Http\Request;

Expand All @@ -12,6 +13,8 @@ class Authenticate extends Middleware
*/
protected function redirectTo(Request $request): ?string
{
return $request->expectsJson() ? null : route('login');
$panelID = Filament::getCurrentPanel()?->getId();

return $request->expectsJson() ? null : route("filament.{$panelID}.auth.login");
}
}
49 changes: 49 additions & 0 deletions app/Mail/NewCustomerMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace App\Mail;

use App\Models\Customer;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class NewCustomerMail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;

public function __construct(
public readonly Customer $customer,
public readonly string $secret,
) {
}

public function envelope(): Envelope
{
return new Envelope(
from: new Address(config('mail.from.address'), config('app.name')),
subject: 'Seu acesso ao Freezer Control',
);
}

public function content(): Content
{
return new Content(
view: 'emails.new-customer',
with: [
'customer' => $this->customer,
'secret' => $this->secret,
]
);
}

public function attachments(): array
{
return [];
}
}
5 changes: 3 additions & 2 deletions app/Models/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;

class Customer extends Model
Expand All @@ -14,8 +15,8 @@ class Customer extends Model
'birthdate' => 'date',
];

public function user(): HasOne
public function user(): BelongsTo
{
return $this->hasOne(User::class);
return $this->belongsTo(User::class);
}
}
4 changes: 2 additions & 2 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public function canAccessPanel(Panel $panel): bool
return false;
}

public function customer(): BelongsTo
public function customer(): HasOne
{
return $this->belongsTo(Customer::class);
return $this->hasOne(Customer::class);
}
}
32 changes: 32 additions & 0 deletions app/Observers/CustomerObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Observers;

use App\Enums\PanelTypeEnum;
use App\Mail\NewCustomerMail;
use App\Models\Customer;
use App\Models\User;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;

class CustomerObserver
{
public function created(Customer $customer): void
{
$password = Str::random(8);

$user = User::create([
'name' => $customer->name,
'email' => $customer->email,
'panel' => PanelTypeEnum::APP,
'password' => bcrypt($password),
]);

$customer->user_id = $user->id;
$customer->saveQuietly();

Mail::to($customer->email)->send(new NewCustomerMail($customer, $password));
}
}
6 changes: 6 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Providers;

use App\Models\Customer;
use App\Observers\CustomerObserver;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
Expand All @@ -20,6 +22,10 @@ class EventServiceProvider extends ServiceProvider
],
];

protected $observers = [
Customer::class => [CustomerObserver::class],
];

/**
* Register any events for your application.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function up(): void

$table->foreignId('user_id')
->index()
->nullable()
->constrained()
->cascadeOnDelete();

Expand Down
18 changes: 18 additions & 0 deletions resources/views/emails/new-customer.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Seu acesso ao {{ config('app.name') }}</title>
</head>
<body>
<div>
<h1>Dados de Acesso</h1>
<p><strong>Link:</strong> {{ route("filament.app.auth.login") }}</p>
<p><strong>Usuário:</strong> {{ $customer->email }}</p>
<p><strong>Senha:</strong> {{ $secret }}</p>
</div>
</body>
</html>
Loading

0 comments on commit 2fdd0ed

Please sign in to comment.