-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' of https://github.com/hngprojects/hng_boilerplate_…
…php_laravel_web into feat/delete_product
- Loading branch information
Showing
47 changed files
with
45,974 additions
and
2,539 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
146 changes: 146 additions & 0 deletions
146
app/Http/Controllers/Api/V1/Admin/DashboardController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api\V1\Admin; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\Order; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
|
||
class DashboardController extends Controller | ||
{ | ||
/** | ||
* Display a listing of the resource. | ||
*/ | ||
public function index() | ||
{ | ||
$user = auth()->user(); | ||
|
||
$currentMonth = now()->startOfMonth(); | ||
$lastMonth = now()->subMonth()->startOfMonth(); | ||
$userProducts = $user->products(); | ||
$currentMonthRevenue = $userProducts | ||
->with(['orders' => function ($query) use ($currentMonth) { | ||
$query->where('created_at', '>=', $currentMonth); | ||
}]) | ||
->get() | ||
->flatMap(function ($product) { | ||
return $product->orders->map(function ($order) { | ||
return $order->quantity * $order->amount; | ||
}); | ||
})->sum(); | ||
|
||
$currentMonthOrders = $userProducts->withCount(['orders' => function ($query) use ($currentMonth) { | ||
$query->where('created_at', '>=', $currentMonth); | ||
}]) | ||
->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; | ||
}); | ||
})->sum(); | ||
|
||
$lastMonthOrders = $userProducts->withCount(['orders' => function ($query) use ($currentMonth, $lastMonth) { | ||
$query->whereBetween('created_at', [$lastMonth, $currentMonth]); | ||
}]) | ||
->get() | ||
->sum('orders_count'); | ||
|
||
$percentageDifference = 0; | ||
|
||
if ($lastMonthRevenue > 0 && $currentMonthRevenue > 0) { | ||
$percentageDifference = (($currentMonthRevenue - $lastMonthRevenue) / $lastMonthRevenue) * 100; | ||
} else if ($currentMonthRevenue > 0 && $lastMonthRevenue === 0) { | ||
$percentageDifference = 100; | ||
} | ||
|
||
$percentageDifferenceOrders = 0; | ||
if ($lastMonthOrders > 0 && $currentMonthOrders > 0) { | ||
$percentageDifferenceOrders = (($currentMonthOrders - $lastMonthOrders) / $lastMonthOrders) * 100; | ||
} else if ($currentMonthOrders > 0 && $lastMonthOrders === 0) { | ||
$percentageDifferenceOrders = 100; | ||
} | ||
|
||
$oneHourAgo = now()->subHour(); | ||
$activeUser = $user->owned_organisations()->with(['users' => function ($query) { | ||
$query->where('is_active', true); | ||
}])->count(); | ||
$activeUserAnHourAgo = $user->owned_organisations()->with(['users' => function ($query) use ($oneHourAgo) { | ||
$query->where([ | ||
['is_active', true], | ||
['modified_at', '>=', $oneHourAgo] | ||
] | ||
); | ||
}])->count(); | ||
|
||
return response()->json([ | ||
'message' => 'Dashboard retrieved successfully', | ||
'status_code' => Response::HTTP_OK, | ||
'data' => [ | ||
'current_month_revenue' => $currentMonthRevenue, | ||
'difference_in_revenue_percent' => $percentageDifference . '%', | ||
'subscriptions' => 0, | ||
'monthly_subscriptions_diff' => 0, | ||
'current_month_orders' => $currentMonthOrders, | ||
'difference_in_orders_percent' => $percentageDifferenceOrders . '%', | ||
'active_users_count' => $activeUser, | ||
'difference_an_hour_ago' => max(($activeUser - $activeUserAnHourAgo), 0), | ||
] | ||
]); | ||
} | ||
|
||
/** | ||
* Show the form for creating a new resource. | ||
*/ | ||
public function create() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Store a newly created resource in storage. | ||
*/ | ||
public function store(Request $request) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Display the specified resource. | ||
*/ | ||
public function show(string $id) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Show the form for editing the specified resource. | ||
*/ | ||
public function edit(string $id) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Update the specified resource in storage. | ||
*/ | ||
public function update(Request $request, string $id) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
*/ | ||
public function destroy(string $id) | ||
{ | ||
// | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
app/Http/Controllers/Api/V1/SuperAdmin/SuperAdminProductController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api\V1\SuperAdmin; | ||
|
||
use App\Http\Controllers\Controller; | ||
use Illuminate\Http\Request; | ||
use App\Models\Product; | ||
use Illuminate\Support\Facades\Validator; | ||
|
||
class SuperAdminProductController extends Controller | ||
{ | ||
public function store(Request $request) | ||
{ | ||
$validator = Validator::make($request->all(), [ | ||
'name' => 'required|string|max:255', | ||
'description' => 'required|string', | ||
'price' => 'required|numeric', | ||
'slug' => 'required|string|max:255', | ||
'tags' => 'required|string', | ||
'imageUrl' => 'nullable|string|max:255', | ||
'status' => 'required|string|max:50', | ||
'quantity' => 'required|integer', | ||
'org_id' => 'required|uuid', | ||
]); | ||
|
||
if ($validator->fails()) { | ||
return response()->json([ | ||
'success' => false, | ||
'status_code' => 422, | ||
'message' => 'Validation errors', | ||
'data' => $validator->errors(), | ||
], 422); | ||
} | ||
|
||
$product = Product::create([ | ||
'user_id' => auth()->id(), | ||
'name' => $request->name, | ||
'description' => $request->description, | ||
'price' => $request->price, | ||
'slug' => $request->slug, | ||
'tags' => $request->tags, | ||
'imageUrl' => $request->imageUrl, | ||
'status' => $request->status, | ||
'quantity' => $request->quantity, | ||
'is_archived' => false, | ||
'org_id' => $request->org_id, | ||
]); | ||
|
||
return response()->json([ | ||
'success' => true, | ||
'status_code' => 201, | ||
'message' => 'Product created successfully', | ||
'data' => $product, | ||
], 201); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api\V1; | ||
|
||
use App\Http\Controllers\Controller; | ||
use Illuminate\Http\Request; | ||
use App\Models\Timezone; | ||
|
||
class TimezoneController extends Controller | ||
{ | ||
public function index() | ||
{ | ||
$timezones = Timezone::all(); | ||
return response()->json([ | ||
'status' => 'success', | ||
'message' => 'Timezones retrieved successfully', | ||
'data' => $timezones | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Events\QuestUpdated; | ||
use App\Models\Quest; | ||
use App\Models\QuestMessage; | ||
use Illuminate\Http\Request; | ||
|
||
class QuestController extends Controller | ||
{ | ||
public function getQuestMessages($id) | ||
{ | ||
$quest = Quest::find($id); | ||
if (!$quest) { | ||
return response()->json(['message' => 'Quest not found'], 404); | ||
} | ||
|
||
$messages = $quest->messages; | ||
return response()->json($messages); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\StoreQuestMessageRequest; | ||
use App\Http\Requests\UpdateQuestMessageRequest; | ||
use App\Models\QuestMessage; | ||
|
||
class QuestMessageController extends Controller | ||
{ | ||
/** | ||
* Display a listing of the resource. | ||
*/ | ||
public function index() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Show the form for creating a new resource. | ||
*/ | ||
public function create() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Store a newly created resource in storage. | ||
*/ | ||
public function store(StoreQuestMessageRequest $request) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Display the specified resource. | ||
*/ | ||
public function show(QuestMessage $questMessage) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Show the form for editing the specified resource. | ||
*/ | ||
public function edit(QuestMessage $questMessage) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Update the specified resource in storage. | ||
*/ | ||
public function update(UpdateQuestMessageRequest $request, QuestMessage $questMessage) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
*/ | ||
public function destroy(QuestMessage $questMessage) | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class StoreQuestMessageRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
*/ | ||
public function authorize(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
// | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class UpdateQuestMessageRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
*/ | ||
public function authorize(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
// | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.