Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…php_laravel_web into feat/fetch-a-role-in-an-organisation-endpoint
  • Loading branch information
sparkybug committed Aug 9, 2024
2 parents 7c37feb + bf7b32c commit f6c5e55
Show file tree
Hide file tree
Showing 19 changed files with 909 additions and 153 deletions.
46 changes: 14 additions & 32 deletions app/Http/Controllers/Api/V1/Admin/FaqController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,24 @@ class FaqController extends Controller
/**
* Display a listing of the resource.
*/
public function index(Request $request)
public function index()
{
$validator = Validator::make($request->all(), [
'page' => 'nullable|integer|min:1',
'size' => 'nullable|integer|min:1',
]);


if ($validator->fails()) {
return response()->json([
'success' => false,
'message' => 'Invalid input parameters.',
'errors' => $validator->errors(),
], Response::HTTP_BAD_REQUEST);
}

$perPage = $request->input('size', 15);

$faqs = Faq::where('status', 1)->paginate($perPage);
$faqs = Faq::where('status', 1)->get()->map(function ($faq) {
return [
'id' => $faq->id,
'question' => $faq->question,
'answer' => $faq->answer,
'category' => $faq->category,
'createdBy' => "ADMIN",
'createdAt' => $faq->created_at,
'updatedAt' => $faq->updated_at,
];
});

return response()->json([
'status_code' => 200,
'message' => "Faq returned successfully",
'data' => collect($faqs->items())->map(function ($faq) {
return [
'id' => $faq->id,
'question' => $faq->question,
'answer' => $faq->answer,
];
}),
'pagination' => [
'current_page' => $faqs->currentPage(),
'total_pages' => $faqs->lastPage(),
'page_size' => $faqs->perPage(),
'total_items' => $faqs->total(),
],
'message' => "Faq fetched successfully",
'data' => $faqs
], Response::HTTP_OK);
}

Expand Down
119 changes: 64 additions & 55 deletions app/Http/Controllers/Api/V1/Auth/SocialAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace App\Http\Controllers\Api\V1\Auth;

use Google_Client;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite;
use Illuminate\Support\Facades\Auth;
use Tymon\JWTAuth\Facades\JWTAuth;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Validator;

Expand Down Expand Up @@ -114,7 +116,7 @@ public function saveGoogleRequest(Request $request)

public function saveGoogleRequestPost(Request $request)
{
// Validate the incoming request working
// Validate the incoming request
$validator = Validator::make($request->all(), [
'id_token' => 'required|string',
]);
Expand All @@ -125,64 +127,71 @@ public function saveGoogleRequestPost(Request $request)
'message' => $validator->errors()
], 422);
}

// Extract Google user data from the request
$google_token = $request->id_token;

try {
// Retrieve user information from Google
$googleUser = Socialite::driver('google')->userFromToken($google_token);

// Create or update the user
$user = User::updateOrCreate(
['email' => $googleUser->email],
[
'password' => Hash::make(Str::random(12)), // Random password for social sign-ins
'social_id' => $googleUser->id,
'is_verified' => true,
'signup_type' => 'Google',
'is_active' => true,
]
);

// Handle profile update or creation
if ($user->profile) {
$user->profile->update([
'first_name' => $googleUser->user['given_name'],
'last_name' => $googleUser->user['family_name'],
'avatar_url' => $googleUser->user['picture'],
]);
} else {
$user->profile()->create([
'first_name' => $googleUser->user['given_name'],
'last_name' => $googleUser->user['family_name'],
'avatar_url' => $googleUser->user['picture'],
]);
}

// Generate JWT token
$token = JWTAuth::fromUser($user);

return response()->json([
'status_code' => 200,
'message' => 'User Created Successfully',
'access_token' => $token,
'data' => [
'user' => [
$idToken = $request->id_token;

$response = Http::get("https://www.googleapis.com/oauth2/v3/tokeninfo?id_token={$idToken}");
if($response->successful()) {
$payload = $response->json();

if (isset($payload['sub']) && isset($payload['email'])) {
$email = $payload['email'];
$firstName = $payload['given_name'];
$lastName = $payload['family_name'];
$avatarUrl = $payload['picture'] ?? null;

// Create or update user
$user = User::updateOrCreate(
['email' => $email],
[
'password' => Hash::make(Str::random(12)), // Generate a random password for the user
'social_id' => $idToken,
'is_verified' => true,
'signup_type' => 'Google',
'is_active' => true,
]
);

// Update or create user profile
if ($user->profile) {
$user->profile->update([
'first_name' => $firstName,
'last_name' => $lastName,
'avatar_url' => $avatarUrl,
]);
} else {
$user->profile()->create([
'first_name' => $firstName,
'last_name' => $lastName,
'avatar_url' => $avatarUrl,
]);
}

$token = JWTAuth::fromUser($user);

return response()->json([
'status_code' => 200,
'message' => 'User Created',
'access_token' => $token,
'data' => [
'id' => $user->id,
'email' => $user->email,
'first_name' => $googleUser->user['given_name'],
'last_name' => $googleUser->user['family_name'],
'fullname' => $googleUser->user['given_name'].' '.$googleUser->user['family_name'],
'role' => $user->role,
'first_name' => $firstName,
'last_name' => $lastName,
]
]
], 200);
} catch (Exception $e) {
]);
} else {
return response()->json([
'status_code' => 401,
'message' => 'Invalid Token Payload'
], 401);
}
} else {
return response()->json([
'status_code' => 500,
'message' => $e->getMessage()
], 500);
'status_code' => 401,
'message' => 'Invalid Token: ' . $response->body()
], 401);
}
}

Expand Down
35 changes: 32 additions & 3 deletions app/Http/Controllers/Api/V1/PreferenceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use App\Http\Requests\Preference\StorePreferenceRequest;
use App\Http\Requests\Preference\UpdatePreferenceRequest;
use App\Http\Requests\Preference\SavePreferencesRequest;
use App\Models\Preference;
use App\Models\Region;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

use App\Models\Preference;
use App\Models\Profile;

class PreferenceController extends Controller
Expand All @@ -30,7 +30,7 @@ public function index()
Log::info('Preferences retrieved', ['user_id' => Auth::id(), 'preferences' => $preferences]);
return response()->json([
'status_code' => 200,
'message' => 'Languages fetched successfully',
'message' => 'Preferences fetched successfully',
'preferences' => $preferences
], 200);
}
Expand Down Expand Up @@ -138,6 +138,8 @@ public function delete(DeletePreferenceRequest $request, $id)
{
$preference = Auth::user()->preferences()->find($id);



if (!$preference) {
return response()->json([
'status_code' => 404,
Expand Down Expand Up @@ -179,4 +181,31 @@ public function showRegion($user_id)
], 404);
}
}

//update the regionsss
public function updateRegion(Request $request, $user_id){
$request->validate([
'region_id' => 'required|uuid|exists:regions,id'
]);

$preference = Preference::where('user_id', $user_id)->first();

if(!$preference){
return response()->json([
'status'=> 404,
'message'=> 'Preference not found for user'
], 404);
}

$preference->region_id = $request->input('region_id');
$preference->save();

return response()->json([
'status' => 200,
'message' => 'Region updated successfully',
'data' => [
'region' => $preference->region,
],
]);
}
}
16 changes: 14 additions & 2 deletions app/Http/Controllers/Api/V1/SqueezePageCoontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Http\Controllers\Controller;
use App\Http\Requests\FilterSqueezeRequest;
use App\Http\Requests\DeleteSqueezeRequest;
use App\Models\SqueezePage;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
Expand Down Expand Up @@ -74,9 +75,20 @@ public function update(Request $request, string $id)
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
public function destroy(DeleteSqueezeRequest $request, string $squeeze_page)
{
//
try {
SqueezePage::findOrFail($squeeze_page)->delete();
return response()->json([
'message' => 'Squeeze Page deleted successfully',
'status' => Response::HTTP_OK,
]);
} catch (\Exception $e) {
return response()->json([
'message' => 'Internal server error',
'status' => Response::HTTP_INTERNAL_SERVER_ERROR
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}

public function search(Request $request)
Expand Down
10 changes: 4 additions & 6 deletions app/Http/Controllers/Api/V1/User/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function index()
->get()
->flatMap(function ($product) {
return $product->orders->map(function ($order) {
return $order->quantity * $order->amount;
return $order->total_amount;
});
})->sum();

Expand All @@ -37,15 +37,14 @@ public function index()
}])
->get()
->sum('orders_count');

$lastMonthRevenue = $userProducts
->with(['orders' => function ($query) use ($currentMonth, $lastMonth) {
$query->whereBetween('created_at', [$lastMonth, $currentMonth]);
}])
->get()
->flatMap(function ($product) {
return $product->orders->map(function ($order) {
return $order->quantity * $order->amount;
return $order->total_amount;
});
})->sum();

Expand Down Expand Up @@ -149,9 +148,8 @@ public function user_analytics()
return response()->json([
'message' => 'User analytics retrieved successfully',
'status_code' => Response::HTTP_OK,
'data' => [
$revenueByMonth,
]
'data' => $revenueByMonth,

]);
}

Expand Down
15 changes: 8 additions & 7 deletions app/Http/Controllers/Api/V1/User/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ public function stats()
[
"status_code" => 200,
"message" => "User statistics retrieved successfully",
"total_users" => $totalUsers,
"deleted_users" => $totalDeletedUsers,
"active_users" => $totalActiveUsers,
"in_active_users" => $totalInActiveUsers,
"data" => [
"total_users" => $totalUsers,
"deleted_users" => $totalDeletedUsers,
"active_users" => $totalActiveUsers,
"in_active_users" => $totalInActiveUsers,
]
],
200
);
Expand All @@ -34,14 +36,13 @@ public function stats()
*/
public function index()
{
$users = User::latest()->paginate();

$users = User::paginate();

return response()->json(
[
"status_code" => 200,
"message" => "Users returned successfully",
"data" =>$users
"data" => $users
],
200
);
Expand Down
Loading

0 comments on commit f6c5e55

Please sign in to comment.