dealhub/app/Http/Controllers/Admin/DealController.php

59 lines
1.6 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Actions\SendDealCreatedNotificationCustomerAction;
use App\Http\Controllers\Controller;
use App\Models\Deal;
use Illuminate\Support\Facades\Log;
class DealController extends Controller
{
public function index()
{
return view('dashboards.admin.deals.index')
->with('pendingDeals', $this->pendingDeals())
->with('activeDeals', $this->activeDeals());
}
public function approve(Deal $deal, SendDealCreatedNotificationCustomerAction $notificationCustomerAction)
{
try {
\DB::transaction(function () use ($deal) {
$deal->active = true;
$deal->save();
});
$notificationCustomerAction->execute($deal);
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 pendingDeals()
{
return Deal::where('active', false)->get();
}
private function activeDeals()
{
return Deal::where('active', true)->get();
}
}