45 lines
2.4 KiB
PHP
45 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Services\InvestmentRelationshipRepair;
|
|
use App\Services\InvestmentTerritorialTraceability;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
|
class InvestmentRelationshipRepairController extends Controller
|
|
{
|
|
public function preview(InvestmentRelationshipRepair $repair): Response
|
|
{
|
|
return Inertia::render('management/relationship-repair', [...$repair->preview(), 'traceability' => app(InvestmentTerritorialTraceability::class)->preview()]);
|
|
}
|
|
|
|
public function apply(Request $request, InvestmentRelationshipRepair $repair): RedirectResponse|JsonResponse
|
|
{
|
|
if ($request->input('operation') === 'traceability') {
|
|
$data = $request->validate(['hash' => ['required', 'string', 'size:64'], 'reverse' => ['required', 'boolean'],
|
|
'reason' => ['required_if:reverse,true', 'nullable', 'string', 'max:1000'], 'confirm_reverse' => ['accepted_if:reverse,true']]);
|
|
try {
|
|
$result = app(InvestmentTerritorialTraceability::class)->apply($data['hash'], $request->user()->id, $data['reverse'], $data['reason'] ?? null);
|
|
} catch (HttpException $exception) {
|
|
if ($request->expectsJson()) {
|
|
throw $exception;
|
|
}
|
|
|
|
return back()->withErrors(['operation' => $exception->getMessage()]);
|
|
}
|
|
|
|
return $request->expectsJson() ? response()->json($result) : back()->with('success', $data['reverse'] ? 'Rastreabilidade revertida, histórico preservado.' : 'Rastreabilidade aplicada; validações e dados territoriais preservados.');
|
|
}
|
|
$data = $request->validate(['hash' => ['required', 'string', 'size:64'], 'row_id' => ['nullable', 'string', 'size:64'],
|
|
'validation_hash' => ['required_with:row_id', 'nullable', 'string', 'size:64']]);
|
|
$result = $repair->apply($data['hash'], $request->user()->id, $data['row_id'] ?? null, $data['validation_hash'] ?? null);
|
|
|
|
return $request->expectsJson() ? response()->json($result) : back()->with('success', $result['replayed'] ? 'Este plano já foi aplicado; nenhum duplicado criado.' : 'Associações documentadas aplicadas. Registos de oportunidades permanecem preparados sem publicação automática.');
|
|
}
|
|
}
|