Skip to content

Commit

Permalink
Merge pull request #364 from code4romania/287-500-internal-server-err…
Browse files Browse the repository at this point in the history
…or-when-attempting-to-create-an-existing-staff-account-in-a-different-center

Add user from another organization
  • Loading branch information
gheorghelupu17 authored Dec 3, 2024
2 parents 3a5097f + 51f9c2e commit 6551f9a
Show file tree
Hide file tree
Showing 30 changed files with 717 additions and 97 deletions.
2 changes: 1 addition & 1 deletion app/Concerns/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait HasPermissions
{
public function isAdmin(): bool
{
return $this->is_admin;
return (bool) $this->is_admin;
}

public function isNgoAdmin(): bool
Expand Down
12 changes: 11 additions & 1 deletion app/Enums/UserStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
use App\Concerns\Enums\Arrayable;
use App\Concerns\Enums\Comparable;
use App\Concerns\Enums\HasLabel as HasLabelTrait;
use Filament\Support\Contracts\HasColor;
use Filament\Support\Contracts\HasLabel;

enum UserStatus: string implements HasLabel
enum UserStatus: string implements HasLabel, HasColor
{
use Arrayable;
use Comparable;
Expand All @@ -23,4 +24,13 @@ protected function labelKeyPrefix(): ?string
{
return 'enum.user_status';
}

public function getColor(): string|array|null
{
return match ($this) {
self::ACTIVE => 'success',
self::INACTIVE => 'danger',
self::PENDING => 'warning',
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function table(Table $table): Table
TextColumn::make('roles.name')
->label(__('institution.labels.roles')),

TextColumn::make('status')
TextColumn::make('userStatus.status')
->label(__('institution.labels.account_status')),

TextColumn::make('last_login_at')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Filament\Admin\Resources\UserInstitutionResource\Actions;

use App\Models\Organization;
use App\Models\User;
use Filament\Actions\Action;

Expand All @@ -18,7 +19,15 @@ protected function setUp(): void
{
parent::setUp();

$this->visible(fn (User $record) => $record->isInactive());
$this->visible(
fn (User $record) => $record->institution
->organizations
->filter(
fn (Organization $organization) => $record->getStatusInOrganization($organization->id)
->isInactive()
)
->count()
);

$this->label(__('user.actions.activate'));

Expand All @@ -31,9 +40,13 @@ protected function setUp(): void
$this->modalWidth('md');

$this->action(function (User $record) {
$record->activate();
$record->institution
->organizations
->each(
fn (Organization $organization) => $record->getStatusInOrganization($organization->id)
->activate()
);
$this->success();
});

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Filament\Admin\Resources\UserInstitutionResource\Actions;

use App\Models\Organization;
use App\Models\User;
use Filament\Actions\Action;

Expand All @@ -18,7 +19,15 @@ protected function setUp(): void
{
parent::setUp();

$this->visible(fn (User $record) => $record->isActive());
$this->visible(
fn (User $record) => $record->institution
->organizations
->filter(
fn (Organization $organization) => $record->getStatusInOrganization($organization->id)
->isActive()
)
->count()
);

$this->label(__('user.actions.deactivate'));

Expand All @@ -33,7 +42,12 @@ protected function setUp(): void
$this->modalWidth('md');

$this->action(function (User $record) {
$record->deactivate();
$record->institution
->organizations
->each(
fn (Organization $organization) => $record->getStatusInOrganization($organization->id)
->deactivate()
);
$this->success();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Filament\Admin\Resources\UserInstitutionResource\Actions;

use App\Models\Organization;
use App\Models\User;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
Expand All @@ -20,12 +21,22 @@ protected function setUp(): void
{
parent::setUp();

$this->visible(fn (User $record) => $record->isPending());
$this->visible(
fn (User $record) => $record->institution
->organizations
->filter(
fn (Organization $organization) => $record->getStatusInOrganization($organization->id)
->isPending()
)
->count()
);

$this->label(__('user.actions.resend_invitation'));

$this->icon('heroicon-o-envelope-open');

$this->outlined();

$this->action(function (User $record) {
$key = $this->getRateLimiterKey($record);
$maxAttempts = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function infolist(Infolist $infolist): Infolist
->maxWidth('3xl')
->columns()
->schema([
TextEntry::make('status')
TextEntry::make('userStatus.status')
->formatStateUsing(fn ($state) => $state === '-' ? $state : $state->label()),
TextEntry::make('updated_at'),
]),
Expand Down
10 changes: 7 additions & 3 deletions app/Filament/Organizations/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\HtmlString;
use Illuminate\Validation\Rules\Unique;

class UserResource extends Resource
{
Expand Down Expand Up @@ -67,7 +68,7 @@ public static function form(Form $form): Form
public static function table(Table $table): Table
{
return $table
->modifyQueryUsing(fn (Builder $query) => $query->with('rolesInOrganization'))
->modifyQueryUsing(fn (Builder $query) => $query->with(['rolesInOrganization', 'userStatus']))
->columns([
TextColumn::make('first_name')
->sortable()
Expand All @@ -82,7 +83,7 @@ public static function table(Table $table): Table
->sortable()
->label(__('user.labels.roles')),

TextColumn::make('status')
TextColumn::make('userStatus.status')
->sortable()
->label(__('user.labels.account_status')),

Expand Down Expand Up @@ -130,7 +131,10 @@ public static function getSchema(): array
TextInput::make('email')
->label(__('user.labels.email'))
->email()
->unique(ignoreRecord: true)
->unique(
ignoreRecord: true,
modifyRuleUsing: fn (Unique $rule) => $rule->whereIn('id', User::getTenantOrganizationUsers()->keys())
)
->required(),

TextInput::make('phone_number')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ protected function setUp(): void
{
parent::setUp();

$this->visible(fn (User $record) => $record->isActive());
$this->visible(fn (User $record) => $record->userStatus->isActive());

$this->label(__('user.actions.deactivate'));

$this->color('danger');

$this->icon('heroicon-o-user-minus');

$this->outlined();

$this->modalHeading(__('user.action_deactivate_confirm.title'));

$this->modalDescription(__('user.action_deactivate_confirm.description'));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace App\Filament\Organizations\Resources\UserResource\Actions;

use App\Models\User;
use Filament\Actions\Action;

class ReactivateUserAction extends Action
{
public static function getDefaultName(): ?string
{
return 'reactivate';
}

protected function setUp(): void
{
parent::setUp();

$this->visible(fn (User $record) => $record->userStatus->isInactive());

$this->label(__('user.actions.activate'));

$this->icon('heroicon-o-arrow-path');

$this->color('success');

$this->outlined();

$this->action(function (User $record) {
$record->activate();
$this->success();
});

$this->successNotificationTitle(__('user.action_reactivate_confirm.success'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ protected function setUp(): void
{
parent::setUp();

$this->visible(fn (User $record) => $record->isPending());
$this->visible(fn (User $record) => $record->userStatus->isPending());

$this->label(__('user.actions.resend_invitation'));

$this->icon('heroicon-o-envelope-open');

$this->outlined();

$this->action(function (User $record) {
$key = $this->getRateLimiterKey($record);
$maxAttempts = 1;
Expand Down

This file was deleted.

Loading

0 comments on commit 6551f9a

Please sign in to comment.