88 lines
2.9 KiB
PHP
88 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Enums\ReportStatus;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Report;
|
|
use App\Notifications\ReportResolvedNotificationToBroker;
|
|
use App\Notifications\ReportRejectedNotificationToUser;
|
|
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();
|
|
|
|
$report->user->notify(new ReportRejectedNotificationToUser($report->deals()->first()->title, false));
|
|
$report->deals()->first()->broker->notify(new ReportResolvedNotificationToBroker($report->deals()->first()->title,
|
|
false));
|
|
|
|
return back()->with('success', 'Report resolved successfully.');
|
|
} catch (\Throwable $e) {
|
|
\Log::error('Error resolving report', [$report->id, $e->getMessage()]);
|
|
|
|
return back()->with('error', 'Something went wrong.');
|
|
}
|
|
}
|
|
|
|
public function reject(Report $report)
|
|
{
|
|
try {
|
|
$report->status = ReportStatus::Rejected;
|
|
$report->save();
|
|
|
|
$report->user->notify(new ReportRejectedNotificationToUser($report->deals()->first()->title));
|
|
|
|
return back()->with('success', 'Report Rejected successfully.');
|
|
} catch (\Throwable $e) {
|
|
\Log::error('Error rejecting report', [$report->id, $e->getMessage()]);
|
|
|
|
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) {
|
|
|
|
$report->user->notify(new ReportRejectedNotificationToUser($report->deals()->first()->title, true));
|
|
$report->deals()->first()->broker->notify(new ReportResolvedNotificationToBroker($report->deals()->first()->title,
|
|
true));
|
|
|
|
$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.');
|
|
}
|
|
}
|
|
}
|