feat: API de reportes, fix distancia espacial y protección OneSignal
- GET/POST /api/contracts/reports y /api/postulations/reports
- GET/POST /api/contracts/reports/{id}/comments con verificación de autoría
- Fix distancia withinDistanceTo: 0.5 grados → 5000 metros (SRID 4326)
- Fix FK Report::finishedcontracts() → contract_id
- Wrap todas las llamadas OneSignal en try/catch para evitar crashes
- Chat UI de comentarios: burbujas por rol, botón veredicto fijo, estilos
- Botón veredicto movido de reports/index a reports/comments
- Ruta POST veredict y restructura reports/{id}/comments en web
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -108,7 +108,7 @@ class ReportController extends Controller
|
||||
|
||||
$report = Report::find($id);
|
||||
$report->veredict = strip_tags($request->veredict);
|
||||
$coupon->save();
|
||||
$report->save();
|
||||
|
||||
return redirect('reports');
|
||||
}
|
||||
@@ -138,6 +138,155 @@ class ReportController extends Controller
|
||||
return redirect('reports');
|
||||
}
|
||||
|
||||
public function getcomments(Request $request, $id)
|
||||
{
|
||||
$user = Auth::user();
|
||||
$report = Report::with('finishedcontracts.suppliers')->find($id);
|
||||
|
||||
if (!$report) {
|
||||
return response()->json(['message' => 'Reporte no encontrado'], 404);
|
||||
}
|
||||
|
||||
$contract = $report->finishedcontracts;
|
||||
$supplierUserId = $contract->suppliers->user_id ?? null;
|
||||
|
||||
if ($user->id !== $contract->user_id && $user->id !== $supplierUserId) {
|
||||
return response()->json(['message' => 'No autorizado'], 403);
|
||||
}
|
||||
|
||||
$comments = ReportComment::with('user')
|
||||
->where('report_id', $id)
|
||||
->orderBy('created_at', 'asc')
|
||||
->get()
|
||||
->map(function ($c) use ($contract, $supplierUserId) {
|
||||
$isSupplier = $c->user_id === $supplierUserId;
|
||||
$isClient = $c->user_id === $contract->user_id;
|
||||
return [
|
||||
'id' => $c->id,
|
||||
'sender_id' => $c->user_id,
|
||||
'sender_name' => $isSupplier
|
||||
? ($contract->suppliers->company_name ?? null)
|
||||
: ($c->user->name ?? null),
|
||||
'role_id' => $c->user->role_id ?? null,
|
||||
'sender_type' => !$isClient && !$isSupplier ? 'moderator' : null,
|
||||
'comment' => $c->comment,
|
||||
'created_at' => $c->created_at,
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json($comments);
|
||||
}
|
||||
|
||||
public function storecomment(Request $request, $id)
|
||||
{
|
||||
$validator = Validator::make($request->all(), ['comment' => 'required|string']);
|
||||
if ($validator->fails()) {
|
||||
return response()->json($validator->errors(), 422);
|
||||
}
|
||||
|
||||
$user = Auth::user();
|
||||
$report = Report::with('finishedcontracts.suppliers')->find($id);
|
||||
|
||||
if (!$report) {
|
||||
return response()->json(['message' => 'Reporte no encontrado'], 404);
|
||||
}
|
||||
|
||||
$contract = $report->finishedcontracts;
|
||||
$supplierUserId = $contract->suppliers->user_id ?? null;
|
||||
|
||||
if ($user->id !== $contract->user_id && $user->id !== $supplierUserId) {
|
||||
return response()->json(['message' => 'No autorizado'], 403);
|
||||
}
|
||||
|
||||
$comment = new ReportComment();
|
||||
$comment->report_id = $id;
|
||||
$comment->user_id = $user->id;
|
||||
$comment->comment = strip_tags($request->comment);
|
||||
$comment->save();
|
||||
|
||||
$supplierUserId = $contract->suppliers->user_id ?? null;
|
||||
$isSupplier = $user->id === $supplierUserId;
|
||||
$isClient = $user->id === $contract->user_id;
|
||||
|
||||
return response()->json([
|
||||
'id' => $comment->id,
|
||||
'sender_id' => $user->id,
|
||||
'sender_name' => $isSupplier ? ($contract->suppliers->company_name ?? null) : $user->name,
|
||||
'role_id' => $user->role_id,
|
||||
'sender_type' => !$isClient && !$isSupplier ? 'moderator' : null,
|
||||
'comment' => $comment->comment,
|
||||
'created_at' => $comment->created_at,
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function getsupplierreports(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
if (!$user->suppliers) {
|
||||
return response()->json(['success' => false, 'message' => 'No tienes un perfil de proveedor registrado'], 400);
|
||||
}
|
||||
|
||||
$contractIds = FinishedContracts::where('supplier_id', $user->suppliers->id)->pluck('id');
|
||||
|
||||
$reports = Report::with([
|
||||
'finishedcontracts.suppliers.user',
|
||||
'finishedcontracts.categories',
|
||||
])
|
||||
->whereIn('contract_id', $contractIds)
|
||||
->orderBy('created_at', 'desc')
|
||||
->get();
|
||||
|
||||
$data = $reports->map(function ($report) {
|
||||
$contract = $report->finishedcontracts;
|
||||
return [
|
||||
'id' => $report->id,
|
||||
'supplier' => $contract->suppliers->user->name ?? null,
|
||||
'company' => $contract->suppliers->company_name ?? null,
|
||||
'category' => $contract->categories->name ?? null,
|
||||
'en_category' => $contract->categories->en_name ?? null,
|
||||
'appointment' => $contract->appointment,
|
||||
'address' => $contract->address,
|
||||
'amount' => $contract->amount,
|
||||
'veredict' => $report->veredict,
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
public function getreports(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
$contractIds = FinishedContracts::where('user_id', $user->id)->pluck('id');
|
||||
|
||||
$reports = Report::with([
|
||||
'finishedcontracts.suppliers.user',
|
||||
'finishedcontracts.categories',
|
||||
])
|
||||
->whereIn('contract_id', $contractIds)
|
||||
->orderBy('created_at', 'desc')
|
||||
->get();
|
||||
|
||||
$data = $reports->map(function ($report) {
|
||||
$contract = $report->finishedcontracts;
|
||||
return [
|
||||
'id' => $report->id,
|
||||
'supplier' => $contract->suppliers->user->name ?? null,
|
||||
'company' => $contract->suppliers->company_name ?? null,
|
||||
'category' => $contract->categories->name ?? null,
|
||||
'en_category' => $contract->categories->en_name ?? null,
|
||||
'appointment' => $contract->appointment,
|
||||
'address' => $contract->address,
|
||||
'amount' => $contract->amount,
|
||||
'veredict' => $report->veredict,
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
public function report(Request $request) {
|
||||
|
||||
$rules = [
|
||||
|
||||
Reference in New Issue
Block a user