Skip to content

Commit

Permalink
received new changesclear
Browse files Browse the repository at this point in the history
  • Loading branch information
NgBlaze committed Aug 8, 2024
2 parents 309b4a7 + 7ccfa5b commit 3f4e0ee
Show file tree
Hide file tree
Showing 12 changed files with 435 additions and 10 deletions.
40 changes: 35 additions & 5 deletions app/Http/Controllers/Api/V1/Admin/FaqController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,52 @@
use App\Http\Controllers\Controller;
use App\Models\Faq;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Validator;


class FaqController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
public function index(Request $request)
{
$faq = Faq::where('status', 1)->get();
$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);

return response()->json([
'status_code' => 200,
'message' => "Faq returned successfully",
'data' => $faq
]);
'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(),
],
], Response::HTTP_OK);
}

/**
Expand Down Expand Up @@ -70,7 +101,6 @@ public function update(Request $request, Faq $faq)
'message' => "Faq updated successfully",
'data' => $faq
], 200);

}

/**
Expand Down
87 changes: 87 additions & 0 deletions app/Http/Controllers/Api/V1/Auth/SocialAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Validator;

class SocialAuthController extends Controller
{
Expand Down Expand Up @@ -111,6 +112,92 @@ public function saveGoogleRequest(Request $request)
return response()->json($response);
}

public function saveGoogleRequestPost(Request $request)
{
// Validate the incoming request
$validator = Validator::make($request->all(), [
'token' => 'required|string',
'email' => 'required|email:rfc',
]);

if ($validator->fails()) {
return response()->json([
'status_code' => 422,
'message' => $validator->errors()
], 422);
}

// Extract Google user data from the request
$google_token = $request->input('token');
$email = $request->input('email');


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

// Check if the token email matches the email provided
if ($googleUser->email !== $email) {
return response()->json([
'status_code' => 400,
"error" => "Bad Request",
'message' => 'Invalid Google token'
], 401);
}

// Create or update the user
$user = User::updateOrCreate(
['email' => $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' => [
'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,
]
]
], 200);
} catch (Exception $e) {
return response()->json([
'status_code' => 500,
'message' => $e->getMessage()
], 500);
}
}

public function loginUsingFacebook()
{
return Socialite::driver('facebook')->stateless()->redirect();
Expand Down
72 changes: 72 additions & 0 deletions app/Http/Controllers/Api/V1/LanguageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace App\Http\Controllers\Api\v1;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;

use App\Models\Language;

class LanguageController extends Controller
{
public function create(Request $request)
{
if (!auth()->check()) {
return response()->json([
'status' => 401,
'message' => 'Unauthorized'
], 401);
}

$validator = Validator::make($request->all(), [
'language' => 'required|string|unique:languages',
'code' => 'required|string|unique:languages',
'description' => 'string|nullable',
]);

if ($validator->fails()) {
return response()->json([
'status' => 400,
'message' => 'Validation Error',
'errors' => $validator->errors()
], 400);
}

$language = Language::create([
'id' => Str::uuid(),
'language' => $request->language,
'code' => $request->code,
'description' => $request->description,
]);

return response()->json([
'status' => 201,
'message' => 'Language Created Successfully',
'language' => $language
], 201);
}

public function index()
{
// if (!auth()->check()) {
// return response()->json([
// 'status' => 401,
// 'message' => 'Unauthorized'
// ], 401);
// }



$languages = Language::select('language', 'code')->get();

return response()->json([
'status' => 200,
'message' => 'Languages fetched successfully',
'languages' => $languages
], 200);
}
}

34 changes: 32 additions & 2 deletions app/Http/Controllers/Api/V1/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public function __construct(PaymentService $paymentService)

public function initiatePaymentForPayStack(Request $request)
{
if (!auth()->check()) {
return response()->json([
'status_code' => 401,
'message' => 'Unauthorized'
], 401);
}
// return response()->json(['h'=> 'ng']);
$validator = Validator::make($request->all(), [
'organisation_id' => 'required',
Expand Down Expand Up @@ -114,9 +120,17 @@ public function handlePaystackCallback($organisation_id, $id, Request $request)
}

$payment->save();
$user = Organisation::find($organisation_id)->first();
if(!$user) {
return response()->json([
'status' => 404,
'message' => 'User Not found'
], 404);
}
$user_id = $user->id;

$userSubscription = new UserSubscription;
$userSubscription->user_id = auth()->user()->id;
$userSubscription->user_id = $user_id;
$userSubscription->subscription_plan_id = $id;
$userSubscription->org_id = $organisation_id;
$userSubscription->save();
Expand All @@ -134,6 +148,13 @@ public function handlePaystackCallback($organisation_id, $id, Request $request)

public function initiatePaymentForFlutterWave(Request $request)
{
if (!auth()->check()) {
return response()->json([
'status_code' => 401,
'message' => 'Unauthorized'
], 401);
}

$validator = Validator::make($request->all(), [
'organisation_id' => 'required',
'plan_id' =>'required',
Expand Down Expand Up @@ -229,8 +250,17 @@ public function handleFlutterwaveCallback($organisation_id, $id, Request $reques
}

$payment->save();
$user = Organisation::find($organisation_id)->first();
if(!$user) {
return response()->json([
'status' => 404,
'message' => 'User Not found'
], 404);
}
$user_id = $user->id;

$userSubscription = new UserSubscription;
$userSubscription->user_id = auth()->user()->id;
$userSubscription->user_id = $user_id;
$userSubscription->subscription_plan_id = $id;
$userSubscription->org_id = $organisation_id;
$userSubscription->save();
Expand Down
49 changes: 49 additions & 0 deletions app/Http/Controllers/Api/V1/User/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace App\Http\Controllers\Api\V1\User;

use App\Http\Controllers\Controller;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class DashboardController extends Controller
{
Expand Down Expand Up @@ -106,6 +109,52 @@ public function index()
]);
}

public function recent_sales()
{
$user = auth()->user();
$orders = Order::whereHas('product', function ($query) use ($user) {
$query->where('user_id', $user->id);
})->with('user')->get();

return response()->json([
'message' => 'Recent sales retrieved successfully',
'status_code' => Response::HTTP_OK,
'data' => $orders,
]);
}

public function user_analytics()
{
$user = Auth::user();

$monthlyRevenue = Order::whereHas('product', function ($query) use ($user) {
$query->where('user_id', $user->id);
})
->select(
DB::raw('EXTRACT(MONTH FROM created_at) as month'),
DB::raw('SUM(total_amount) as total_revenue')
)
->groupBy('month')
->orderBy('month')
->get()
->pluck('total_revenue', 'month')
->toArray();

$revenueByMonth = collect(range(1, 12))->mapWithKeys(function ($month) use ($monthlyRevenue) {
$monthName = date('M', mktime(0, 0, 0, $month, 1));
$revenue = $monthlyRevenue[$month] ?? 0;
return [$monthName => $revenue];
});

return response()->json([
'message' => 'User analytics retrieved successfully',
'status_code' => Response::HTTP_OK,
'data' => [
$revenueByMonth,
]
]);
}

/**
* Show the form for creating a new resource.
*/
Expand Down
11 changes: 11 additions & 0 deletions app/Models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Order extends Model
{
use HasUuids;
use HasFactory;

public function product(): BelongsTo
{
return $this->belongsTo(Product::class, 'product_id');
}

public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
}
Loading

0 comments on commit 3f4e0ee

Please sign in to comment.