updated document
This commit is contained in:
@@ -13,11 +13,15 @@
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Str;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
|
||||
class ContractDocumentGenerationController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request, Contract $contract): Response
|
||||
public function __invoke(Request $request, Contract $contract): Response|RedirectResponse
|
||||
{
|
||||
// Inertia requests include the X-Inertia header and should receive redirects or Inertia responses, not JSON
|
||||
$isInertia = (bool) $request->header('X-Inertia');
|
||||
$wantsJson = ! $isInertia && ($request->expectsJson() || $request->wantsJson());
|
||||
if (Gate::denies('read')) { // baseline read permission required to generate
|
||||
abort(403);
|
||||
}
|
||||
@@ -50,11 +54,18 @@ public function __invoke(Request $request, Contract $contract): Response
|
||||
// For custom tokens: pass overrides via request bag; service already reads request()->input('custom') if present.
|
||||
$result = $renderer->render($template, $contract, Auth::user());
|
||||
} catch (\App\Services\Documents\Exceptions\UnresolvedTokensException $e) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Unresolved tokens detected.',
|
||||
'tokens' => $e->unresolved ?? [],
|
||||
], 422);
|
||||
if ($wantsJson) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Unresolved tokens detected.',
|
||||
'tokens' => $e->unresolved ?? [],
|
||||
], 500);
|
||||
}
|
||||
|
||||
// Return back with validation-like errors so Inertia can surface them via onError
|
||||
return back()->withErrors([
|
||||
'document' => 'Unresolved tokens detected.',
|
||||
])->with('unresolved_tokens', $e->unresolved ?? []);
|
||||
} catch (\Throwable $e) {
|
||||
try {
|
||||
logger()->error('ContractDocumentGenerationController generation failed', [
|
||||
@@ -66,10 +77,16 @@ public function __invoke(Request $request, Contract $contract): Response
|
||||
} catch (\Throwable $logEx) {
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Generation failed.',
|
||||
], 500);
|
||||
if ($wantsJson) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Generation failed.',
|
||||
], 500);
|
||||
}
|
||||
|
||||
return back()->withErrors([
|
||||
'document' => 'Generation failed.',
|
||||
]);
|
||||
}
|
||||
|
||||
$doc = new Document;
|
||||
@@ -130,16 +147,31 @@ public function __invoke(Request $request, Contract $contract): Response
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'status' => 'ok',
|
||||
'document_uuid' => $doc->uuid,
|
||||
'path' => $doc->path,
|
||||
'stats' => $result['stats'] ?? null,
|
||||
'template' => [
|
||||
'id' => $template->id,
|
||||
'slug' => $template->slug,
|
||||
'version' => $template->version,
|
||||
'file_path' => $template->file_path,
|
||||
if ($wantsJson) {
|
||||
return response()->json([
|
||||
'status' => 'ok',
|
||||
'document_uuid' => $doc->uuid,
|
||||
'path' => $doc->path,
|
||||
'stats' => $result['stats'] ?? null,
|
||||
'template' => [
|
||||
'id' => $template->id,
|
||||
'slug' => $template->slug,
|
||||
'version' => $template->version,
|
||||
'file_path' => $template->file_path,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
// Flash some lightweight info if needed by the UI; Inertia will GET the page after redirect
|
||||
return back()->with([
|
||||
'doc_generated' => [
|
||||
'uuid' => $doc->uuid,
|
||||
'path' => $doc->path,
|
||||
'template' => [
|
||||
'slug' => $template->slug,
|
||||
'version' => $template->version,
|
||||
],
|
||||
'stats' => $result['stats'] ?? null,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user