Skip to content

Commit

Permalink
Fixed user create in filament
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Feb 6, 2025
1 parent 09b168c commit 0a956fd
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 8 deletions.
7 changes: 2 additions & 5 deletions app/Console/Commands/Admin/UserCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ public function handle(): int
}

$user = null;
DB::transaction(function () use (&$user, $name, $email, $password): void {
DB::transaction(function () use (&$user, $name, $email, $password, $verifyEmail): void {
$user = app(UserService::class)->createUser(
$name,
$email,
$password,
'UTC',
Weekday::Monday,
'EUR',
$verifyEmail
);
});
/** @var Organization|null $organization */
Expand All @@ -73,10 +74,6 @@ public function handle(): int
throw new LogicException('User does not have an organization');
}

if ($verifyEmail) {
$user->markEmailAsVerified();
}

$this->info('Created user "'.$name.'" ("'.$email.'")');
$this->line('ID: '.$user->getKey());
$this->line('Name: '.$name);
Expand Down
22 changes: 22 additions & 0 deletions app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Korridor\LaravelModelValidationRules\Rules\UniqueEloquent;
use STS\FilamentImpersonate\Tables\Actions\Impersonate;

class UserResource extends Resource
Expand All @@ -39,6 +40,8 @@ class UserResource extends Resource

public static function form(Form $form): Form
{
/** @var User|null $record */
$record = $form->getRecord();
return $form
->columns(1)
->schema([
Expand All @@ -55,14 +58,25 @@ public static function form(Form $form): Form
Forms\Components\TextInput::make('email')
->label('Email')
->required()
->rules($record?->is_placeholder ? [] : [
UniqueEloquent::make(User::class, 'email')
->ignore($record?->getKey()),
])
->rule([
'email',
])
->maxLength(255),
Forms\Components\Toggle::make('is_placeholder')
->label('Is Placeholder?')
->hiddenOn(['create'])
->disabledOn(['edit']),
Forms\Components\DateTimePicker::make('email_verified_at')
->label('Email Verified At')
->hiddenOn(['create'])
->nullable(),
Forms\Components\Toggle::make('is_email_verified')
->label('Email Verified?')
->visibleOn(['create']),
Forms\Components\Select::make('timezone')
->label('Timezone')
->options(fn (): array => app(TimezoneService::class)->getSelectOptions())
Expand All @@ -74,8 +88,16 @@ public static function form(Form $form): Form
->required(),
TextInput::make('password')
->password()
->label('Password')
->dehydrateStateUsing(fn ($state) => Hash::make($state))
->dehydrated(fn ($state) => filled($state))
->hiddenOn(['create'])
->required(fn (string $context): bool => $context === 'create')
->maxLength(255),
TextInput::make('password_create')
->password()
->label('Password')
->visibleOn(['create'])
->required(fn (string $context): bool => $context === 'create')
->maxLength(255),
Forms\Components\Select::make('currency')
Expand Down
3 changes: 2 additions & 1 deletion app/Filament/Resources/UserResource/Pages/CreateUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ protected function handleRecordCreation(array $data): User
$user = $userService->createUser(
$data['name'],
$data['email'],
$data['password'],
$data['password_create'],
$data['timezone'],
Weekday::from($data['week_start']),
$data['currency'],
(bool) $data['is_email_verified']
);

return $user;
Expand Down
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @property string $id
* @property string $name
* @property string $email
* @property string|null $email_verified_at
* @property Carbon|null $email_verified_at
* @property string|null $password
* @property string|null $two_factor_secret
* @property string $timezone
Expand Down
6 changes: 5 additions & 1 deletion app/Service/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@
use App\Models\ProjectMember;
use App\Models\TimeEntry;
use App\Models\User;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Hash;

class UserService
{
public function createUser(string $name, string $email, string $password, string $timezone, Weekday $weekStart, string $currency): User
public function createUser(string $name, string $email, string $password, string $timezone, Weekday $weekStart, string $currency, bool $verifyEmail = false): User
{
$user = new User;
$user->name = $name;
$user->email = $email;
$user->password = Hash::make($password);
$user->timezone = $timezone;
$user->week_start = $weekStart;
if ($verifyEmail) {
$user->email_verified_at = Carbon::now();
}
$user->save();

$organization = new Organization;
Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/Filament/Resources/UserResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Models\User;
use App\Service\DeletionService;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Hash;
use Livewire\Livewire;
use Mockery\MockInterface;
use PHPUnit\Framework\Attributes\UsesClass;
Expand Down Expand Up @@ -67,6 +68,47 @@ public function test_can_see_view_page_of_user(): void
$response->assertSuccessful();
}

public function test_can_see_create_page_of_user(): void
{
// Act
$response = Livewire::test(UserResource\Pages\CreateUser::class);

// Assert
$response->assertSuccessful();
}

public function test_can_create_user(): void
{
// Arrange
$userFake = User::factory()->make();

// Act
$response = Livewire::test(UserResource\Pages\CreateUser::class)
->fillForm([
'name' => $userFake->name,
'email' => $userFake->email,
'password_create' => 'password',
'timezone' => $userFake->timezone,
'week_start' => $userFake->week_start->value,
'currency' => 'EUR',
])
->call('create')
->assertHasNoFormErrors();

// Assert
$response->assertSuccessful();
$user = User::where('email', $userFake->email)->first();
$this->assertNotNull($user);
$this->assertSame($userFake->name, $user->name);
$this->assertSame($userFake->email, $user->email);
$this->assertSame($userFake->timezone, $user->timezone);
$this->assertSame($userFake->week_start->value, $user->week_start->value);
$organization = $user->ownedTeams()->first();
$this->assertNotNull($organization);
$this->assertSame('EUR', $organization->currency);
$this->assertTrue(Hash::check('password', $user->password));
}

public function test_can_delete_a_user(): void
{
// Arrange
Expand Down

0 comments on commit 0a956fd

Please sign in to comment.