refactor: update EmployeeController to use ToggleStatusRequest for status toggling and streamline EmployeeService methods for improved validation and clarity
This commit is contained in:
parent
0803cbeb96
commit
b7fddf0597
@ -9,6 +9,7 @@
|
||||
use App\Http\Controllers\Concerns\ParsesDataTableQuery;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Admin\Hr\EmployeeRequest;
|
||||
use App\Http\Requests\Admin\ToggleStatusRequest;
|
||||
use App\Models\User;
|
||||
use App\Services\Hr\EmployeeService;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
@ -91,13 +92,9 @@ public function update(EmployeeRequest $request, User $user): RedirectResponse
|
||||
return redirect()->route('admin.hr.employees.index');
|
||||
}
|
||||
|
||||
public function toggleStatus(Request $request, User $user): RedirectResponse
|
||||
public function toggleStatus(ToggleStatusRequest $request, User $user): RedirectResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'is_active' => ['required', 'boolean'],
|
||||
]);
|
||||
|
||||
$this->employeeService->toggleStatus($user, $validated['is_active']);
|
||||
$this->employeeService->toggleStatus($user, $request->validated());
|
||||
|
||||
$this->flashStatusUpdated('pegawai');
|
||||
|
||||
|
||||
@ -35,22 +35,12 @@ public function paginateForIndex(
|
||||
});
|
||||
});
|
||||
})
|
||||
->when(
|
||||
$role !== '',
|
||||
fn ($query) => $query->whereHas('roles', fn ($query) => $query->where('name', $role))
|
||||
)
|
||||
->when(
|
||||
$gender !== '',
|
||||
fn ($query) => $query->whereHas('profile', fn ($query) => $query->where('gender', $gender))
|
||||
)
|
||||
->when(
|
||||
$employmentStatus !== '',
|
||||
fn ($query) => $query->whereHas('employee', fn ($query) => $query->where('employment_status', $employmentStatus))
|
||||
)
|
||||
->when(
|
||||
$isActive !== '',
|
||||
fn ($query) => $query->where('is_active', $isActive === '1')
|
||||
);
|
||||
->when($role !== '', fn ($query) => $query->whereHas('roles', fn ($query) => $query->where('name', $role)))
|
||||
->when($gender !== '', fn ($query) => $query->whereHas('profile', fn ($query) => $query->where('gender', $gender)))
|
||||
->when($employmentStatus !== '', function ($query) use ($employmentStatus): void {
|
||||
$query->whereHas('employee', fn ($query) => $query->where('employment_status', $employmentStatus));
|
||||
})
|
||||
->when($isActive !== '', fn ($query) => $query->where('is_active', $isActive === '1'));
|
||||
|
||||
$this->applySorting($query, $tableQuery['sort'], $tableQuery['direction']);
|
||||
|
||||
@ -144,12 +134,12 @@ public function update(User $user, array $validated): void
|
||||
});
|
||||
}
|
||||
|
||||
public function toggleStatus(User $user, bool $isActive): void
|
||||
public function toggleStatus(User $user, array $validated): void
|
||||
{
|
||||
$user->is_active = $isActive;
|
||||
$user->is_active = $validated['is_active'];
|
||||
$user->save();
|
||||
|
||||
if (! $isActive) {
|
||||
if (! $validated['is_active']) {
|
||||
DB::table('sessions')->where('user_id', $user->id)->delete();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { Head, Link } from '@inertiajs/vue3';
|
||||
import { ArrowLeft } from '@lucide/vue';
|
||||
import EmployeeForm from '@/components/admin/hr/EmployeeForm.vue';
|
||||
import EmployeeForm from './form/EmployeeForm.vue';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import AdminLayout from '@/layouts/AdminLayout.vue';
|
||||
import type { EnumOption } from '@/types/employee';
|
||||
@ -14,15 +14,12 @@ defineProps<{
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
<Head title="Tambah Pegawai" />
|
||||
|
||||
<AdminLayout>
|
||||
<div class="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div class="space-y-1">
|
||||
<h2 class="text-2xl font-bold tracking-tight">
|
||||
Tambah Pegawai
|
||||
</h2>
|
||||
<h2 class="text-2xl font-bold tracking-tight">Tambah Pegawai</h2>
|
||||
</div>
|
||||
|
||||
<Button variant="outline" as-child class="shrink-0 self-start sm:self-center">
|
||||
@ -33,7 +30,6 @@ defineProps<{
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<EmployeeForm submit-url="/admin/hr/employees" method="post" submit-label="Simpan" :genders="genders"
|
||||
:employment-statuses="employmentStatuses" :roles="roles" />
|
||||
<EmployeeForm :genders="genders" :employment-statuses="employmentStatuses" :roles="roles" />
|
||||
</AdminLayout>
|
||||
</template>
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
import { Head, Link } from '@inertiajs/vue3';
|
||||
import { ArrowLeft } from '@lucide/vue';
|
||||
import { computed } from 'vue';
|
||||
import EmployeeForm from '@/components/admin/hr/EmployeeForm.vue';
|
||||
import EmployeeForm from './form/EmployeeForm.vue';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import AdminLayout from '@/layouts/AdminLayout.vue';
|
||||
import type { EmployeeListItem, EnumOption } from '@/types/employee';
|
||||
|
||||
@ -2,12 +2,15 @@
|
||||
import { Head, Link } from '@inertiajs/vue3';
|
||||
import { Plus } from '@lucide/vue';
|
||||
import { computed, ref, watch } from 'vue';
|
||||
import { columns } from '@/components/admin/hr/employees/columns';
|
||||
import { columns } from './table/columns';
|
||||
import { DataTable } from '@/components/data-table';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Card, CardContent } from '@/components/ui/card';
|
||||
import { useCan } from '@/composables/useCan';
|
||||
import { useDataTableQuery, useDataTableQuerySync } from '@/composables/useDataTableQuery';
|
||||
import {
|
||||
useDataTableQuery,
|
||||
useDataTableQuerySync,
|
||||
} from '@/composables/useDataTableQuery';
|
||||
import AdminLayout from '@/layouts/AdminLayout.vue';
|
||||
import type { DataTableFilterDef, DataTableSort } from '@/types/data-table';
|
||||
import type { EnumOption, PaginatedEmployees } from '@/types/employee';
|
||||
@ -107,15 +110,12 @@ watch(
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
<Head title="Pegawai" />
|
||||
|
||||
<AdminLayout>
|
||||
<div class="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div class="space-y-1">
|
||||
<h2 class="text-2xl font-bold tracking-tight">
|
||||
Pegawai
|
||||
</h2>
|
||||
<h2 class="text-2xl font-bold tracking-tight">Pegawai</h2>
|
||||
</div>
|
||||
|
||||
<Button v-if="can('employees.create')" as-child class="shrink-0 self-start sm:self-center">
|
||||
@ -128,10 +128,19 @@ watch(
|
||||
|
||||
<Card class="min-w-0">
|
||||
<CardContent class="min-w-0">
|
||||
<DataTable v-model:search="search" :columns="columns" :data="employees.data" :pagination="pagination"
|
||||
:pagination-links="employees.links" :sort="currentSort" :filter-defs="filterDefs"
|
||||
:filter-values="filterValues" @sort-change="setSort" @filter-change="setFilter"
|
||||
@filters-reset="resetFilters" />
|
||||
<DataTable
|
||||
v-model:search="search"
|
||||
:columns="columns"
|
||||
:data="employees.data"
|
||||
:pagination="pagination"
|
||||
:pagination-links="employees.links"
|
||||
:sort="currentSort"
|
||||
:filter-defs="filterDefs"
|
||||
:filter-values="filterValues"
|
||||
@sort-change="setSort"
|
||||
@filter-change="setFilter"
|
||||
@filters-reset="resetFilters"
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</AdminLayout>
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
import type { ColumnDef } from '@tanstack/vue-table';
|
||||
import { h } from 'vue';
|
||||
import DataTableActions from '@/components/admin/hr/employees/data-table-actions.vue';
|
||||
import EmployeeStatusToggle from '@/components/admin/hr/employees/employee-status-toggle.vue';
|
||||
import { DataTableColumnHeader } from '@/components/data-table';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import type { EmployeeListItem } from '@/types/employee';
|
||||
import DataTableActions from './data-table-actions.vue';
|
||||
import EmployeeStatusToggle from './employee-status-toggle.vue';
|
||||
|
||||
export const columns: ColumnDef<EmployeeListItem>[] = [
|
||||
{
|
||||
Loading…
Reference in New Issue
Block a user