diff --git a/app/FailedJob.php b/app/FailedJob.php index 4104bb4e0..c332975b3 100644 --- a/app/FailedJob.php +++ b/app/FailedJob.php @@ -28,4 +28,9 @@ public function getCommand() { return \App\Job::getPayloadCommand($this->getPayloadDecoded()); } + + public static function retry($job_id) + { + \Artisan::call('queue:retry', ['id' => $job_id]); + } } diff --git a/app/Http/Controllers/ConversationsController.php b/app/Http/Controllers/ConversationsController.php index ef41d2806..2369da33d 100755 --- a/app/Http/Controllers/ConversationsController.php +++ b/app/Http/Controllers/ConversationsController.php @@ -2246,6 +2246,29 @@ public function ajax(Request $request) ])->render(); $response['status'] = 'success'; } + break; + + case 'retry_send': + $thread = Thread::find($request->thread_id); + + if (!$thread) { + $response['msg'] = __('Thread not found'); + } elseif (!$user->can('view', $thread->conversation)) { + $response['msg'] = __('Not enough permissions'); + } + + if (!$response['msg']) { + $job_id = $thread->getFailedJobId(); + + if ($job_id) { + \App\FailedJob::retry($job_id); + $thread->send_status = SendLog::STATUS_ACCEPTED; + $thread->updateSendStatusData(['msg' => '']); + $thread->save(); + + $response['status'] = 'success'; + } + } break; diff --git a/app/Jobs/SendReplyToCustomer.php b/app/Jobs/SendReplyToCustomer.php index 97c8f14d5..c0854478f 100644 --- a/app/Jobs/SendReplyToCustomer.php +++ b/app/Jobs/SendReplyToCustomer.php @@ -317,7 +317,7 @@ public function handle() // If an email has not been sent after 1 hour - show an error message to support agent. if ($this->attempts() >= 3 || $response_code >= 500) { - $this->last_thread->send_status = SendLog::STATUS_SEND_ERROR; + $this->last_thread->send_status = SendLog::STATUS_SEND_INTERMEDIATE_ERROR; $this->last_thread->updateSendStatusData(['msg' => $error_message]); $this->last_thread->save(); } diff --git a/app/SendLog.php b/app/SendLog.php index 81e32327e..4dfa46132 100644 --- a/app/SendLog.php +++ b/app/SendLog.php @@ -14,6 +14,7 @@ class SendLog extends Model * https://documentation.mailgun.com/en/latest/api-events.html#event-types. */ const STATUS_ACCEPTED = 1; // accepted (for delivery) + const STATUS_SEND_INTERMEDIATE_ERROR = 10; const STATUS_SEND_ERROR = 2; const STATUS_DELIVERY_SUCCESS = 4; const STATUS_DELIVERY_ERROR = 5; // rejected, failed @@ -42,6 +43,7 @@ class SendLog extends Model * @var [type] */ public static $status_errors = [ + self::STATUS_SEND_INTERMEDIATE_ERROR, self::STATUS_SEND_ERROR, self::STATUS_DELIVERY_ERROR, ]; diff --git a/app/Thread.php b/app/Thread.php index 0e33caac0..2d7591308 100644 --- a/app/Thread.php +++ b/app/Thread.php @@ -2,6 +2,7 @@ namespace App; +use App\SendLog; use App\Events\ConversationStatusChanged; use App\Events\ConversationUserChanged; use App\Events\UserAddedNote; @@ -1517,4 +1518,24 @@ public static function getLastThread($threads) $threads = self::sortThreads($threads); return $threads->first(); } + + public function canRetrySend() + { + if (!in_array($this->send_status, [SendLog::STATUS_SEND_ERROR, SendLog::STATUS_DELIVERY_ERROR])) { + return false; + } + // Check if failed_job still exists. + if (!$this->getFailedJobId()) { + return false; + } + + return true; + } + + public function getFailedJobId() + { + return \App\FailedJob::where('queue', 'emails') + ->where('payload', 'like', '{"displayName":"App\\\\\\\\Jobs\\\\\\\\SendReplyToCustomer"%{i:0;i:'.$this->id.';%') + ->value('id'); + } } diff --git a/public/js/main.js b/public/js/main.js index 7a3aae071..a07506d51 100755 --- a/public/js/main.js +++ b/public/js/main.js @@ -1285,6 +1285,30 @@ function initConversation() ); }); + // Retry failed thread + jQuery("#conv-layout-main .btn-thread-retry").click(function(e){ + var button = $(this); + button.button('loading'); + + fsAjax( + { + action: 'retry_send', + thread_id: button.parents('.thread:first').attr('data-thread_id') + }, + laroute.route('conversations.ajax'), + function(response) { + if (isAjaxSuccess(response)) { + reloadPage(); + } else { + showAjaxError(response); + button.button('reset'); + } + }, true + ); + + e.preventDefault(); + }); + // Print if (getQueryParam('print')) { window.print(); @@ -5509,4 +5533,9 @@ function initLogsTable() function isChatMode() { return $("body:first").hasClass('chat-mode'); +} + +function reloadPage() +{ + window.location.href = ''; } \ No newline at end of file diff --git a/resources/views/conversations/partials/thread.blade.php b/resources/views/conversations/partials/thread.blade.php index 433a530fa..a12165baf 100644 --- a/resources/views/conversations/partials/thread.blade.php +++ b/resources/views/conversations/partials/thread.blade.php @@ -231,6 +231,10 @@