56 lines
1.6 KiB
PHP
56 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Ktm;
|
|
|
|
use App\Models\Application;
|
|
use App\Models\Position;
|
|
use App\Models\StaffAssignment;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class KtmScopeService
|
|
{
|
|
/**
|
|
* @return EloquentCollection<int, StaffAssignment>
|
|
*/
|
|
public function assignmentsFor(User $user): EloquentCollection
|
|
{
|
|
$ktmPosition = Position::query()->where('code', 'KTM')->first();
|
|
|
|
if ($ktmPosition === null) {
|
|
return new EloquentCollection;
|
|
}
|
|
|
|
return StaffAssignment::query()
|
|
->with(['pusatMengundi', 'saluranMengundi', 'teamMembers.application', 'teamMembers.position'])
|
|
->where('user_id', $user->id)
|
|
->where('position_id', $ktmPosition->id)
|
|
->where('status', 'active')
|
|
->whereNotNull('saluran_mengundi_id')
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, int>
|
|
*/
|
|
public function assignmentIdsFor(User $user): Collection
|
|
{
|
|
return $this->assignmentsFor($user)
|
|
->pluck('id')
|
|
->map(fn (mixed $id): int => (int) $id)
|
|
->values();
|
|
}
|
|
|
|
public function ownsAssignment(User $user, StaffAssignment $assignment): bool
|
|
{
|
|
return $this->assignmentIdsFor($user)->contains((int) $assignment->id);
|
|
}
|
|
|
|
public function canAccessApplication(User $user, Application $application): bool
|
|
{
|
|
return $application->selected_ktm_assignment_id !== null
|
|
&& $this->assignmentIdsFor($user)->contains((int) $application->selected_ktm_assignment_id);
|
|
}
|
|
}
|