Document gen fixed
This commit is contained in:
@@ -23,15 +23,24 @@ public function __invoke(Request $request, Contract $contract): Response
|
||||
}
|
||||
$request->validate([
|
||||
'template_slug' => ['required', 'string', 'exists:document_templates,slug'],
|
||||
'template_version' => ['nullable', 'integer'],
|
||||
'custom' => ['nullable', 'array'],
|
||||
'custom.*' => ['nullable'],
|
||||
]);
|
||||
|
||||
$template = DocumentTemplate::where('slug', $request->template_slug)
|
||||
// Prefer explicitly requested version if provided and active; otherwise use latest active
|
||||
$baseQuery = DocumentTemplate::query()
|
||||
->where('slug', $request->template_slug)
|
||||
->where('core_entity', 'contract')
|
||||
->where('active', true)
|
||||
->orderByDesc('version')
|
||||
->firstOrFail();
|
||||
->where('active', true);
|
||||
if ($request->filled('template_version')) {
|
||||
$template = (clone $baseQuery)->where('version', (int) $request->integer('template_version'))->first();
|
||||
if (! $template) {
|
||||
$template = (clone $baseQuery)->orderByDesc('version')->firstOrFail();
|
||||
}
|
||||
} else {
|
||||
$template = $baseQuery->orderByDesc('version')->firstOrFail();
|
||||
}
|
||||
|
||||
// Load related data minimally
|
||||
$contract->load(['clientCase.client.person', 'clientCase.person', 'clientCase.client']);
|
||||
@@ -47,6 +56,16 @@ public function __invoke(Request $request, Contract $contract): Response
|
||||
'tokens' => $e->unresolved ?? [],
|
||||
], 422);
|
||||
} catch (\Throwable $e) {
|
||||
try {
|
||||
logger()->error('ContractDocumentGenerationController generation failed', [
|
||||
'template_id' => $template->id ?? null,
|
||||
'template_slug' => $template->slug ?? null,
|
||||
'template_version' => $template->version ?? null,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
} catch (\Throwable $logEx) {
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Generation failed.',
|
||||
@@ -115,6 +134,13 @@ public function __invoke(Request $request, Contract $contract): Response
|
||||
'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,
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user