Skip to content

Commit

Permalink
Change user status and activate from pending
Browse files Browse the repository at this point in the history
  • Loading branch information
alexPopaCode4 committed Dec 2, 2024
1 parent b0539cd commit 31af2c5
Show file tree
Hide file tree
Showing 15 changed files with 322 additions and 19 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ protected function setUp(): void

$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 @@ -26,6 +26,8 @@ protected function setUp(): void

$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 @@ -8,6 +8,9 @@
use App\Enums\CasePermission;
use App\Enums\Ternary;
use App\Filament\Organizations\Resources\UserResource;
use App\Filament\Organizations\Resources\UserResource\Actions\DeactivateUserAction;
use App\Filament\Organizations\Resources\UserResource\Actions\ReactivateUserAction;
use App\Filament\Organizations\Resources\UserResource\Actions\ResendInvitationAction;
use App\Infolists\Components\SectionHeader;
use App\Models\User;
use Filament\Infolists\Components\Actions\Action;
Expand Down Expand Up @@ -107,11 +110,13 @@ public function infolist(Infolist $infolist): Infolist
protected function getHeaderActions(): array
{
return [
UserResource\Actions\DeactivateUserAction::make(),
DeactivateUserAction::make(),

// UserResource\Actions\ResetPassword::make('reset-password'),

UserResource\Actions\ResendInvitationAction::make(),
ResendInvitationAction::make(),

ReactivateUserAction::make(),
];
}

Expand Down
4 changes: 4 additions & 0 deletions app/Http/Middleware/UpdateDefaultTenant.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public function handle(Request $request, Closure $next): Response
if ($user->userStatus->status === UserStatus::PENDING) {
$user->userStatus->activate();
}

if ($tenant->institution->isPending()) {
$tenant->institution->activate();
}
}

return $next($request);
Expand Down
28 changes: 25 additions & 3 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Concerns\HasPermissions;
use App\Concerns\HasUlid;
use App\Concerns\MustSetInitialPassword;
use App\Models\Scopes\BelongsToCurrentTenant;
use App\Notifications\Organizations\WelcomeNotificationInAnotherTenant;
use Filament\Facades\Filament;
use Filament\Models\Contracts\FilamentUser;
Expand Down Expand Up @@ -103,6 +104,11 @@ protected static function booted()
?->pluck('id')
->diff($model->organizations?->pluck('id'))
);

$model->load('organizations')
->organizations->each(
fn (Organization $organization) => $model->initializeStatus($organization->id)
);
}
});
}
Expand Down Expand Up @@ -272,17 +278,33 @@ public function userStatus(): HasOne
return $this->hasOne(UserStatus::class);
}

public function initializeStatus(): void
public function initializeStatus(?int $organizationID = null): void
{
if (! Filament::getTenant()?->id) {
$organizationID = Filament::getTenant()?->id ?? $organizationID;

if (! $organizationID) {
return;
}

if ($this->getStatusInOrganization($organizationID)) {
return;
}

$this->userStatus()->create([
'user_id' => $this->id,
'organization_id' => Filament::getTenant()->id,
'organization_id' => $organizationID,
]);
}

public function getStatusInOrganization(int $organizationID): ?UserStatus
{
return UserStatus::query()
->withoutGlobalScopes([BelongsToCurrentTenant::class])
->where('user_id', $this->id)
->where('organization_id', $organizationID)
->first();
}

public function sendWelcomeNotificationInAnotherTenant(): void
{
$this->notify(new WelcomeNotificationInAnotherTenant);
Expand Down
8 changes: 4 additions & 4 deletions lang/ro/enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@

'user_status' => [
'active' => 'Activ',
'inactive' => 'Dezactivat',
'inactive' => 'Suspendat',
'pending' => 'În așteptare',
],

Expand Down Expand Up @@ -350,8 +350,8 @@

'beneficiary_segmentation_by_age' => [
'minor' => '0-17 ani (minor)',
'major' => '18 ani + (major)'
],
'major' => '18 ani + (major)',
],

'activity_description' => [
'created' => 'Creat',
Expand Down Expand Up @@ -455,7 +455,7 @@
'11' => 'Nov',
'12' => 'Dec',
],

'diseases' => [
'denies_diseases' => 'Neagă boli',
'chronic_diseases' => 'Boli cronice',
Expand Down
7 changes: 6 additions & 1 deletion lang/ro/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@
'action_deactivate_confirm' => [
'title' => 'Deactivează cont',
'success' => 'Cont dezactivat cu succes',
'description' => 'Odată dezactivat contul, utilizatorul nu va mai avea acces în platformă. Toate datele asociate contului vor rămâne în baza de date. Pentru a oferi din nou acces utilizatorului, va trebui să reactivați contul din profilul acestuia.'
'description' => 'Odată dezactivat contul, utilizatorul nu va mai avea acces în platformă. Toate datele asociate contului vor rămâne în baza de date. Pentru a oferi din nou acces utilizatorului, va trebui să reactivați contul din profilul acestuia.',
],

'action_reactivate_confirm' => [
'title' => 'Deactivează cont',
'success' => 'Cont reactivat cu succes',
],

'status' => [
Expand Down
Loading

0 comments on commit 31af2c5

Please sign in to comment.