Skip to content

Commit

Permalink
Merge pull request #429 from canyongbs/feature/advapp-137-admissions-…
Browse files Browse the repository at this point in the history
…state-management

[ADVAPP-137]: Implement Table Filters for Status and State Management / Progression Buttons on Detail View for Online Admissions
  • Loading branch information
Orrison authored Jan 10, 2024
2 parents 3a2e39f + 02b1167 commit 764f36a
Show file tree
Hide file tree
Showing 8 changed files with 579 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,27 @@
namespace AdvisingApp\Application\Enums;

use Filament\Support\Contracts\HasLabel;
use Bvtterfly\ModelStateMachine\Attributes\InitialState;
use Bvtterfly\ModelStateMachine\Attributes\AllowTransitionTo;

enum ApplicationSubmissionStateClassification: string implements HasLabel
{
#[InitialState]
#[AllowTransitionTo(self::Review)]
case Received = 'received';

#[AllowTransitionTo(self::Complete)]
#[AllowTransitionTo(self::DocumentsRequired)]
case Review = 'review';

case Complete = 'complete';

#[AllowTransitionTo(self::Admit)]
#[AllowTransitionTo(self::Deny)]
case DocumentsRequired = 'documents_required';

#[AllowTransitionTo(self::Admit)]
#[AllowTransitionTo(self::Deny)]
case Complete = 'complete';

case Admit = 'admit';

case Deny = 'deny';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/*
<COPYRIGHT>
Copyright © 2022-2023, Canyon GBS LLC. All rights reserved.
Advising App™ is licensed under the Elastic License 2.0. For more details,
see https://github.com/canyongbs/advisingapp/blob/main/LICENSE.
Notice:
- You may not provide the software to third parties as a hosted or managed
service, where the service provides users with access to any substantial set of
the features or functionality of the software.
- You may not move, change, disable, or circumvent the license key functionality
in the software, and you may not remove or obscure any functionality in the
software that is protected by the license key.
- You may not alter, remove, or obscure any licensing, copyright, or other notices
of the licensor in the software. Any use of the licensor’s trademarks is subject
to applicable law.
- Canyon GBS LLC respects the intellectual property rights of others and expects the
same in return. Canyon GBS™ and Advising App™ are registered trademarks of
Canyon GBS LLC, and we are committed to enforcing and protecting our trademarks
vigorously.
- The software solution, including services, infrastructure, and code, is offered as a
Software as a Service (SaaS) by Canyon GBS LLC.
- Use of this software implies agreement to the license terms and conditions as stated
in the Elastic License 2.0.
For more information or inquiries please visit our website at
https://www.canyongbs.com or contact us via email at [email protected].
</COPYRIGHT>
*/

namespace AdvisingApp\Application\Filament\Resources\ApplicationResource\Actions;

use Filament\Tables\Actions\Action;
use AdvisingApp\Application\Models\Scopes\ClassifiedAs;
use AdvisingApp\Application\Models\ApplicationSubmission;
use AdvisingApp\Application\Models\ApplicationSubmissionState;
use AdvisingApp\Application\Enums\ApplicationSubmissionStateClassification;

// TODO We either need to introduce support for choosing the state that abides by the allowed classification transitions
// Or we need to restrict creation of these states so that our `first()` logic to grab the desired state is always correct
class ApplicationAdmissionActions
{
public static function get(): array
{
return [
Action::make('mark_as_reviewed')
->label('Mark as Reviewed')
->action(
fn (ApplicationSubmission $record) => $record->getStateMachine(ApplicationSubmissionStateClassification::class, 'state.classification')
->transitionTo(ApplicationSubmissionState::tap(new ClassifiedAs(ApplicationSubmissionStateClassification::Review))->first(), ApplicationSubmissionStateClassification::Review)
)
->cancelParentActions()
->visible(fn (ApplicationSubmission $record) => $record->getStateMachine(ApplicationSubmissionStateClassification::class, 'state.classification')->getStateTransitions()->contains(ApplicationSubmissionStateClassification::Review->value)),
Action::make('mark_as_complete')
->label('Mark as Complete')
->action(
fn (ApplicationSubmission $record) => $record->getStateMachine(ApplicationSubmissionStateClassification::class, 'state.classification')
->transitionTo(ApplicationSubmissionState::tap(new ClassifiedAs(ApplicationSubmissionStateClassification::Complete))->first(), ApplicationSubmissionStateClassification::Complete)
)
->cancelParentActions()
->visible(fn (ApplicationSubmission $record) => $record->getStateMachine(ApplicationSubmissionStateClassification::class, 'state.classification')->getStateTransitions()->contains(ApplicationSubmissionStateClassification::Complete->value)),
Action::make('mark_as_documents_required')
->label('Mark as Documents Required')
->action(
fn (ApplicationSubmission $record) => $record->getStateMachine(ApplicationSubmissionStateClassification::class, 'state.classification')
->transitionTo(ApplicationSubmissionState::tap(new ClassifiedAs(ApplicationSubmissionStateClassification::DocumentsRequired))->first(), ApplicationSubmissionStateClassification::DocumentsRequired)
)
->cancelParentActions()
->visible(fn (ApplicationSubmission $record) => $record->getStateMachine(ApplicationSubmissionStateClassification::class, 'state.classification')->getStateTransitions()->contains(ApplicationSubmissionStateClassification::DocumentsRequired->value)),
Action::make('mark_as_deny')
->label('Mark as Deny')
->action(
fn (ApplicationSubmission $record) => $record->getStateMachine(ApplicationSubmissionStateClassification::class, 'state.classification')
->transitionTo(ApplicationSubmissionState::tap(new ClassifiedAs(ApplicationSubmissionStateClassification::Deny))->first(), ApplicationSubmissionStateClassification::Deny)
)
->cancelParentActions()
->visible(fn (ApplicationSubmission $record) => $record->getStateMachine(ApplicationSubmissionStateClassification::class, 'state.classification')->getStateTransitions()->contains(ApplicationSubmissionStateClassification::Deny->value)),
Action::make('mark_as_admit')
->label('Mark as Admit')
->action(
fn (ApplicationSubmission $record) => $record->getStateMachine(ApplicationSubmissionStateClassification::class, 'state.classification')
->transitionTo(ApplicationSubmissionState::tap(new ClassifiedAs(ApplicationSubmissionStateClassification::Admit))->first(), ApplicationSubmissionStateClassification::Admit)
)
->cancelParentActions()
->visible(fn (ApplicationSubmission $record) => $record->getStateMachine(ApplicationSubmissionStateClassification::class, 'state.classification')->getStateTransitions()->contains(ApplicationSubmissionStateClassification::Admit->value)),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use App\Filament\Columns\IdColumn;
use Filament\Tables\Actions\Action;
use Maatwebsite\Excel\Facades\Excel;
use Filament\Resources\Components\Tab;
use Filament\Tables\Actions\BulkAction;
use Filament\Tables\Actions\ViewAction;
use Filament\Tables\Columns\TextColumn;
Expand All @@ -51,9 +52,12 @@
use Filament\Tables\Actions\DeleteBulkAction;
use App\Filament\Filters\OpenSearch\SelectFilter;
use Filament\Resources\Pages\ManageRelatedRecords;
use AdvisingApp\Application\Models\Scopes\ClassifiedAs;
use AdvisingApp\Application\Models\ApplicationSubmission;
use AdvisingApp\Application\Exports\ApplicationSubmissionExport;
use AdvisingApp\Application\Filament\Resources\ApplicationResource;
use AdvisingApp\Application\Enums\ApplicationSubmissionStateClassification;
use AdvisingApp\Application\Filament\Resources\ApplicationResource\Actions\ApplicationAdmissionActions;

class ManageApplicationSubmissions extends ManageRelatedRecords
{
Expand All @@ -68,6 +72,30 @@ class ManageApplicationSubmissions extends ManageRelatedRecords

protected static ?string $navigationIcon = 'heroicon-o-document-text';

public function getDefaultActiveTab(): string | int | null
{
return 'received';
}

public function getTabs(): array
{
return [
'received' => Tab::make('Received')
->modifyQueryUsing(fn (Builder $query) => $query->whereHas('state', fn (Builder $query) => $query->tap(new ClassifiedAs(ApplicationSubmissionStateClassification::Received)))),
'review' => Tab::make('Review')
->modifyQueryUsing(fn (Builder $query) => $query->whereHas('state', fn (Builder $query) => $query->tap(new ClassifiedAs(ApplicationSubmissionStateClassification::Review)))),
'documents_required' => Tab::make('Documents Required')
->modifyQueryUsing(fn (Builder $query) => $query->whereHas('state', fn (Builder $query) => $query->tap(new ClassifiedAs(ApplicationSubmissionStateClassification::DocumentsRequired)))),
'complete' => Tab::make('Complete')
->modifyQueryUsing(fn (Builder $query) => $query->whereHas('state', fn (Builder $query) => $query->tap(new ClassifiedAs(ApplicationSubmissionStateClassification::Complete)))),
'admit' => Tab::make('Admit')
->modifyQueryUsing(fn (Builder $query) => $query->whereHas('state', fn (Builder $query) => $query->tap(new ClassifiedAs(ApplicationSubmissionStateClassification::Admit)))),
'deny' => Tab::make('Deny')
->modifyQueryUsing(fn (Builder $query) => $query->whereHas('state', fn (Builder $query) => $query->tap(new ClassifiedAs(ApplicationSubmissionStateClassification::Deny)))),
'all' => Tab::make('All'),
];
}

public function table(Table $table): Table
{
return $table
Expand Down Expand Up @@ -138,7 +166,8 @@ public function table(Table $table): Table
])
->modalContent(
fn (ApplicationSubmission $record) => view('application::submission', ['submission' => $record])
),
)
->extraModalFooterActions(ApplicationAdmissionActions::get()),
DeleteAction::make(),
])
->bulkActions([
Expand Down
10 changes: 10 additions & 0 deletions app-modules/application/src/Models/ApplicationSubmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use AdvisingApp\StudentDataModel\Models\Scopes\LicensedToEducatable;
use AdvisingApp\Application\Models\Concerns\HasRelationBasedStateMachine;

/**
* @mixin IdeHelperApplicationSubmission
*/
class ApplicationSubmission extends Submission
{
use HasRelationBasedStateMachine;

public function submissible(): BelongsTo
{
return $this
Expand All @@ -71,6 +74,13 @@ public function state(): BelongsTo
->belongsTo(ApplicationSubmissionState::class, 'state_id');
}

public function getStateMachineFields(): array
{
return [
'state.classification',
];
}

protected static function booted(): void
{
static::addGlobalScope('licensed', function (Builder $builder) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

/*
<COPYRIGHT>
Copyright © 2022-2023, Canyon GBS LLC. All rights reserved.
Advising App™ is licensed under the Elastic License 2.0. For more details,
see https://github.com/canyongbs/advisingapp/blob/main/LICENSE.
Notice:
- You may not provide the software to third parties as a hosted or managed
service, where the service provides users with access to any substantial set of
the features or functionality of the software.
- You may not move, change, disable, or circumvent the license key functionality
in the software, and you may not remove or obscure any functionality in the
software that is protected by the license key.
- You may not alter, remove, or obscure any licensing, copyright, or other notices
of the licensor in the software. Any use of the licensor’s trademarks is subject
to applicable law.
- Canyon GBS LLC respects the intellectual property rights of others and expects the
same in return. Canyon GBS™ and Advising App™ are registered trademarks of
Canyon GBS LLC, and we are committed to enforcing and protecting our trademarks
vigorously.
- The software solution, including services, infrastructure, and code, is offered as a
Software as a Service (SaaS) by Canyon GBS LLC.
- Use of this software implies agreement to the license terms and conditions as stated
in the Elastic License 2.0.
For more information or inquiries please visit our website at
https://www.canyongbs.com or contact us via email at [email protected].
</COPYRIGHT>
*/

namespace AdvisingApp\Application\Models\Concerns;

use Bvtterfly\ModelStateMachine\StateMachine;
use Bvtterfly\ModelStateMachine\HasStateMachine;
use Bvtterfly\ModelStateMachine\Exceptions\FieldWithoutCast;
use Bvtterfly\ModelStateMachine\Exceptions\CouldNotFindStateMachineField;
use AdvisingApp\Application\Models\State\StateMachine as RelationBasedStateMachine;

trait HasRelationBasedStateMachine
{
use TargetsRelationships;
use HasStateMachine {
getStateMachine as parentGetStateMachine;
}

public function getStateMachine(string $enumClass, string $state): RelationBasedStateMachine|StateMachine
{
// We assume that any state utilizing dot notation is targeting a relationship
if ($this->targetingRelationship($state)) {
$this->isStateMachineField($state);

return new RelationBasedStateMachine($this, $enumClass, $state);
}

return $this->parentGetStateMachine($state);
}

private function setInitialState(): void
{
foreach ($this->getStateMachineFields() as $field) {
// We don't necessarily need to set the initial state if we are targeting a relationship
if ($this->targetingRelationship($field) || $this->{$field} !== null) {
continue;
}

$stateMachineConfig = $this->getStateMachineConfig($field);

$initialValue = $stateMachineConfig->initial;

if ($initialValue === null) {
continue;
}

$this->{$field} = $initialValue;
}
}

private function setInitialStates(): void
{
foreach ($this->getStateMachineFields() as $field) {
if ($this->targetingRelationship($field) || $this->{$field} !== null) {
continue;
}

$stateMachineConfig = $this->getStateMachineConfig($field);

$initialValue = $stateMachineConfig->initial;

if ($initialValue === null) {
continue;
}

$this->{$field} = $initialValue;
}
}

private function isStateMachineField(string $field): bool
{
if (! in_array($field, $this->getStateMachineFields())) {
throw CouldNotFindStateMachineField::make($field);
}

if ($this->targetingRelationship($field)) {
$relationPath = explode('.', $field);

$stateField = array_pop($relationPath);

$relatedModel = $this->accessNestedRelations($this, $relationPath);

if (! $relatedModel->hasCast($stateField)) {
throw FieldWithoutCast::make($stateField);
}
} else {
if (! $this->hasCast($field)) {
throw FieldWithoutCast::make($field);
}
}

return true;
}
}
Loading

0 comments on commit 764f36a

Please sign in to comment.