diff --git a/app/Http/Controllers/Admin/DealController.php b/app/Http/Controllers/Admin/DealController.php new file mode 100644 index 0000000..7ac9c00 --- /dev/null +++ b/app/Http/Controllers/Admin/DealController.php @@ -0,0 +1,49 @@ +with('deals', $this->deals()); + } + + public function approve(Deal $deal) + { + try { + \DB::transaction(function () use ($deal) { + $deal->active = true; + $deal->save(); + }); + + return back()->with('success', 'Deal activated successfully.'); + } catch (\Throwable $e) { + Log::error('Deal activation Failed: ', [$e->getMessage(), $e->getTrace()]); + + return back()->with('error', 'Something went wrong.'); + } + } + + public function reject(Deal $deal){ + try { + $deal->delete(); + + return back()->with('success', 'Deal deleted successfully.'); + } catch (\Throwable $e) { + Log::error('Deal deletion Failed: ', [$e->getMessage(), $e->getTrace()]); + + return back()->with('error', 'Something went wrong.'); + } + } + + private function deals() + { + return Deal::where('active', false)->get(); + } +} diff --git a/app/Http/Controllers/ExplorePageController.php b/app/Http/Controllers/ExplorePageController.php index 9cf1cb5..c2fddbf 100644 --- a/app/Http/Controllers/ExplorePageController.php +++ b/app/Http/Controllers/ExplorePageController.php @@ -31,6 +31,7 @@ public function __invoke( protected function deals(FormRequest $request, Builder $query, AddRecentSearchAction $action): LengthAwarePaginator { + $query->tap(fn ($q) => (new Deal)->withActiveDeals($q)); // Add a search query if ($request->has('search') && $request->get('search') !== null) { $query->tap(fn ($q) => (new Deal)->search($q, $request->search)); diff --git a/app/Queries/ExplorePageDealsQuery.php b/app/Queries/ExplorePageDealsQuery.php index 0f5f749..8c2728a 100644 --- a/app/Queries/ExplorePageDealsQuery.php +++ b/app/Queries/ExplorePageDealsQuery.php @@ -25,8 +25,7 @@ public function builder(): Builder ->with('type'); }, ]) - // Select only admin-approved deals - ->tap(fn ($q) => (new Deal)->withActiveDeals($q)) + // Check if the current user interacted with the deal ->tap(fn ($q) => (new Deal)->withCurrentUserInteractions($q)) ->tap(fn ($q) => (new Deal)->withLikePerDeal($q)) diff --git a/resources/js/admin-deals.js b/resources/js/admin-deals.js new file mode 100644 index 0000000..9e66cf3 --- /dev/null +++ b/resources/js/admin-deals.js @@ -0,0 +1,12 @@ +import {showDealModal} from "./deal-view-modal.js"; + +const table = document.querySelector('.ui-table') +if (table) { + const btns = table.querySelectorAll('.view-deal-btn'); + btns.forEach(btn => { + btn.addEventListener('click', async () => { + let dealId = btn.dataset.dealId; + await showDealModal(dealId) + }) + }) +} diff --git a/resources/views/components/dashboard/admin/sidebar.blade.php b/resources/views/components/dashboard/admin/sidebar.blade.php index 0e60a0d..98186d1 100644 --- a/resources/views/components/dashboard/admin/sidebar.blade.php +++ b/resources/views/components/dashboard/admin/sidebar.blade.php @@ -37,6 +37,11 @@ class="flex flex-col p-4 pt-6 justify-between font-medium h-full w-full overflow + + + + + diff --git a/resources/views/dashboards/admin/deals/index.blade.php b/resources/views/dashboards/admin/deals/index.blade.php index ece8c7d..6290bbf 100644 --- a/resources/views/dashboards/admin/deals/index.blade.php +++ b/resources/views/dashboards/admin/deals/index.blade.php @@ -1,78 +1,53 @@ @php use App\Enums\ReportStatus; @endphp - +
-

Reported deals

-

Total Reports: {{$reports->count()}}

+

Approve deals

- Type + Title Description - Customer - Status - - Deal - - + Link + Category + Broker - @forelse($reports as $report) + @forelse($deals as $deal) - {{ucfirst($report->type->value)}} - {{$report->description}} - {{$report->user->name}} - - - - - - {{$report->deals()->first()->title}} - - + {{ucfirst($deal->title)}} + {{$deal->description}} + {{$deal->link}} + {{$deal->category->name}} + {{$deal->broker->name}}
- @if($report->status !== ReportStatus::Resolved) -
- @csrf - - - -
- @endif - @if($report->status !== ReportStatus::Rejected) -
- @csrf - - - -
- @endif -
@csrf - - + + + +
+ @csrf + + + +
+ + + +
@@ -84,4 +59,6 @@ class=" h-full items-center flex justify-center">
+ + @vite('resources/js/admin-deals.js')
diff --git a/routes/api/deals.php b/routes/api/deals.php index ea3872a..6d7e0ab 100644 --- a/routes/api/deals.php +++ b/routes/api/deals.php @@ -5,10 +5,14 @@ use App\Queries\ExplorePageDealsQuery; Route::get('/deals/{deal}', function (Deal $deal) { + $query = (new ExplorePageDealsQuery)->builder(); + if (! Auth::user()->isAdmin()) { + $query // Select only admin-approved deals + ->tap(fn ($q) => (new Deal)->withActiveDeals($q)); + } + $query->where('id', $deal->id); + return new DealResource( - (new ExplorePageDealsQuery) - ->builder() - ->where('id', $deal->id) - ->first() + $query->first() ); }); diff --git a/routes/web/admin.php b/routes/web/admin.php index 4c2c238..0c87cdf 100644 --- a/routes/web/admin.php +++ b/routes/web/admin.php @@ -4,6 +4,7 @@ use App\Http\Controllers\Admin\AdminDashboardController; use App\Http\Controllers\Admin\BrokerController; use App\Http\Controllers\Admin\CustomerController; +use App\Http\Controllers\Admin\DealController; use App\Http\Controllers\Admin\ReportController; use App\Http\Middleware\HasRole; @@ -22,4 +23,8 @@ Route::post('/reports/reject/{report}', [ReportController::class, 'reject'])->name('reports.reject'); Route::post('/reports/remove/{report}', [ReportController::class, 'removeContent'])->name('reports.remove-content'); + + Route::get('/deals', [DealController::class, 'index'])->name('deals.index'); + Route::post('/deals/approve/{deal}', [DealController::class, 'approve'])->name('deals.approve'); + Route::post('/deals/reject/{deal}', [DealController::class, 'reject'])->name('deals.reject'); });