From 0a688f102858378b0f08b9ddce76c7b833fb1e7b Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Tue, 4 Aug 2026 23:15:00 +0700 Subject: [PATCH] refactor: streamline user query methods for improved readability and maintainability --- app/Services/Admin/HR/EmployeeService.php | 39 ++++++++----------- .../js/pages/admin/hr/employee/columns.tsx | 9 +++-- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/app/Services/Admin/HR/EmployeeService.php b/app/Services/Admin/HR/EmployeeService.php index 5d3887e..d396a8d 100644 --- a/app/Services/Admin/HR/EmployeeService.php +++ b/app/Services/Admin/HR/EmployeeService.php @@ -11,16 +11,16 @@ class EmployeeService { public function getAll(array $filters = []): Collection { - return User::select('id', 'email', 'username', 'is_active') - ->whereHas('employee') + return User::select(['id', 'email', 'username', 'is_active']) + ->where(fn ($q) => $q->whereHas('employee')->orWhereHas('roles', fn ($rq) => $rq->where('name', 'Owner'))) ->with([ - 'userProfile' => fn ($q) => $q->select('id', 'user_id', 'full_name', 'phone_number', 'gender'), - 'employee' => fn ($q) => $q->select('id', 'user_id', 'join_date', 'employment_status', 'base_salary'), - 'roles' => fn ($q) => $q->select('id', 'name'), + 'userProfile' => fn($q) => $q->select('id', 'user_id', 'full_name', 'phone_number', 'gender'), + 'employee' => fn($q) => $q->select('id', 'user_id', 'join_date', 'employment_status', 'base_salary'), + 'roles' => fn($q) => $q->select('id', 'name'), ]) - ->when($filters['employment_status'] ?? null, fn ($q, $status) => $q->whereHas('employee', fn ($eq) => $eq->where('employment_status', $status))) - ->when(isset($filters['is_active']) && $filters['is_active'] !== '', fn ($q) => $q->where('is_active', filter_var($filters['is_active'], FILTER_VALIDATE_BOOLEAN))) - ->when($filters['gender'] ?? null, fn ($q, $gender) => $q->whereHas('userProfile', fn ($uq) => $uq->where('gender', $gender))) + ->when($filters['employment_status'] ?? null, fn($q, $status) => $q->whereHas('employee', fn($eq) => $eq->where('employment_status', $status))) + ->when(isset($filters['is_active']) && $filters['is_active'] !== '', fn($q) => $q->where('is_active', filter_var($filters['is_active'], FILTER_VALIDATE_BOOLEAN))) + ->when($filters['gender'] ?? null, fn($q, $gender) => $q->whereHas('userProfile', fn($uq) => $uq->where('gender', $gender))) ->latest() ->get(); } @@ -28,26 +28,21 @@ public function getAll(array $filters = []): Collection public function paginated(int $perPage = 25, string $search = '', string $sort = 'created_at', string $direction = 'desc', array $filters = []): LengthAwarePaginator { return User::query() - ->select('id', 'email', 'username', 'is_active') - ->whereHas('employee') + ->select(['id', 'email', 'username', 'is_active']) + ->where(fn ($q) => $q->whereHas('employee')->orWhereHas('roles', fn ($rq) => $rq->where('name', 'Owner'))) ->with([ - 'userProfile' => fn ($q) => $q->select('id', 'user_id', 'full_name', 'phone_number', 'gender'), - 'employee' => fn ($q) => $q->select('id', 'user_id', 'join_date', 'employment_status', 'base_salary'), - 'roles' => fn ($q) => $q->select('id', 'name'), + 'userProfile' => fn($q) => $q->select('id', 'user_id', 'full_name', 'phone_number', 'gender'), + 'employee' => fn($q) => $q->select('id', 'user_id', 'join_date', 'employment_status', 'base_salary'), + 'roles' => fn($q) => $q->select('id', 'name'), ]) - ->when($search, fn ($q) => $q->whereHas('userProfile', fn ($uq) => $uq->where('full_name', 'like', "%{$search}%"))) - ->when($filters['employment_status'] ?? null, fn ($q, $status) => $q->whereHas('employee', fn ($eq) => $eq->where('employment_status', $status))) - ->when(isset($filters['is_active']) && $filters['is_active'] !== '', fn ($q) => $q->where('is_active', filter_var($filters['is_active'], FILTER_VALIDATE_BOOLEAN))) - ->when($filters['gender'] ?? null, fn ($q, $gender) => $q->whereHas('userProfile', fn ($uq) => $uq->where('gender', $gender))) + ->when($search, fn($q) => $q->whereHas('userProfile', fn($uq) => $uq->where('full_name', 'like', "%{$search}%"))) + ->when($filters['employment_status'] ?? null, fn($q, $status) => $q->whereHas('employee', fn($eq) => $eq->where('employment_status', $status))) + ->when(isset($filters['is_active']) && $filters['is_active'] !== '', fn($q) => $q->where('is_active', filter_var($filters['is_active'], FILTER_VALIDATE_BOOLEAN))) + ->when($filters['gender'] ?? null, fn($q, $gender) => $q->whereHas('userProfile', fn($uq) => $uq->where('gender', $gender))) ->orderBy($sort, $direction) ->paginate($perPage); } - public function getById(int $id): User - { - return User::with(['userProfile', 'employee'])->findOrFail($id); - } - public function create(array $data): User { return DB::transaction(function () use ($data) { diff --git a/resources/js/pages/admin/hr/employee/columns.tsx b/resources/js/pages/admin/hr/employee/columns.tsx index 9aa9569..0c8ac10 100644 --- a/resources/js/pages/admin/hr/employee/columns.tsx +++ b/resources/js/pages/admin/hr/employee/columns.tsx @@ -111,12 +111,15 @@ export function createEmployeeColumns( header: () => Status, cell: ({ row }) => { const employee = row.original; + const status = employee.employee?.employment_status; + + if (!status) { + return -; + } return ( - {getEmploymentStatusLabel( - employee.employee?.employment_status ?? '', - )} + {getEmploymentStatusLabel(status)} ); },