Merge pull request 'sneha' (#17) from sneha into main
Reviewed-on: Nikhil.Kadam/vib360#17
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'email' => 'required|email|unique:users,email',
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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']);
|
||||
|
||||
Reference in New Issue
Block a user