first
This commit is contained in:
46
app/Services/Ppm/PpmScopeService.php
Normal file
46
app/Services/Ppm/PpmScopeService.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Ppm;
|
||||
|
||||
use App\Models\Application;
|
||||
use App\Models\Position;
|
||||
use App\Models\StaffAssignment;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class PpmScopeService
|
||||
{
|
||||
/**
|
||||
* @return Collection<int, int>
|
||||
*/
|
||||
public function assignedPusatIds(User $user): Collection
|
||||
{
|
||||
$ppmPosition = Position::query()->where('code', 'PPM')->first();
|
||||
|
||||
if ($ppmPosition === null) {
|
||||
return collect();
|
||||
}
|
||||
|
||||
return StaffAssignment::query()
|
||||
->where('user_id', $user->id)
|
||||
->where('position_id', $ppmPosition->id)
|
||||
->where('status', 'active')
|
||||
->whereNull('saluran_mengundi_id')
|
||||
->pluck('pusat_mengundi_id')
|
||||
->map(fn (mixed $id): int => (int) $id)
|
||||
->values();
|
||||
}
|
||||
|
||||
public function canAccessApplication(User $user, Application $application): bool
|
||||
{
|
||||
if ($user->hasRole('Admin')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (! $user->hasRole('PPM')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->assignedPusatIds($user)->contains((int) $application->pusat_mengundi_id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user