diff --git a/app/Http/Controllers/APIS/AdminApi/UsersController.php b/app/Http/Controllers/APIS/AdminApi/UsersController.php index 212e83f..307e718 100644 --- a/app/Http/Controllers/APIS/AdminApi/UsersController.php +++ b/app/Http/Controllers/APIS/AdminApi/UsersController.php @@ -24,7 +24,7 @@ class UsersController extends Controller public function store(Request $request) { - // dd('test'); + try { $userData = [ 'email' => $request->email, @@ -73,7 +73,63 @@ class UsersController extends Controller return jsonResponseWithSuccessMessage(__('auth.data_fetched_successfully'), $response, 200); } catch (QueryException $e) { Log::error('Error in creating User ' . $e->getMessage()); - return jsonResponseWithErrorMessageApi(__('auth.authentication_failed'), 401); + return jsonResponseWithErrorMessageApi(__('auth.something went wrong'), 401); } } + + public function list() + { + try { + $users = $this->adminService->listUsers(); + + if (!empty($users['data'])) { + return response()->json([ + 'message' => 'Users fetched successfully', + 'users' => $users['data'] + ], 200); + } + + return response()->json(['message' => 'No users found'], 404); + } catch (Exception $e) { + return response()->json(['error' => 'Failed to fetch users', 'details' => $e->getMessage()], 500); + } + } + public function delete($userId) + { + try { + $response = $this->adminService->deleteUser($userId); + + if (isset($response['status']) && $response['status'] === 404) { + return response()->json([ + 'error' => "User with ID $userId not found in ThingsBoard", + 'response' => $response + ], 404); + } + + $user = User::find($userId); + + if ($user) { + $user->delete(); + } else { + return response()->json([ + 'error' => "User with ID $userId not found in local database" + ], 404); + } + + return response()->json([ + 'message' => 'User deleted successfully from ThingsBoard and local database', + 'response' => $response + ], 200); + + } catch (Exception $e) { + Log::error('Error in deleting User ' . $e->getMessage()); + + return response()->json([ + 'error' => 'Failed to delete user', + 'details' => $e->getMessage() + ], 500); + } + } + + } diff --git a/app/Http/Requests/CreateUserRequest.php b/app/Http/Requests/CreateUserRequest.php index 4b7e5a2..dbce996 100644 --- a/app/Http/Requests/CreateUserRequest.php +++ b/app/Http/Requests/CreateUserRequest.php @@ -6,12 +6,20 @@ use Illuminate\Foundation\Http\FormRequest; class CreateUserRequest extends FormRequest { - public function authorize() + /** + * Determine if the user is authorized to make this request. + */ + public function authorize(): bool { return true; } - public function rules() + /** + * Get the validation rules that apply to the request. + * + * @return array|string> + */ + public function rules(): array { return [ 'email' => 'required|email|unique:users,email', diff --git a/app/Services/AdminService.php b/app/Services/AdminService.php index cdde734..a8bdbd6 100644 --- a/app/Services/AdminService.php +++ b/app/Services/AdminService.php @@ -5,6 +5,7 @@ namespace App\Services; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Cache; use Exception; +use Illuminate\Support\Facades\Log; class AdminService { @@ -28,7 +29,8 @@ class AdminService $response = Http::withHeaders([ 'accept' => 'application/json', 'Content-Type' => 'application/json', - ])->post("{$this->baseUrl}/api/auth/login", [ + ]) + ->post("{$this->baseUrl}/api/auth/login", [ 'username' => $this->username, 'password' => $this->password, ]); @@ -45,13 +47,32 @@ class AdminService public function createUser(array $data) { $token = $this->getToken(); - + + + $payload = [ + 'email' => $data['email'] ?? 'default@example.com', + 'tenantId' => [ + 'id' => $data['tenant_id'] ?? '6e9b7fde-0ca0-4d19-9d2a-fba98e3e12a0', + 'entityType' => 'TENANT' + ], + 'customerId' => [ + 'id' => $data['customerId'], + 'entityType' => 'CUSTOMER' + ], + 'authority' => $data['authority'] ?? 'TENANT_ADMIN', + 'name' => $data['name'] ?? 'John Doe', + 'phone' => $data['phone'] ?? '1234567890', + 'additionalInfo' => [ + 'description' => $data['description'] ?? 'User description' + ] + ]; $response = Http::withHeaders([ 'Authorization' => "Bearer $token", 'accept' => 'application/json', 'Content-Type' => 'application/json', - ])->post(env('THINGSBOARD_CREATE_USER_URL'), $data); + ])->withBody(json_encode($payload), 'application/json') + ->post("{$this->baseUrl}/api/user"); if ($response->successful()) { return $response->json(); @@ -59,4 +80,47 @@ class AdminService throw new Exception('Failed to create user: ' . $response->body()); } } + + public function listUsers() +{ + $token = $this->getToken(); + + $response = Http::withHeaders([ + 'Authorization' => "Bearer $token", + 'accept' => 'application/json', + ])->get("{$this->baseUrl}/api/users?pageSize=100&page=0"); + + if ($response->successful()) { + return $response->json(); + } else { + throw new Exception('Failed to fetch users: ' . $response->body()); + } +} + + +public function deleteUser($userId) +{ + $token = $this->getToken(); + + $response = Http::withHeaders([ + 'Authorization' => "Bearer $token", + 'accept' => 'application/json', + 'Content-Type' => 'application/json', + ])->delete("{$this->baseUrl}/api/user/{$userId}"); + + // Handle ThingsBoard API errors + if ($response->failed()) { + Log::error('Failed to delete user: ' . $response->body()); + + // Return the ThingsBoard error message + return $response->json(); + } + + return $response->json(); +} + + + + + } diff --git a/routes/admin_api.php b/routes/admin_api.php index 34915c0..d9b239c 100644 --- a/routes/admin_api.php +++ b/routes/admin_api.php @@ -10,6 +10,5 @@ Route::get('/adminapi', function () { }); Route::post('/users-store', [UsersController::class, 'store'])->name('user_create'); -// Route::post('/users-store', function () { -// return ('Welcome to admin api routes.'); -// }); +Route::get('/users-list', [UsersController::class, 'list'])->name('user_list'); +Route::delete('/users-delete/{userId}', [UsersController::class, 'delete']);