Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement statuses on organisations #169

Merged
merged 4 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/Enum/OrganisationStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ enum OrganisationStatus: string

case active = 'active';
case inactive = 'inactive';
case invited = 'invited';

protected function labelKeyPrefix(): ?string
{
return 'organisation.status';
}
}
17 changes: 5 additions & 12 deletions app/Filament/Resources/OrganisationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Enum\NGOType;
use App\Enum\OrganisationAreaType;
use App\Enum\OrganisationStatus;
use App\Enum\OrganisationType;
use App\Filament\Forms\Components\Location;
use App\Filament\Resources\OrganisationResource\Pages;
Expand Down Expand Up @@ -379,21 +380,13 @@ public static function table(Table $table): Table
->sortable()
->toggleable(),

IconColumn::make('status')
->options([
'heroicon-o-x-circle',
'heroicon-o-x' => 'inactive',
'heroicon-o-check' => 'active',
])
Tables\Columns\BadgeColumn::make('status')
->colors([
'secondary',
'danger' => 'inactive',
'secondary' => 'inactive',
'warning' => 'guest',
'success' => 'active',
])
->label(__('organisation.field.status'))
->searchable()
->sortable()
->toggleable(),
->enum(OrganisationStatus::options()),

TextColumn::make('county.name')
->label(__('organisation.field.hq'))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\OrganisationResource\Actions;

use App\Models\Organisation;
use Filament\Notifications\Notification;
use Filament\Pages\Actions\Action;
use Illuminate\Support\Facades\RateLimiter;

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

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

$this->color('secondary');

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

if (RateLimiter::tooManyAttempts($key, $maxAttempts)) {
return $this->failure();
}

RateLimiter::increment($key, 3600); // 1h

$record->users->first()->sendWelcomeNotification();
$this->success();
});

$this->requiresConfirmation();

$this->label(__('organisation.action.resend_invitation.button'));

$this->modalHeading(__('organisation.action.resend_invitation.heading'));
$this->modalSubheading(__('organisation.action.resend_invitation.subheading'));
$this->modalButton(__('organisation.action.resend_invitation.button'));

$this->successNotificationTitle(__('organisation.action.resend_invitation.success'));

$this->failureNotification(
fn (Notification $notification) => $notification
->danger()
->title(__('organisation.action.resend_invitation.failure_title'))
->body(__('organisation.action.resend_invitation.failure_body'))
);
}

private function getRateLimiterKey(Organisation $organisation): string
{
return 'resend-invitation:' . $organisation->id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Filament\Resources\OrganisationResource;
use App\Filament\Resources\OrganisationResource\Actions\ActivateOrganisationAction;
use App\Filament\Resources\OrganisationResource\Actions\DeactivateOrganisationAction;
use App\Filament\Resources\OrganisationResource\Actions\ResendInvitationAction;
use App\Models\Organisation;
use Filament\Pages\Actions\DeleteAction;
use Filament\Pages\Actions\EditAction;
Expand All @@ -28,6 +29,10 @@ protected function getActions(): array
->visible(fn (Organisation $record) => auth()->user()->isPlatformAdmin() && $record->isInactive())
->record($this->getRecord()),

ResendInvitationAction::make()
->visible(fn (Organisation $record) => auth()->user()->isPlatformAdmin() && $record->isInvited())
->record($this->getRecord()),

DeactivateOrganisationAction::make()
->visible(fn (Organisation $record) => auth()->user()->isPlatformAdmin() && $record->isActive())
->record($this->getRecord()),
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Livewire/Welcome.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public function handle(): ?LoginResponse
data_get($this->form->getState(), 'password')
);

$this->user->activateOrganisation();

Filament::auth()->login($this->user);

return app(LoginResponse::class);
Expand Down
5 changes: 5 additions & 0 deletions app/Models/Organisation.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ public function isInactive(): bool
return $this->status->is(OrganisationStatus::inactive);
}

public function isInvited(): bool
{
return $this->status->is(OrganisationStatus::invited);
}

public function routeNotificationForMail(?Notification $notification = null): string
{
return data_get($this->contact_person, ['email'], $this->email);
Expand Down
10 changes: 10 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Concerns\HasRole;
use App\Concerns\LimitsVisibility;
use App\Concerns\MustSetInitialPassword;
use App\Enum\OrganisationStatus;
use App\Enum\UserRole;
use Filament\Models\Contracts\FilamentUser;
use Filament\Models\Contracts\HasName;
Expand Down Expand Up @@ -83,4 +84,13 @@ public function getFilamentName(): string
{
return "{$this->first_name} {$this->last_name}";
}

public function activateOrganisation(): void
{
$organisation = $this->organisation;
if ($this->isOrgAdmin($organisation) && $this->organisation->status === OrganisationStatus::invited)
{
$organisation->setActive();
}
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"sentry/sentry-laravel": "^4.1"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.9",
"barryvdh/laravel-debugbar": "^3.13",
"barryvdh/laravel-ide-helper": "^2.13",
"fakerphp/faker": "^1.23",
"friendsofphp/php-cs-fixer": "^3.39",
Expand Down
70 changes: 37 additions & 33 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading