diff --git a/app/Listeners/Document/CreateDocumentTransaction.php b/app/Listeners/Document/CreateDocumentTransaction.php index cd5c16245..76c4535f7 100644 --- a/app/Listeners/Document/CreateDocumentTransaction.php +++ b/app/Listeners/Document/CreateDocumentTransaction.php @@ -36,16 +36,30 @@ class CreateDocumentTransaction if (empty($user) || $signed) { flash($message)->error()->important(); - redirect()->route("signed.$type.show", $document->id)->send(); + return $this->getResponse('signed.' . $type . '.show', $document, $message); } if ($user->can('read-client-portal')) { flash($message)->error()->important(); - redirect()->route("portal.$type.show", $document->id)->send(); + return $this->getResponse('portal.' . $type . '.show', $document, $message); } throw new \Exception($message); } } + + protected function getResponse($path, $document, $message) + { + if (request()->expectsJson()) { + return response()->json([ + 'success' => false, + 'errors' => true, + 'message' => $message, + 'redirect' => route($path, $document->id) + ]); + } + + return redirect()->route($path, $document->id); + } }