From 492e595338e5f2ebd1e2ba4138f8f97dd183859f Mon Sep 17 00:00:00 2001 From: Dede Nugroho <49790011+thisnugroho@users.noreply.github.com> Date: Wed, 13 Dec 2023 10:06:58 +0800 Subject: [PATCH 01/11] add local scopes. --- app/Models/Submission.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/Models/Submission.php b/app/Models/Submission.php index 04785847b..72297eb40 100644 --- a/app/Models/Submission.php +++ b/app/Models/Submission.php @@ -90,7 +90,7 @@ protected static function booted(): void ]); //If current user does not exists in participant - if (! $userAsParticipant = auth()->user()->asParticipant()) { + if (!$userAsParticipant = auth()->user()->asParticipant()) { $userAsParticipant = CreateParticipantFromUserAction::run(auth()->user()); } @@ -137,6 +137,16 @@ public function contributors() return $this->hasMany(SubmissionContributor::class); } + public function scopeinAnyStatus(Builder $query, array $statuses) + { + return $query->whereIn('status', $statuses); + } + + public function scopePublished(Builder $query) + { + return $this->status(SubmissionStatus::Published); + } + public function scopeStage(Builder $query, SubmissionStage $stage) { return $query->where('stage', $stage); From 0cf0db6f9a43e1d06b7af51cb83b31fdb46d3bfb Mon Sep 17 00:00:00 2001 From: Dede Nugroho <49790011+thisnugroho@users.noreply.github.com> Date: Wed, 13 Dec 2023 10:08:36 +0800 Subject: [PATCH 02/11] wip --- .../Pages/ManageSubmissions.php | 257 +++++++++++++----- 1 file changed, 194 insertions(+), 63 deletions(-) diff --git a/app/Panel/Resources/SubmissionResource/Pages/ManageSubmissions.php b/app/Panel/Resources/SubmissionResource/Pages/ManageSubmissions.php index ae80ed67d..1f96c2f64 100644 --- a/app/Panel/Resources/SubmissionResource/Pages/ManageSubmissions.php +++ b/app/Panel/Resources/SubmissionResource/Pages/ManageSubmissions.php @@ -4,11 +4,12 @@ use App\Models\Enums\SubmissionStatus; use App\Models\Enums\UserRole; +use App\Models\Submission; use App\Panel\Livewire\Workflows\Classes\StageManager; use App\Panel\Pages\Settings\Workflow; use App\Panel\Resources\SubmissionResource; use Filament\Actions\Action; -use Filament\Resources\Pages\ListRecords\Tab; +use Filament\Resources\Components\Tab; use Filament\Resources\Pages\ManageRecords; use Illuminate\Database\Eloquent\Builder; @@ -29,7 +30,7 @@ protected function getHeaderActions(): array ->button() ->authorize('Submission:create') ->disabled( - fn (): bool => ! StageManager::callForAbstract()->isStageOpen() + fn (): bool => !StageManager::callForAbstract()->isStageOpen() ) ->url(static::$resource::getUrl('create')) ->icon('heroicon-o-plus') @@ -45,80 +46,210 @@ protected function getHeaderActions(): array public function getTabs(): array { + $currentUser = auth()->user(); return [ 'My Queue' => Tab::make('My Queue') - ->when(auth()->user()->hasRole(UserRole::Author->value), function (Tab $tab) { - return $tab->modifyQueryUsing(function (Builder $query) { - return $query->where('user_id', auth()->id()); - }); - }) - ->when(auth()->user()->hasRole(UserRole::Reviewer->value), function (Tab $tab) { - return $tab->modifyQueryUsing(function (Builder $query) { - return $query->whereHas( - 'reviews', - fn (Builder $query) => $query->where('user_id', auth()->id()) + ->when( + $currentUser->hasRole(UserRole::Author->value), + function (Tab $tab) use ($currentUser) { + return $tab->modifyQueryUsing( + fn (Builder $query) => $query->where('user_id', $currentUser->getKey()) + ->inAnyStatus([ + SubmissionStatus::Incomplete, + SubmissionStatus::Queued, + SubmissionStatus::OnReview, + SubmissionStatus::Editing, + ]) ); - }); - }) - ->when(auth()->user()->hasRole(UserRole::Editor->value), function (Tab $tab) { - return $tab->modifyQueryUsing(function (Builder $query) { - return $query->whereHas( - 'participants', - fn (Builder $query) => $query->where('user_id', auth()->id()) + } + ) + ->when( + $currentUser->hasRole(UserRole::Reviewer->value), + function (Tab $tab) use ($currentUser) { + return $tab->modifyQueryUsing( + fn (Builder $query) => $query->whereHas( + 'reviews', + fn (Builder $query) => $query->where('user_id', $currentUser->getKey()) + ) + ->inAnyStatus([ + SubmissionStatus::Incomplete, + SubmissionStatus::Queued, + SubmissionStatus::OnReview, + SubmissionStatus::Editing, + ]) ); - }); - }), + } + ) + ->when( + $currentUser->hasRole(UserRole::Editor->value), + function (Tab $tab) use ($currentUser) { + return $tab->modifyQueryUsing( + fn (Builder $query) => $query->whereHas( + 'participants', + fn (Builder $query) => $query->where('user_id', $currentUser->getKey()) + ) + ->inAnyStatus([ + SubmissionStatus::Incomplete, + SubmissionStatus::Queued, + SubmissionStatus::OnReview, + SubmissionStatus::Editing, + ]) + ); + } + ) + ->when( + $currentUser->hasAnyRole([ + UserRole::Admin->value, + UserRole::ConferenceManager->value + ]), + function (Tab $tab) { + return $tab->modifyQueryUsing( + fn (Builder $query) => $query->inAnyStatus([ + SubmissionStatus::Incomplete, + SubmissionStatus::Queued, + SubmissionStatus::OnReview, + SubmissionStatus::Editing, + ]) + ); + } + ), 'Active' => Tab::make('Active') ->when( - auth()->user()->hasRole(UserRole::Author->value), + $currentUser->hasRole(UserRole::Author->value), function (Tab $tab) { - return $tab->modifyQueryUsing(function (Builder $query) { - return $query->where('user_id', auth()->id()); - }); + return $tab->modifyQueryUsing( + fn (Builder $query) => $query->where('user_id', auth()->id()) + ->inAnyStatus([ + SubmissionStatus::Queued, + SubmissionStatus::OnReview, + SubmissionStatus::Editing, + ]) + ); } ) - ->when(auth()->user()->hasRole(UserRole::Reviewer->value), function (Tab $tab) { - return $tab->modifyQueryUsing(function (Builder $query) { - return $query->whereHas( - 'reviews', - fn (Builder $query) => $query->where('user_id', auth()->id()) + ->when( + $currentUser->hasRole(UserRole::Reviewer->value), + function (Tab $tab) use ($currentUser) { + return $tab->modifyQueryUsing( + fn (Builder $query) => $query->whereHas( + 'reviews', + fn (Builder $query) => $query->where('user_id', $currentUser->getKey()) + ->inAnyStatus([ + SubmissionStatus::Queued, + SubmissionStatus::OnReview, + SubmissionStatus::Editing, + ]) + ) ); - }); - }), + } + ) + ->when( + $currentUser->hasRole(UserRole::Editor->value), + function (Tab $tab) use ($currentUser) { + return $tab->modifyQueryUsing( + fn (Builder $query) => $query->whereHas( + 'participants', + fn (Builder $query) => $query->where('user_id', $currentUser->getKey()) + ) + ->inAnyStatus([ + SubmissionStatus::Queued, + SubmissionStatus::OnReview, + SubmissionStatus::Editing, + ]) + ); + } + ) + ->when( + $currentUser->hasAnyRole([ + UserRole::Admin->value, + UserRole::ConferenceManager->value + ]), + function (Tab $tab) { + return $tab->modifyQueryUsing( + fn (Builder $query) => $query + ->inAnyStatus([ + SubmissionStatus::Queued, + SubmissionStatus::OnReview, + SubmissionStatus::Editing, + ]) + ); + } + ), 'Published' => Tab::make('Published') - ->when(auth()->user()->hasRole(UserRole::Author->value), function (Tab $tab) { - return $tab->modifyQueryUsing(function (Builder $query) { - return $query->where('user_id', auth()->id()); - }); - }) - ->when(auth()->user()->hasRole(UserRole::Reviewer->value), function (Tab $tab) { - return $tab->modifyQueryUsing(function (Builder $query) { - return $query->whereHas( - 'reviews', - fn (Builder $query) => $query->where('user_id', auth()->id()) + ->when( + $currentUser->hasRole(UserRole::Author->value), + function (Tab $tab) use ($currentUser) { + return $tab->modifyQueryUsing( + fn (Builder $query) => $query + ->where('user_id', $currentUser->getKey()) + ->published() ); - }); - }) - ->modifyQueryUsing(function (Builder $query) { - return $query->where('status', SubmissionStatus::Published); - }), - 'Declined' => Tab::make('Declined') - ->when(auth()->user()->hasRole(UserRole::Author->value), function (Tab $tab) { - return $tab->modifyQueryUsing(function (Builder $query) { - return $query->where('user_id', auth()->id()); - }); - }) - ->when(auth()->user()->hasRole(UserRole::Reviewer->value), function (Tab $tab) { - return $tab->modifyQueryUsing(function (Builder $query) { - return $query->whereHas( - 'reviews', - fn (Builder $query) => $query->where('user_id', auth()->id()) + } + ) + ->when( + $currentUser->hasRole(UserRole::Reviewer->value), + function (Tab $tab) use ($currentUser) { + return $tab->modifyQueryUsing( + fn (Builder $query) => $query->whereHas( + 'reviews', + fn (Builder $query) => $query->where('user_id', $currentUser->getKey()) + )->published() ); - }); - }) - ->modifyQueryUsing(function (Builder $query) { - return $query->where('status', SubmissionStatus::Declined); - }), + } + ) + ->when( + $currentUser->hasRole([ + UserRole::Admin->value, + UserRole::ConferenceManager->value + ]), + function (Tab $tab) { + return $tab->modifyQueryUsing( + fn (Builder $query) => $query->published() + ); + } + ), + 'Archived' => Tab::make('Archived') + ->when( + $currentUser->hasRole(UserRole::Author->value), + function (Tab $tab) use ($currentUser) { + return $tab->modifyQueryUsing( + fn (Builder $query) => $query->where('user_id', $currentUser->getKey()) + ->inAnyStatus([ + SubmissionStatus::Declined, + SubmissionStatus::Withdrawn, + ]) + ); + } + ) + ->when( + $currentUser->hasRole(UserRole::Reviewer->value), + function (Tab $tab) { + return $tab->modifyQueryUsing( + fn (Builder $query) => $query->whereHas( + 'reviews', + fn (Builder $query) => $query->where('user_id', auth()->id()) + ->inAnyStatus([ + SubmissionStatus::Declined, + SubmissionStatus::Withdrawn, + ]) + ) + ); + } + ) + ->when( + $currentUser->hasAnyRole([ + UserRole::Admin->value, + UserRole::ConferenceManager->value + ]), + function (Tab $tab) { + return $tab->modifyQueryUsing( + fn (Builder $query) => $query->inAnyStatus([ + SubmissionStatus::Declined, + SubmissionStatus::Withdrawn, + ]) + ); + } + ) ]; } } From 2d29248e663f54d9c082058fd4d430e5634ebb65 Mon Sep 17 00:00:00 2001 From: Dede Nugroho <49790011+thisnugroho@users.noreply.github.com> Date: Wed, 13 Dec 2023 11:06:14 +0800 Subject: [PATCH 03/11] Fix: Searching participant --- .../Components/ParticipantList.php | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/app/Panel/Livewire/Submissions/Components/ParticipantList.php b/app/Panel/Livewire/Submissions/Components/ParticipantList.php index b499f4007..09801f4c0 100644 --- a/app/Panel/Livewire/Submissions/Components/ParticipantList.php +++ b/app/Panel/Livewire/Submissions/Components/ParticipantList.php @@ -138,7 +138,24 @@ function (Model $record) { ->toArray() ) ->searchable() - ->preload() + ->getSearchResultsUsing(function (Get $get, string $search) { + return User::with('roles') + ->whereHas( + 'roles', + fn (Builder $query) => $query->whereId($get('role_id')) + ) + ->whereNotIn('id', $this->submission->participants->pluck('user_id')) + ->where('given_name', 'like', "%{$search}%") + ->orWhere('family_name', 'like', "%{$search}%") + ->orWhere('email', 'like', "%{$search}%") + ->get() + ->mapWithKeys( + fn (User $user) => [ + $user->getKey() => static::renderSelectParticipant($user), + ] + ) + ->toArray(); + }) ->columnSpan(2), Fieldset::make() ->label('Notification') @@ -163,7 +180,7 @@ function (Model $record) { 'role_id' => $data['role_id'], ]); - if (! $data['no-notification']) { + if (!$data['no-notification']) { try { Mail::to($submissionParticipant->user->email) ->send( @@ -245,7 +262,7 @@ function (Message $message) use ($data) { ->color('primary') ->redirectTo('panel') ->action(function (SubmissionParticipant $record, Impersonate $action) { - if (! $action->impersonate($record->user)) { + if (!$action->impersonate($record->user)) { $action->failureNotificationTitle("User can't be impersonated"); $action->failure(); } From 3b120defcb8d76e67412d135ce19b916c255bf17 Mon Sep 17 00:00:00 2001 From: Dede Nugroho <49790011+thisnugroho@users.noreply.github.com> Date: Wed, 13 Dec 2023 11:06:24 +0800 Subject: [PATCH 04/11] add: local scope --- app/Models/SubmissionParticipant.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/Models/SubmissionParticipant.php b/app/Models/SubmissionParticipant.php index 734fcc213..02493aabb 100644 --- a/app/Models/SubmissionParticipant.php +++ b/app/Models/SubmissionParticipant.php @@ -2,6 +2,8 @@ namespace App\Models; +use App\Models\Enums\UserRole; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -27,6 +29,12 @@ public function role() return $this->belongsTo(Role::class); } + public function scopeEditor(Builder $builder) + { + $roleEditor = Role::where('name', UserRole::Editor->value)->first(); + return $builder->where('role_id', $roleEditor->getKey()); + } + public function submission() { return $this->belongsTo(Submission::class); From 5fe6d5eea450d33dc9ae005209661ed32a3c4123 Mon Sep 17 00:00:00 2001 From: Dede Nugroho <49790011+thisnugroho@users.noreply.github.com> Date: Wed, 13 Dec 2023 11:06:55 +0800 Subject: [PATCH 05/11] fix: policy --- app/Policies/SubmissionPolicy.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/Policies/SubmissionPolicy.php b/app/Policies/SubmissionPolicy.php index 04ae12b7e..3238a8dd5 100644 --- a/app/Policies/SubmissionPolicy.php +++ b/app/Policies/SubmissionPolicy.php @@ -21,8 +21,12 @@ public function viewAny(User $user) return $user->can('Submission:viewAny'); } - public function view(User $user) + public function view(User $user, Submission $submission) { + if ($submission->participants()->where('user_id', $user->getKey())->exists()) { + return true; + } + if ($user->can('Submission:view')) { return true; } @@ -42,7 +46,7 @@ public function update(User $user, Submission $submission) public function delete(User $user, Submission $submission) { // Only submission with status: withdrawn or declined can be deleted. - if (! in_array($submission->status, [SubmissionStatus::Declined, SubmissionStatus::Withdrawn])) { + if (!in_array($submission->status, [SubmissionStatus::Declined, SubmissionStatus::Withdrawn])) { return false; } @@ -266,7 +270,7 @@ public function withdraw(User $user, Submission $submission) } // Editors cannot withdraw submissions; they must wait for the author to request it.. - if (! filled($submission->withdrawn_reason)) { + if (!filled($submission->withdrawn_reason)) { return false; } From 5df0baa0be1c7641d82b9aa96ddc6051e81db11a Mon Sep 17 00:00:00 2001 From: Dede Nugroho <49790011+thisnugroho@users.noreply.github.com> Date: Wed, 13 Dec 2023 11:56:52 +0800 Subject: [PATCH 06/11] update policy --- app/Policies/SubmissionPolicy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Policies/SubmissionPolicy.php b/app/Policies/SubmissionPolicy.php index 3238a8dd5..9e0bfba64 100644 --- a/app/Policies/SubmissionPolicy.php +++ b/app/Policies/SubmissionPolicy.php @@ -46,7 +46,7 @@ public function update(User $user, Submission $submission) public function delete(User $user, Submission $submission) { // Only submission with status: withdrawn or declined can be deleted. - if (!in_array($submission->status, [SubmissionStatus::Declined, SubmissionStatus::Withdrawn])) { + if (!in_array($submission->status, [SubmissionStatus::Declined, SubmissionStatus::Withdrawn, SubmissionStatus::Incomplete])) { return false; } From 5b3bca8ef8fb32046cfe8bd70eb99d855f701ba5 Mon Sep 17 00:00:00 2001 From: Dede Nugroho <49790011+thisnugroho@users.noreply.github.com> Date: Thu, 14 Dec 2023 16:23:45 +0800 Subject: [PATCH 07/11] fix: local scope. --- app/Models/Submission.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Models/Submission.php b/app/Models/Submission.php index 72297eb40..b41fc6536 100644 --- a/app/Models/Submission.php +++ b/app/Models/Submission.php @@ -144,7 +144,7 @@ public function scopeinAnyStatus(Builder $query, array $statuses) public function scopePublished(Builder $query) { - return $this->status(SubmissionStatus::Published); + return $query->status(SubmissionStatus::Published); } public function scopeStage(Builder $query, SubmissionStage $stage) From 201080d47f3873aa84deaf042655a46277b173e4 Mon Sep 17 00:00:00 2001 From: Dede Nugroho <49790011+thisnugroho@users.noreply.github.com> Date: Thu, 14 Dec 2023 16:25:04 +0800 Subject: [PATCH 08/11] wip: improving submission list. --- .../Pages/ManageSubmissions.php | 239 +++++++++++------- 1 file changed, 143 insertions(+), 96 deletions(-) diff --git a/app/Panel/Resources/SubmissionResource/Pages/ManageSubmissions.php b/app/Panel/Resources/SubmissionResource/Pages/ManageSubmissions.php index 1f96c2f64..184301d74 100644 --- a/app/Panel/Resources/SubmissionResource/Pages/ManageSubmissions.php +++ b/app/Panel/Resources/SubmissionResource/Pages/ManageSubmissions.php @@ -12,6 +12,7 @@ use Filament\Resources\Components\Tab; use Filament\Resources\Pages\ManageRecords; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Support\Facades\Auth; class ManageSubmissions extends ManageRecords { @@ -44,209 +45,255 @@ protected function getHeaderActions(): array ]; } + /** Need to be optimized */ public function getTabs(): array { - $currentUser = auth()->user(); return [ 'My Queue' => Tab::make('My Queue') ->when( - $currentUser->hasRole(UserRole::Author->value), - function (Tab $tab) use ($currentUser) { + Auth::user()->hasRole(UserRole::Author->value), + function (Tab $tab) { return $tab->modifyQueryUsing( - fn (Builder $query) => $query->where('user_id', $currentUser->getKey()) - ->inAnyStatus([ - SubmissionStatus::Incomplete, - SubmissionStatus::Queued, - SubmissionStatus::OnReview, - SubmissionStatus::Editing, - ]) + function (Builder $query) { + return $query->where('user_id', Auth::id()) + ->inAnyStatus([ + SubmissionStatus::Queued, + SubmissionStatus::OnReview, + ]); + } ); } ) ->when( - $currentUser->hasRole(UserRole::Reviewer->value), - function (Tab $tab) use ($currentUser) { + Auth::user()->hasRole(UserRole::Reviewer->value), + function (Tab $tab) { return $tab->modifyQueryUsing( - fn (Builder $query) => $query->whereHas( - 'reviews', - fn (Builder $query) => $query->where('user_id', $currentUser->getKey()) - ) - ->inAnyStatus([ + function (Builder $query) { + return $query->whereHas('reviews', function (Builder $query) { + return $query->where('user_id', Auth::id()); + })->inAnyStatus([ SubmissionStatus::Incomplete, SubmissionStatus::Queued, SubmissionStatus::OnReview, SubmissionStatus::Editing, - ]) + ]); + } ); } ) ->when( - $currentUser->hasRole(UserRole::Editor->value), - function (Tab $tab) use ($currentUser) { + Auth::user()->hasRole(UserRole::Editor->value), + function (Tab $tab) { return $tab->modifyQueryUsing( - fn (Builder $query) => $query->whereHas( - 'participants', - fn (Builder $query) => $query->where('user_id', $currentUser->getKey()) - ) - ->inAnyStatus([ + function (Builder $query) { + return $query->whereHas('participants', function (Builder $query) { + return $query->where('user_id', Auth::id()); + })->inAnyStatus([ SubmissionStatus::Incomplete, SubmissionStatus::Queued, SubmissionStatus::OnReview, SubmissionStatus::Editing, - ]) + ]); + } ); } ) ->when( - $currentUser->hasAnyRole([ + Auth::user()->hasAnyRole([ UserRole::Admin->value, UserRole::ConferenceManager->value ]), function (Tab $tab) { return $tab->modifyQueryUsing( - fn (Builder $query) => $query->inAnyStatus([ - SubmissionStatus::Incomplete, - SubmissionStatus::Queued, - SubmissionStatus::OnReview, - SubmissionStatus::Editing, - ]) - ); - } - ), - 'Active' => Tab::make('Active') - ->when( - $currentUser->hasRole(UserRole::Author->value), - function (Tab $tab) { - return $tab->modifyQueryUsing( - fn (Builder $query) => $query->where('user_id', auth()->id()) - ->inAnyStatus([ + function (Builder $query) { + return $query->inAnyStatus([ + SubmissionStatus::Incomplete, SubmissionStatus::Queued, SubmissionStatus::OnReview, SubmissionStatus::Editing, - ]) + ]); + } ); } - ) + ), + 'Active' => Tab::make('Active') ->when( - $currentUser->hasRole(UserRole::Reviewer->value), - function (Tab $tab) use ($currentUser) { + Auth::user()->hasRole(UserRole::Author->value), + function (Tab $tab) { return $tab->modifyQueryUsing( - fn (Builder $query) => $query->whereHas( - 'reviews', - fn (Builder $query) => $query->where('user_id', $currentUser->getKey()) + function (Builder $query) { + return $query->where('user_id', Auth::id()) ->inAnyStatus([ SubmissionStatus::Queued, SubmissionStatus::OnReview, SubmissionStatus::Editing, - ]) - ) + ]); + } + ); } ) ->when( - $currentUser->hasRole(UserRole::Editor->value), - function (Tab $tab) use ($currentUser) { + Auth::user()->hasRole(UserRole::Reviewer->value), + function (Tab $tab) { return $tab->modifyQueryUsing( - fn (Builder $query) => $query->whereHas( - 'participants', - fn (Builder $query) => $query->where('user_id', $currentUser->getKey()) - ) - ->inAnyStatus([ + function (Builder $query) { + return $query->whereHas('reviews', function (Builder $query) { + return $query->where('user_id', Auth::id()); + })->inAnyStatus([ SubmissionStatus::Queued, SubmissionStatus::OnReview, SubmissionStatus::Editing, - ]) + ]); + } + ); + } + ) + ->when( + Auth::user()->hasRole(UserRole::Editor->value), + function (Tab $tab) { + return $tab->modifyQueryUsing( + function (Builder $query) { + return $query->whereHas( + 'participants', + function (Builder $query) { + return $query->where('user_id', Auth::id()); + } + ) + ->inAnyStatus([ + SubmissionStatus::Queued, + SubmissionStatus::OnReview, + SubmissionStatus::Editing, + ]); + } ); } ) ->when( - $currentUser->hasAnyRole([ + Auth::user()->hasAnyRole([ UserRole::Admin->value, UserRole::ConferenceManager->value ]), function (Tab $tab) { return $tab->modifyQueryUsing( - fn (Builder $query) => $query - ->inAnyStatus([ + function (Builder $query) { + return $query->inAnyStatus([ SubmissionStatus::Queued, SubmissionStatus::OnReview, SubmissionStatus::Editing, - ]) + ]); + } ); } ), 'Published' => Tab::make('Published') ->when( - $currentUser->hasRole(UserRole::Author->value), - function (Tab $tab) use ($currentUser) { + Auth::user()->hasRole(UserRole::Author->value), + function (Tab $tab) { return $tab->modifyQueryUsing( - fn (Builder $query) => $query - ->where('user_id', $currentUser->getKey()) - ->published() + function (Builder $query) { + return $query->where('user_id', Auth::id()) + ->published(); + } ); } ) ->when( - $currentUser->hasRole(UserRole::Reviewer->value), - function (Tab $tab) use ($currentUser) { + Auth::user()->hasRole(UserRole::Editor->value), + function (Tab $tab) { return $tab->modifyQueryUsing( - fn (Builder $query) => $query->whereHas( - 'reviews', - fn (Builder $query) => $query->where('user_id', $currentUser->getKey()) - )->published() + function (Builder $query) { + return $query->whereHas('participants', function (Builder $query) { + return $query->where('user_id', Auth::id()); + })->published(); + } ); } ) ->when( - $currentUser->hasRole([ + Auth::user()->hasRole(UserRole::Reviewer->value), + function (Tab $tab) { + return $tab->modifyQueryUsing( + function (Builder $query) { + return $query->whereHas('reviews', function (Builder $query) { + return $query->where('user_id', Auth::id()); + })->published(); + } + ); + } + ) + ->when( + Auth::user()->hasRole([ UserRole::Admin->value, UserRole::ConferenceManager->value ]), function (Tab $tab) { return $tab->modifyQueryUsing( - fn (Builder $query) => $query->published() + function (Builder $query) { + return $query->published(); + } ); } ), 'Archived' => Tab::make('Archived') ->when( - $currentUser->hasRole(UserRole::Author->value), - function (Tab $tab) use ($currentUser) { + Auth::user()->hasRole(UserRole::Author->value), + function (Tab $tab) { + return $tab->modifyQueryUsing( + function (Builder $query) { + return $query->where('user_id', Auth::id()) + ->inAnyStatus([ + SubmissionStatus::Declined, + SubmissionStatus::Withdrawn, + ]); + } + ); + } + ) + ->when( + Auth::user()->hasRole(UserRole::Editor->value), + function (Tab $tab) { return $tab->modifyQueryUsing( - fn (Builder $query) => $query->where('user_id', $currentUser->getKey()) - ->inAnyStatus([ + function (Builder $query) { + return $query->whereHas('participants', function (Builder $query) { + return $query->where('user_id', Auth::id()); + })->inAnyStatus([ SubmissionStatus::Declined, SubmissionStatus::Withdrawn, - ]) + ]); + } ); } ) ->when( - $currentUser->hasRole(UserRole::Reviewer->value), + Auth::user()->hasRole(UserRole::Reviewer->value), function (Tab $tab) { return $tab->modifyQueryUsing( - fn (Builder $query) => $query->whereHas( - 'reviews', - fn (Builder $query) => $query->where('user_id', auth()->id()) - ->inAnyStatus([ - SubmissionStatus::Declined, - SubmissionStatus::Withdrawn, - ]) - ) + function (Builder $query) { + return $query->whereHas('reviews', function (Builder $query) { + return $query->where('user_id', Auth::id()); + })->inAnyStatus([ + SubmissionStatus::Declined, + SubmissionStatus::Withdrawn, + ]); + } + ); } ) ->when( - $currentUser->hasAnyRole([ + Auth::user()->hasAnyRole([ UserRole::Admin->value, UserRole::ConferenceManager->value ]), function (Tab $tab) { return $tab->modifyQueryUsing( - fn (Builder $query) => $query->inAnyStatus([ - SubmissionStatus::Declined, - SubmissionStatus::Withdrawn, - ]) + function (Builder $query) { + return $query->inAnyStatus([ + SubmissionStatus::Declined, + SubmissionStatus::Withdrawn, + ]); + } ); } ) From 47aa36aaebc7e9fb9b0e733328fdc0b60fa59843 Mon Sep 17 00:00:00 2001 From: Dede Nugroho <49790011+thisnugroho@users.noreply.github.com> Date: Thu, 28 Dec 2023 11:38:18 +0800 Subject: [PATCH 09/11] Tabs Improvement. --- .../Pages/ManageSubmissions.php | 316 ++++-------------- 1 file changed, 68 insertions(+), 248 deletions(-) diff --git a/app/Panel/Resources/SubmissionResource/Pages/ManageSubmissions.php b/app/Panel/Resources/SubmissionResource/Pages/ManageSubmissions.php index 935fb3368..3b5c6342d 100644 --- a/app/Panel/Resources/SubmissionResource/Pages/ManageSubmissions.php +++ b/app/Panel/Resources/SubmissionResource/Pages/ManageSubmissions.php @@ -22,6 +22,14 @@ class ManageSubmissions extends ManageRecords protected static string $view = 'panel.resources.submission-resource.pages.list-submission'; + protected const TAB_MYQUEUE = 'My Queue'; + + protected const TAB_ACTIVE = 'Active'; + + protected const TAB_PUBLISHED = 'Published'; + + protected const TAB_ARCHIVED = 'Archived'; + public function infolist(Infolist $infolist): Infolist { return $infolist @@ -67,258 +75,70 @@ protected function getHeaderActions(): array ]; } + protected static function generateQueryByCurrentUser(string $tabs) + { + $statuses = match ($tabs) { + static::TAB_MYQUEUE => [ + SubmissionStatus::Queued, + ], + static::TAB_ACTIVE => [ + SubmissionStatus::OnReview, + SubmissionStatus::Editing, + ], + static::TAB_PUBLISHED => [ + SubmissionStatus::Published, + ], + static::TAB_ARCHIVED => [ + SubmissionStatus::Declined, + SubmissionStatus::Withdrawn, + ], + default => null, + }; + + $query = static::getResource()::getEloquentQuery(); + + if (Auth::user()->hasAnyRole([ + UserRole::Admin->value, + UserRole::ConferenceManager->value + ])) { + return $query->whereIn('status', $statuses); + } + + return $query->when( + Auth::user()->hasRole(UserRole::Author->value), + function (Builder $query) { + $query->where('user_id', Auth::id()); + } + )->when( + Auth::user()->hasRole(UserRole::Reviewer->value), + function (Builder $query) use ($statuses) { + $query->whereHas('reviews', function (Builder $query) { + return $query->where('user_id', Auth::id()); + })->whereIn('status', [...$statuses, SubmissionStatus::OnReview]); + } + )->when( + Auth::user()->hasRole(UserRole::Editor->value), + function (Builder $query) use ($statuses) { + $query->whereHas('participants', function (Builder $query) { + return $query->where('user_id', Auth::id()); + })->whereIn('status', $statuses); + } + ); + } + /** Need to be optimized */ public function getTabs(): array { - return [ - 'My Queue' => Tab::make('My Queue') - ->when( - Auth::user()->hasRole(UserRole::Author->value), - function (Tab $tab) { - return $tab->modifyQueryUsing( - function (Builder $query) { - return $query->where('user_id', Auth::id()) - ->inAnyStatus([ - SubmissionStatus::Queued, - SubmissionStatus::OnReview, - ]); - } - ); - } - ) - ->when( - Auth::user()->hasRole(UserRole::Reviewer->value), - function (Tab $tab) { - return $tab->modifyQueryUsing( - function (Builder $query) { - return $query->whereHas('reviews', function (Builder $query) { - return $query->where('user_id', Auth::id()); - })->inAnyStatus([ - SubmissionStatus::Incomplete, - SubmissionStatus::Queued, - SubmissionStatus::OnReview, - SubmissionStatus::Editing, - ]); - } - ); - } - ) - ->when( - Auth::user()->hasRole(UserRole::Editor->value), - function (Tab $tab) { - return $tab->modifyQueryUsing( - function (Builder $query) { - return $query->whereHas('participants', function (Builder $query) { - return $query->where('user_id', Auth::id()); - })->inAnyStatus([ - SubmissionStatus::Incomplete, - SubmissionStatus::Queued, - SubmissionStatus::OnReview, - SubmissionStatus::Editing, - ]); - } - ); - } - ) - ->when( - Auth::user()->hasAnyRole([ - UserRole::Admin->value, - UserRole::ConferenceManager->value - ]), - function (Tab $tab) { - return $tab->modifyQueryUsing( - function (Builder $query) { - return $query->inAnyStatus([ - SubmissionStatus::Incomplete, - SubmissionStatus::Queued, - SubmissionStatus::OnReview, - SubmissionStatus::Editing, - ]); - } - ); - } - ), - 'Active' => Tab::make('Active') - ->when( - Auth::user()->hasRole(UserRole::Author->value), - function (Tab $tab) { - return $tab->modifyQueryUsing( - function (Builder $query) { - return $query->where('user_id', Auth::id()) - ->inAnyStatus([ - SubmissionStatus::Queued, - SubmissionStatus::OnReview, - SubmissionStatus::Editing, - ]); - } - - ); - } - ) - ->when( - Auth::user()->hasRole(UserRole::Reviewer->value), - function (Tab $tab) { - return $tab->modifyQueryUsing( - function (Builder $query) { - return $query->whereHas('reviews', function (Builder $query) { - return $query->where('user_id', Auth::id()); - })->inAnyStatus([ - SubmissionStatus::Queued, - SubmissionStatus::OnReview, - SubmissionStatus::Editing, - ]); - } - ); - } - ) - ->when( - Auth::user()->hasRole(UserRole::Editor->value), - function (Tab $tab) { - return $tab->modifyQueryUsing( - function (Builder $query) { - return $query->whereHas( - 'participants', - function (Builder $query) { - return $query->where('user_id', Auth::id()); - } - ) - ->inAnyStatus([ - SubmissionStatus::Queued, - SubmissionStatus::OnReview, - SubmissionStatus::Editing, - ]); - } - ); - } - ) - ->when( - Auth::user()->hasAnyRole([ - UserRole::Admin->value, - UserRole::ConferenceManager->value - ]), - function (Tab $tab) { - return $tab->modifyQueryUsing( - function (Builder $query) { - return $query->inAnyStatus([ - SubmissionStatus::Queued, - SubmissionStatus::OnReview, - SubmissionStatus::Editing, - ]); - } - ); - } - ), - 'Published' => Tab::make('Published') - ->when( - Auth::user()->hasRole(UserRole::Author->value), - function (Tab $tab) { - return $tab->modifyQueryUsing( - function (Builder $query) { - return $query->where('user_id', Auth::id()) - ->published(); - } - ); - } - ) - ->when( - Auth::user()->hasRole(UserRole::Editor->value), - function (Tab $tab) { - return $tab->modifyQueryUsing( - function (Builder $query) { - return $query->whereHas('participants', function (Builder $query) { - return $query->where('user_id', Auth::id()); - })->published(); - } - ); - } - ) - ->when( - Auth::user()->hasRole(UserRole::Reviewer->value), - function (Tab $tab) { - return $tab->modifyQueryUsing( - function (Builder $query) { - return $query->whereHas('reviews', function (Builder $query) { - return $query->where('user_id', Auth::id()); - })->published(); - } - ); - } - ) - ->when( - Auth::user()->hasRole([ - UserRole::Admin->value, - UserRole::ConferenceManager->value - ]), - function (Tab $tab) { - return $tab->modifyQueryUsing( - function (Builder $query) { - return $query->published(); - } - ); - } - ), - 'Archived' => Tab::make('Archived') - ->when( - Auth::user()->hasRole(UserRole::Author->value), - function (Tab $tab) { - return $tab->modifyQueryUsing( - function (Builder $query) { - return $query->where('user_id', Auth::id()) - ->inAnyStatus([ - SubmissionStatus::Declined, - SubmissionStatus::Withdrawn, - ]); - } - ); - } - ) - ->when( - Auth::user()->hasRole(UserRole::Editor->value), - function (Tab $tab) { - return $tab->modifyQueryUsing( - function (Builder $query) { - return $query->whereHas('participants', function (Builder $query) { - return $query->where('user_id', Auth::id()); - })->inAnyStatus([ - SubmissionStatus::Declined, - SubmissionStatus::Withdrawn, - ]); - } - ); - } - ) - ->when( - Auth::user()->hasRole(UserRole::Reviewer->value), - function (Tab $tab) { - return $tab->modifyQueryUsing( - function (Builder $query) { - return $query->whereHas('reviews', function (Builder $query) { - return $query->where('user_id', Auth::id()); - })->inAnyStatus([ - SubmissionStatus::Declined, - SubmissionStatus::Withdrawn, - ]); - } - ); - } - ) - ->when( - Auth::user()->hasAnyRole([ - UserRole::Admin->value, - UserRole::ConferenceManager->value - ]), - function (Tab $tab) { - return $tab->modifyQueryUsing( - function (Builder $query) { - return $query->inAnyStatus([ - SubmissionStatus::Declined, - SubmissionStatus::Withdrawn, - ]); - } - ); - } - ) + return [ + static::TAB_MYQUEUE => Tab::make("My Queue") + ->modifyQueryUsing(fn (): Builder => static::generateQueryByCurrentUser(static::TAB_MYQUEUE)), + static::TAB_ACTIVE => Tab::make("Active") + ->modifyQueryUsing(fn (): Builder => static::generateQueryByCurrentUser(static::TAB_ACTIVE)), + static::TAB_PUBLISHED => Tab::make("Published") + ->modifyQueryUsing(fn (): Builder => static::generateQueryByCurrentUser(static::TAB_PUBLISHED)), + static::TAB_ARCHIVED => Tab::make("Archived") + ->modifyQueryUsing(fn (): Builder => static::generateQueryByCurrentUser(static::TAB_ARCHIVED)), ]; } } From 7fab61450dc188f9da4f635a6961b5cdaf0ebca1 Mon Sep 17 00:00:00 2001 From: Dede Nugroho <49790011+thisnugroho@users.noreply.github.com> Date: Thu, 28 Dec 2023 17:00:03 +0800 Subject: [PATCH 10/11] Fix: Submission list when user had multiple role. --- .../Pages/ManageSubmissions.php | 61 +++++++++++++++---- 1 file changed, 50 insertions(+), 11 deletions(-) diff --git a/app/Panel/Resources/SubmissionResource/Pages/ManageSubmissions.php b/app/Panel/Resources/SubmissionResource/Pages/ManageSubmissions.php index 3b5c6342d..2fb088360 100644 --- a/app/Panel/Resources/SubmissionResource/Pages/ManageSubmissions.php +++ b/app/Panel/Resources/SubmissionResource/Pages/ManageSubmissions.php @@ -36,7 +36,7 @@ public function infolist(Infolist $infolist): Infolist ->schema([ ShoutEntry::make('title') ->hidden(function () { - return StageManager::callForAbstract()->isStageOpen() || ! Auth::user()->can('Workflow:update'); + return StageManager::callForAbstract()->isStageOpen() || !Auth::user()->can('Workflow:update'); }) ->type('warning') ->content(function () { @@ -101,28 +101,67 @@ protected static function generateQueryByCurrentUser(string $tabs) UserRole::Admin->value, UserRole::ConferenceManager->value ])) { - return $query->whereIn('status', $statuses); + return $query->whereIn('status', $statuses)->when( + $tabs == static::TAB_MYQUEUE, + function (Builder $query) { + $query->orWhere([ + ['user_id', '=', Auth::id()], + ['status', '=', SubmissionStatus::Incomplete] + ]); + } + ); } + + // Digunakan untuk menentukan mengetahui kondisi sebelumnya sudah ada atau belum + $conditionBeforeExist = false; return $query->when( Auth::user()->hasRole(UserRole::Author->value), - function (Builder $query) { - $query->where('user_id', Auth::id()); + function (Builder $query) use ($statuses, &$conditionBeforeExist) { + $query->where('user_id', Auth::id())->whereIn('status', $statuses); + $conditionBeforeExist = true; } )->when( Auth::user()->hasRole(UserRole::Reviewer->value), - function (Builder $query) use ($statuses) { - $query->whereHas('reviews', function (Builder $query) { - return $query->where('user_id', Auth::id()); - })->whereIn('status', [...$statuses, SubmissionStatus::OnReview]); + function (Builder $query) use (&$conditionBeforeExist, $tabs, $statuses) { + $query->when( + $conditionBeforeExist, + function (Builder $query) { + $query->orWhereHas('reviews', function (Builder $query) { + return $query->where('user_id', Auth::id()); + }); + }, + function (Builder $query) { + $query->whereHas('reviews', function (Builder $query) { + return $query->where('user_id', Auth::id()); + }); + } + )->when($tabs != static::TAB_MYQUEUE, function (Builder $query) use ($statuses) { + $query->whereIn('status', $statuses); + }); + $conditionBeforeExist = true; } )->when( Auth::user()->hasRole(UserRole::Editor->value), - function (Builder $query) use ($statuses) { - $query->whereHas('participants', function (Builder $query) { - return $query->where('user_id', Auth::id()); + function (Builder $query) use ($statuses, &$conditionBeforeExist) { + $query->when($conditionBeforeExist, function (Builder $query) { + $query->orWhereHas('participants', function (Builder $query) { + return $query->where('user_id', Auth::id()); + }); + }, function (Builder $query) { + $query->whereHas('participants', function (Builder $query) { + return $query->where('user_id', Auth::id()); + }); })->whereIn('status', $statuses); } + )->when( + $tabs == static::TAB_MYQUEUE, + function (Builder $query) { + $query->orWhere([ + ['user_id', '=', Auth::id()], + ['status', '=', SubmissionStatus::Incomplete] + ]); + } ); } From 5950bdf455bcd6b3cc501746f217bbb931da9a22 Mon Sep 17 00:00:00 2001 From: Dede Nugroho <49790011+thisnugroho@users.noreply.github.com> Date: Thu, 28 Dec 2023 22:15:34 +0800 Subject: [PATCH 11/11] remove unused method --- app/Models/Submission.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/app/Models/Submission.php b/app/Models/Submission.php index df5d40118..01a4b4bc3 100644 --- a/app/Models/Submission.php +++ b/app/Models/Submission.php @@ -103,7 +103,7 @@ protected static function booted(): void ]); //If current user does not exists in participant - if (! $userAsParticipant = $submission->user->asParticipant()) { + if (!$userAsParticipant = $submission->user->asParticipant()) { $userAsParticipant = CreateParticipantFromUserAction::run($submission->user); } @@ -155,11 +155,6 @@ public function contributors() return $this->hasMany(SubmissionContributor::class); } - public function scopeinAnyStatus(Builder $query, array $statuses) - { - return $query->whereIn('status', $statuses); - } - public function scopePublished(Builder $query) { return $query->status(SubmissionStatus::Published);