Update Fix
This commit is contained in:
139
app/Http/Controllers/Admin/UserController.php
Normal file
139
app/Http/Controllers/Admin/UserController.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$limit = (int) $request->query('limit', 100);
|
||||
$limit = max(1, min(200, $limit));
|
||||
|
||||
$users = User::query()
|
||||
->with('pegawai:id,nama,nip')
|
||||
->orderByDesc('id')
|
||||
->limit($limit)
|
||||
->get(['id', 'name', 'email', 'role', 'nip', 'pegawai_id', 'created_at']);
|
||||
|
||||
return response()->json([
|
||||
'data' => $users->map(function (User $u) {
|
||||
return [
|
||||
'id' => $u->id,
|
||||
'name' => $u->name,
|
||||
'email' => $u->email,
|
||||
'role' => $u->role,
|
||||
'nip' => $u->nip,
|
||||
'pegawai_id' => $u->pegawai_id,
|
||||
'pegawai_nama' => $u->pegawai?->nama,
|
||||
'created_at' => $u->created_at,
|
||||
];
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'name' => ['nullable', 'string', 'max:100'],
|
||||
'email' => ['nullable', 'string', 'email', 'max:255', Rule::unique('users', 'email')],
|
||||
'password' => ['required', 'string', 'min:6'],
|
||||
'role' => ['nullable', 'string', Rule::in(['admin', 'kasir', 'gudang'])],
|
||||
'pegawai_id' => ['required', 'integer', 'exists:pegawai,id'],
|
||||
]);
|
||||
|
||||
$pegawai = \App\Models\Pegawai::find($data['pegawai_id']);
|
||||
if (!$pegawai) {
|
||||
return response()->json(['message' => 'Pegawai tidak ditemukan.'], 422);
|
||||
}
|
||||
|
||||
if (!$pegawai->nip) {
|
||||
return response()->json(['message' => 'Pegawai belum memiliki NIP.'], 422);
|
||||
}
|
||||
$nip = $pegawai->nip;
|
||||
if (User::query()->where('nip', $nip)->exists()) {
|
||||
return response()->json(['message' => 'NIP sudah digunakan.'], 422);
|
||||
}
|
||||
|
||||
$email = $data['email'] ?? null;
|
||||
if (!$email) {
|
||||
$email = $nip . '@pos.local';
|
||||
}
|
||||
|
||||
$user = User::create([
|
||||
'name' => $pegawai->nama ?? ($data['name'] ?? $nip),
|
||||
'email' => $email,
|
||||
'password' => Hash::make($data['password']),
|
||||
'role' => $data['role'] ?? User::ROLE_KASIR,
|
||||
'nip' => $nip,
|
||||
'pegawai_id' => $pegawai->id,
|
||||
]);
|
||||
|
||||
return response()->json(['data' => $user], 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, User $user): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'name' => ['nullable', 'string', 'max:100'],
|
||||
'email' => ['nullable', 'string', 'email', 'max:255', Rule::unique('users', 'email')->ignore($user->id)],
|
||||
'password' => ['nullable', 'string', 'min:6'],
|
||||
'role' => ['nullable', 'string', Rule::in(['admin', 'kasir', 'gudang'])],
|
||||
'pegawai_id' => ['nullable', 'integer', 'exists:pegawai,id'],
|
||||
]);
|
||||
|
||||
$pegawai = null;
|
||||
if (!empty($data['pegawai_id'])) {
|
||||
$pegawai = \App\Models\Pegawai::find($data['pegawai_id']);
|
||||
}
|
||||
|
||||
$nip = $pegawai?->nip ?? $user->nip;
|
||||
if ($pegawai && !$pegawai->nip) {
|
||||
return response()->json(['message' => 'Pegawai belum memiliki NIP.'], 422);
|
||||
}
|
||||
if (!$nip) {
|
||||
return response()->json(['message' => 'NIP wajib diisi.'], 422);
|
||||
}
|
||||
if (User::query()->where('nip', $nip)->where('id', '!=', $user->id)->exists()) {
|
||||
return response()->json(['message' => 'NIP sudah digunakan.'], 422);
|
||||
}
|
||||
|
||||
$email = $data['email'] ?? $user->email;
|
||||
if (!$email) {
|
||||
$email = $nip . '@pos.local';
|
||||
}
|
||||
|
||||
$payload = [
|
||||
'name' => $pegawai?->nama ?? ($data['name'] ?? $user->name),
|
||||
'email' => $email,
|
||||
'role' => $data['role'] ?? $user->role ?? User::ROLE_KASIR,
|
||||
'nip' => $nip,
|
||||
'pegawai_id' => $pegawai?->id ?? null,
|
||||
];
|
||||
|
||||
if (!empty($data['password'])) {
|
||||
$payload['password'] = Hash::make($data['password']);
|
||||
}
|
||||
|
||||
$user->update($payload);
|
||||
|
||||
return response()->json(['data' => $user]);
|
||||
}
|
||||
|
||||
public function destroy(User $user): JsonResponse
|
||||
{
|
||||
if ($user->role === User::ROLE_ADMIN) {
|
||||
return response()->json(['message' => 'User admin tidak bisa dihapus.'], 422);
|
||||
}
|
||||
|
||||
$user->delete();
|
||||
|
||||
return response()->json(['message' => 'User berhasil dihapus.']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user