Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…php_laravel_web into feat/delete_product
  • Loading branch information
Dev-Tonia committed Aug 7, 2024
2 parents 2becaee + bf1908e commit c9fda1f
Show file tree
Hide file tree
Showing 47 changed files with 45,974 additions and 2,539 deletions.
6,337 changes: 5,832 additions & 505 deletions .scribe/endpoints.cache/00.yaml

Large diffs are not rendered by default.

6,335 changes: 5,831 additions & 504 deletions .scribe/endpoints/00.yaml

Large diffs are not rendered by default.

Empty file added [
Empty file.
146 changes: 146 additions & 0 deletions app/Http/Controllers/Api/V1/Admin/DashboardController.php
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)
{
//
}
}
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);
}
}
21 changes: 21 additions & 0 deletions app/Http/Controllers/Api/V1/TimezoneController.php
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
]);
}

}
22 changes: 22 additions & 0 deletions app/Http/Controllers/QuestController.php
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);
}
}
66 changes: 66 additions & 0 deletions app/Http/Controllers/QuestMessageController.php
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)
{
//
}
}
28 changes: 28 additions & 0 deletions app/Http/Requests/StoreQuestMessageRequest.php
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 [
//
];
}
}
28 changes: 28 additions & 0 deletions app/Http/Requests/UpdateQuestMessageRequest.php
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 [
//
];
}
}
6 changes: 6 additions & 0 deletions app/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Product extends Model
{
Expand Down Expand Up @@ -53,4 +54,9 @@ public function organisation()
{
return $this->belongsTo(Organisation::class);
}

public function orders(): HasMany
{
return $this->hasMany(Order::class, 'product_id');
}
}
Loading

0 comments on commit c9fda1f

Please sign in to comment.