diff --git a/app/Http/Controllers/Api/V1/Admin/AdminDashboardController.php b/app/Http/Controllers/Api/V1/Admin/AdminDashboardController.php index 869c52f4..5e820f8c 100644 --- a/app/Http/Controllers/Api/V1/Admin/AdminDashboardController.php +++ b/app/Http/Controllers/Api/V1/Admin/AdminDashboardController.php @@ -7,9 +7,48 @@ use App\Models\User; use App\Models\Product; use Illuminate\Http\Response; +use Illuminate\Http\Request; class AdminDashboardController extends Controller { + public function getUsers(Request $request) + { + // Get the 'status' and 'is_disabled' query parameters + $status = $request->query('status'); // For filtering by active or inactive status + $isDisabled = $request->query('is_disabled'); // For filtering by disabled status + $createdAtFrom = $request->query('created_at_from'); // Start date for filtering + $createdAtTo = $request->query('created_at_to'); // End date for filtering + + // Build the query + $query = User::select('id', 'name', 'email', 'is_active', 'created_at',) + ->orderBy('created_at', 'desc'); + + // Apply filters if provided + if ($status !== null) { + if ($status === 'true') { + $query->where('status', 'true'); + } elseif ($status === 'false') { + $query->where('status', 'false'); + } + } + + if ($isDisabled !== null) { + $isDisabled = filter_var($isDisabled, FILTER_VALIDATE_BOOLEAN); // Convert to boolean + $query->where('is_disabled', $isDisabled); + } + + if ($createdAtFrom) { + $query->where('created_at', '>=', $createdAtFrom); + } + + if ($createdAtTo) { + $query->where('created_at', '<=', $createdAtTo); + } + // Paginate results + $users = $query->paginate(15); + + return response()->json($users); + } public function getStatistics() { $currentMonth = now()->startOfMonth(); diff --git a/app/Http/Controllers/Api/V1/Admin/FaqController.php b/app/Http/Controllers/Api/V1/Admin/FaqController.php index ed356ca0..d97f8246 100644 --- a/app/Http/Controllers/Api/V1/Admin/FaqController.php +++ b/app/Http/Controllers/Api/V1/Admin/FaqController.php @@ -49,7 +49,8 @@ public function store(Request $request) } } - public function index() + + public function index() { try { $faqs = Faq::all()->map(function ($faq) { @@ -77,7 +78,7 @@ public function index() ], 500); } } - + public function update(Request $request, $id) { try { @@ -124,7 +125,7 @@ public function update(Request $request, $id) } } - public function destroy($id) +public function destroy($id) { try { $faq = Faq::findOrFail($id); diff --git a/app/Http/Controllers/Api/V1/User/ProfileController.php b/app/Http/Controllers/Api/V1/User/ProfileController.php index 365937fe..d668a2f4 100755 --- a/app/Http/Controllers/Api/V1/User/ProfileController.php +++ b/app/Http/Controllers/Api/V1/User/ProfileController.php @@ -40,10 +40,47 @@ public function store(Request $request) /** * Display the specified resource. */ - public function show(string $id) + public function show($id) { - // + try { + $user = User::with('profile')->findOrFail($id); + $profile = $user->profile; + + return response()->json([ + 'status_code' => 200, + 'message' => 'Successfully fetched profile', + 'data' => [ + 'id' => $user->id, + 'created_at' => $user->created_at->toIso8601String(), + 'updated_at' => $user->updated_at->toIso8601String(), + 'username' => $user->name ?? '', + 'jobTitle' => $profile->job_title ?? null, + 'pronouns' => $profile->pronoun ?? null, + 'department' => null, + 'email' => $user->email, + 'bio' => $profile->bio ?? null, + 'social_links' => null, + 'language' => null, + 'region' => null, + 'timezones' => null, + 'profile_pic_url' => $profile->avatar_url ?? null, + 'deletedAt' => $user->deleted_at ? $user->deleted_at->toIso8601String() : null, + 'avatar_url' => $profile->avatar_url ?? null, + ] + ], 200); + } catch (ModelNotFoundException $e) { + return response()->json([ + 'status_code' => 404, + 'message' => 'Profile not found', + ], 404); + } catch (\Exception $e) { + return response()->json([ + 'status_code' => 500, + 'message' => 'An unexpected error occurred while processing your request.', + ], 500); + } } + /** * Update the specified resource in storage. diff --git a/routes/api.php b/routes/api.php index fdf6a30a..61d1510f 100755 --- a/routes/api.php +++ b/routes/api.php @@ -167,8 +167,15 @@ Route::post('/email-templates', [EmailTemplateController::class, 'store']); Route::patch('/email-templates/{id}', [EmailTemplateController::class, 'update']); Route::delete('/email-templates/{id}', [EmailTemplateController::class, 'destroy']); + + }); + Route::middleware(['auth:api', 'admin'])->group(function () { + + // Dashboard + Route::get('/users-list', [AdminDashboardController::class, 'getUsers']); + }); Route::post('/email-requests', [SendEmailController::class, 'createEmailRequest']); @@ -231,6 +238,8 @@ //profile Update Route::patch('/profile', [ProfileController::class, 'update']); Route::post('/profile/upload-image', [ProfileController::class, 'uploadImage']); + Route::get('/profile/{id}', [ProfileController::class, 'show']); + //Timezone Settings @@ -321,13 +330,12 @@ Route::group(['middleware' => ['auth.jwt', 'superadmin']], function () { Route::post('/faqs', [FaqController::class, 'store']); - Route::put('/faqs/{id}', [FaqController::class, 'update']); - Route::delete('/faqs/{id}', [FaqController::class, 'destroy']); + Route::put('/faqs/{id}', [FaqController::class, 'update']); + Route::delete('/faqs/{id}', [FaqController::class, 'destroy']); }); - - Route::get('/faqs', [FaqController::class, 'index']); + Route::get('/faqs', [FaqController::class, 'index']); Route::post('/payment/stripe', [PaymentController::class, 'processPayment']); Route::get('/payment-success//{organisation_id}/{id}', [PaymentController::class, 'paymentSuccess'])->name('payment.success'); Route::get('/payment-cancel', [PaymentController::class, 'paymentCancel'])->name('payment.cancel'); -}); \ No newline at end of file +}); diff --git a/tests/Feature/AdminDashboardTest.php b/tests/Feature/AdminDashboardTest.php new file mode 100644 index 00000000..a5ab61a8 --- /dev/null +++ b/tests/Feature/AdminDashboardTest.php @@ -0,0 +1,93 @@ +admin = User::create([ + 'id' => (string) \Illuminate\Support\Str::uuid(), + 'username' => 'admin_user', + 'email' => 'admin@example.com', + 'role' => 'admin', + 'avatar_url' => 'https://example.com/avatar.jpg', + 'invite_link' => 'https://example.com/invite/admin_user', + 'status' => true, + 'is_disabled' => false, + 'gender' => 'male', + 'dob' => '1980-01-01', + 'password' => Hash::make('password123'), + 'email_verified_at' => now(), + 'created_at' => now(), + 'updated_at' => now(), + ]); + + // Seed the database with a regular user + $this->regularUser = User::create([ + 'id' => (string) \Illuminate\Support\Str::uuid(), + 'username' => 'regular_user', + 'email' => 'user@example.com', + 'role' => 'user', + 'avatar_url' => 'https://example.com/avatar.jpg', + 'invite_link' => 'https://example.com/invite/regular_user', + 'status' => true, + 'is_disabled' => false, + 'gender' => 'female', + 'dob' => '1995-01-01', + 'password' => Hash::make('password123'), + 'email_verified_at' => now(), + 'created_at' => now(), + 'updated_at' => now(), + ]); + + // Generate tokens for admin and regular user + $this->adminToken = JWTAuth::fromUser($this->admin); + $this->userToken = JWTAuth::fromUser($this->regularUser); + } + + public function test_admin_can_get_all_users() + { + $response = $this->withHeaders(['Authorization' => "Bearer $this->adminToken"]) + ->getJson('/api/v1/users-list'); + $response->assertStatus(200) + ->assertJsonStructure([ + 'data' => [ + '*' => [ + 'id', + 'name', + 'email', + 'is_active', + 'created_at', + ] + ] + ]); + + } + + public function test_non_admin_cannot_get_all_users() + { + $response = $this->withHeaders(['Authorization' => "Bearer $this->userToken"]) + ->getJson('/api/v1/users-list'); + + $response->assertStatus(401); + } + +}