Skip to content

Commit

Permalink
Merge pull request #507 from hngprojects/dev
Browse files Browse the repository at this point in the history
updating staging
  • Loading branch information
timiajayi authored Aug 14, 2024
2 parents 638739b + 1873567 commit 8f7f2a7
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 10 deletions.
39 changes: 39 additions & 0 deletions app/Http/Controllers/Api/V1/Admin/AdminDashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 4 additions & 3 deletions app/Http/Controllers/Api/V1/Admin/FaqController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public function store(Request $request)
}
}

public function index()

public function index()
{
try {
$faqs = Faq::all()->map(function ($faq) {
Expand Down Expand Up @@ -77,7 +78,7 @@ public function index()
], 500);
}
}

public function update(Request $request, $id)
{
try {
Expand Down Expand Up @@ -124,7 +125,7 @@ public function update(Request $request, $id)
}
}

public function destroy($id)
public function destroy($id)
{
try {
$faq = Faq::findOrFail($id);
Expand Down
41 changes: 39 additions & 2 deletions app/Http/Controllers/Api/V1/User/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 13 additions & 5 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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');
});
});
93 changes: 93 additions & 0 deletions tests/Feature/AdminDashboardTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Tests\Feature;

use App\Models\User;
use App\Models\Role;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;
use Tymon\JWTAuth\Facades\JWTAuth;

class AdminDashboardTest extends TestCase
{
use RefreshDatabase;

protected $admin;
protected $regularUser;
protected $adminToken;
protected $userToken;

protected function setUp(): void
{
parent::setUp();

// Seed the database with an admin user
$this->admin = User::create([
'id' => (string) \Illuminate\Support\Str::uuid(),
'username' => 'admin_user',
'email' => '[email protected]',
'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' => '[email protected]',
'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);
}

}

0 comments on commit 8f7f2a7

Please sign in to comment.