diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index 9fb1acc..930fda7 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use App\Models\Setting; use App\Models\User; +use Illuminate\Http\JsonResponse; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Hash; @@ -128,15 +129,18 @@ class UserController extends Controller /** * Kirim akun (link + username + password baru) ke nomor HP user via WhatsApp. * Password lama tidak bisa dikirim (tersimpan hash) — jadi dibuatkan password baru. + * + * Jika request AJAX/JSON → kembalikan URL wa.me (aplikasi tetap terbuka, + * WhatsApp dibuka di tab baru). Jika tidak → redirect away (fallback). */ - public function sendWa(User $user): RedirectResponse + public function sendWa(Request $request, User $user): RedirectResponse|JsonResponse { if ($user->id === auth()->id()) { - return back()->with('error', 'Tidak perlu mengirim akun ke diri sendiri.'); + return $this->waResponse($request, null, 'Tidak perlu mengirim akun ke diri sendiri.'); } if (blank($user->phone)) { - return back()->with('error', 'Nomor HP belum diisi untuk ' . $user->name . ' — isi dulu di form edit user.'); + return $this->waResponse($request, null, 'Nomor HP belum diisi untuk ' . $user->name . ' — isi dulu di form edit user.'); } // Generate password baru yang mudah dibaca (8 karakter, tanpa karakter ambigu) @@ -162,6 +166,30 @@ class UserController extends Controller 'Mohon segera login dan ganti password Anda. Terima kasih.', ]); - return redirect()->away('https://wa.me/'.$user->phone.'?text='.rawurlencode($message)); + $waUrl = 'https://wa.me/'.$user->phone.'?text='.rawurlencode($message); + + return $this->waResponse($request, $waUrl, null, [ + 'name' => $user->name, + 'username' => $user->username, + 'password' => $password, + 'app_name' => $appName, + 'app_url' => $appUrl, + ]); + } + + protected function waResponse(Request $request, ?string $url, ?string $error, array $extra = []): RedirectResponse|JsonResponse + { + if ($request->expectsJson() || $request->ajax()) { + return response()->json(array_merge( + $extra, + ['url' => $url, 'error' => $error] + ), $error ? 422 : 200); + } + + if ($error) { + return back()->with('error', $error); + } + + return redirect()->away($url); } } diff --git a/resources/views/users/index.blade.php b/resources/views/users/index.blade.php index dcc1c89..2c7fb89 100644 --- a/resources/views/users/index.blade.php +++ b/resources/views/users/index.blade.php @@ -367,9 +367,45 @@ function sendWaData(userId, userName, hasPhone) { showCancelButton: true, cancelButtonText: 'Batal', }).then((result) => { - if (result.isConfirmed && form) { - form.submit(); - } + if (!result.isConfirmed || !form) return; + + fetch(form.action, { + method: 'POST', + headers: { + 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content, + 'Accept': 'application/json', + }, + }) + .then(r => r.json().catch(() => ({ error: 'Respons tidak valid dari server' }))) + .then(data => { + if (data.error) { + Swal.fire({ icon: 'error', title: 'Gagal', text: data.error }); + return; + } + const msgLines = [ + '📲 Akun Aplikasi ' + (data.app_name || '') + '', + 'Yth. ' + (data.name || userName) + ',', + 'Link: ' + (data.app_url || ''), + 'Username: ' + (data.username || '') + '', + 'Password: ' + (data.password || '') + '', + 'Mohon segera ganti password setelah login.', + ].join('
'); + Swal.fire({ + icon: 'success', + title: 'Akun Siap Dikirim', + html: '
' + msgLines + '
' + + '' + + ' Buka WhatsApp' + + '

WhatsApp dibuka di tab baru — aplikasi tetap terbuka.

', + showConfirmButton: false, + showCloseButton: true, + }); + }) + .catch(() => { + Swal.fire({ icon: 'error', title: 'Gagal', text: 'Tidak dapat menghubungi server.' }); + }); }); } }; diff --git a/tests/Feature/UserManagementTest.php b/tests/Feature/UserManagementTest.php index 13a4a97..484fb28 100644 --- a/tests/Feature/UserManagementTest.php +++ b/tests/Feature/UserManagementTest.php @@ -100,4 +100,26 @@ class UserManagementTest extends TestCase $this->assertFalse(str_contains(session('error'), 'wa.me')); } + + public function test_kirim_akun_via_json_mengembalikan_url_dan_aplikasi_tetap_terbuka(): void + { + $user = User::factory()->create([ + 'roles' => ['bk'], + 'phone' => '081234567890', + 'username' => 'guruwa2', + 'password' => 'passwordlama', + ]); + + $response = $this->actingAs($this->admin) + ->post("/users/{$user->id}/send-wa", [], ['Accept' => 'application/json']); + + $response->assertOk() + ->assertJsonStructure(['url', 'password', 'username']) + ->assertJsonPath('username', 'guruwa2'); + + $url = $response->json('url'); + $this->assertStringStartsWith('https://wa.me/081234567890?text=', $url); + $this->assertStringContainsString('guruwa2', urldecode($url)); + $this->assertStringContainsString($response->json('password'), urldecode($url)); + } }