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

75 lines
2.2 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Enums\ReportStatus;
use App\Http\Controllers\Controller;
use App\Models\Report;
use Illuminate\Support\Facades\DB;
class ReportController extends Controller
{
public function index()
{
$reports = Report::query()
->with(['user:id,name', 'deals:id,title'])
->tap(fn ($q) => (new Report)->orderByStatus($q,
[ReportStatus::Pending, ReportStatus::Rejected, ReportStatus::Resolved]))
->get();
return view('dashboards.admin.reports.index')
->with('reports', $reports);
}
public function resolve(Report $report)
{
try {
$report->status = ReportStatus::Resolved;
$report->save();
return back()->with('success', 'Report resolved successfully.');
} catch (\Throwable $e) {
\Log::error('Error resolving report', [$report->id, $e->getMessage(), $e->getTraceAsString()]);
return back()->with('error', 'Something went wrong.');
}
}
public function reject(Report $report)
{
try {
$report->status = ReportStatus::Rejected;
$report->save();
return back()->with('success', 'Report Rejected successfully.');
} catch (\Throwable $e) {
\Log::error('Error rejecting report', [$report->id, $e->getMessage(), $e->getTraceAsString()]);
return back()->with('error', 'Something went wrong.');
}
}
/**
* Make the attached deal inactive and mark the report resolved.
*/
public function removeContent(Report $report)
{
try {
DB::transaction(function () use ($report) {
$deal = $report->deals()->first();
$deal->active = false;
$report->status = ReportStatus::Resolved;
$deal->save();
$report->save();
});
return back()->with('error', 'Deal has been deactivated.');
} catch (\Throwable $e) {
\Log::error('Error removing content of report', [$report->id, $e->getMessage(), $e->getTraceAsString()]);
return back()->with('error', 'Something went wrong.');
}
}
}