From 82b298c3d473243785f8d2732c80237f01259b83 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Sun, 25 Feb 2018 13:51:20 +0200 Subject: [PATCH] Support reactivating email addresses --- app/Http/Controllers/ClientController.php | 40 +++-------- app/Jobs/LoadPostmarkHistory.php | 88 +++++++++++++++++++++++ app/Jobs/ReactivatePostmarkEmail.php | 29 ++++++++ resources/lang/en/texts.php | 2 + resources/views/clients/show.blade.php | 14 +++- routes/web.php | 3 +- 6 files changed, 143 insertions(+), 33 deletions(-) create mode 100644 app/Jobs/LoadPostmarkHistory.php create mode 100644 app/Jobs/ReactivatePostmarkEmail.php diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index ee018d391a..b3eaa66b7e 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -5,6 +5,8 @@ namespace App\Http\Controllers; use App\Http\Requests\ClientRequest; use App\Http\Requests\CreateClientRequest; use App\Http\Requests\UpdateClientRequest; +use App\Jobs\LoadPostmarkHistory; +use App\Jobs\ReactivatePostmarkEmail; use App\Models\Account; use App\Models\Client; use App\Models\Credit; @@ -276,39 +278,15 @@ class ClientController extends BaseController public function getEmailHistory() { - $str = ''; + $history = dispatch(new LoadPostmarkHistory(request()->email)); - if (config('services.postmark')) { - $email = request()->email; - $account = auth()->user()->account; - $postmark = new \Postmark\PostmarkClient(config('services.postmark')); - $response = $postmark->getOutboundMessages(5, 0, $email, null, $account->account_key); + return response()->json($history); + } - foreach ($response['messages'] as $message) { - $details = $postmark->getOutboundMessageDetails($message['MessageID']); - $str .= sprintf('%s
', $details['subject']); + public function reactivateEmail() + { + $result = dispatch(new ReactivatePostmarkEmail(request()->bounce_id)); - if (count($details['messageevents'])) { - $event = $details['messageevents'][0]; - $str .= sprintf('%s | %s
', $event['Type'], $account->getDateTime($event['ReceivedAt'], true)); - if ($message = $event['Details']['DeliveryMessage']) { - $str .= sprintf('%s
', $message); - } - if ($server = $event['Details']['DestinationServer']) { - $str .= sprintf('%s
', $server); - } - } else { - $str .= trans('texts.processing') . '...'; - } - - $str .= '

'; - } - } - - if (! $str) { - $str = trans('texts.no_messages_found'); - } - - return $str; + return response()->json($result); } } diff --git a/app/Jobs/LoadPostmarkHistory.php b/app/Jobs/LoadPostmarkHistory.php new file mode 100644 index 0000000000..5708e10e2b --- /dev/null +++ b/app/Jobs/LoadPostmarkHistory.php @@ -0,0 +1,88 @@ +email = $email; + $this->bounceId = false; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + $str = ''; + + if (config('services.postmark')) { + $this->account = auth()->user()->account; + $this->postmark = new PostmarkClient(config('services.postmark')); + + $str .= $this->loadBounceEvents(); + $str .= $this->loadEmailEvents(); + } + + if (! $str) { + $str = trans('texts.no_messages_found'); + } + + $response = new stdClass; + $response->str = $str; + $response->bounce_id = $this->bounceId; + + return $response; + } + + private function loadBounceEvents() { + $str = ''; + $response = $this->postmark->getBounces(5, 0, null, null, $this->email, $this->account->account_key); + + foreach ($response['bounces'] as $bounce) { + if (! $bounce['inactive'] || ! $bounce['canactivate']) { + continue; + } + + $str .= sprintf('%s
', $bounce['subject']); + $str .= sprintf('%s | %s
', $bounce['type'], $this->account->getDateTime($bounce['bouncedat'], true)); + $str .= sprintf('%s %s

', $bounce['description'], $bounce['details']); + + $this->bounceId = $bounce['id']; + } + + return $str; + } + + private function loadEmailEvents() { + $str = ''; + $response = $this->postmark->getOutboundMessages(5, 0, $this->email, null, $this->account->account_key); + + foreach ($response['messages'] as $message) { + $details = $this->postmark->getOutboundMessageDetails($message['MessageID']); + $str .= sprintf('%s
', $details['subject']); + + if (count($details['messageevents'])) { + $event = $details['messageevents'][0]; + $str .= sprintf('%s | %s
', $event['Type'], $this->account->getDateTime($event['ReceivedAt'], true)); + if ($message = $event['Details']['DeliveryMessage']) { + $str .= sprintf('%s
', $message); + } + if ($server = $event['Details']['DestinationServer']) { + $str .= sprintf('%s
', $server); + } + } else { + $str .= trans('texts.processing') . '...'; + } + + $str .= '

'; + } + } +} diff --git a/app/Jobs/ReactivatePostmarkEmail.php b/app/Jobs/ReactivatePostmarkEmail.php new file mode 100644 index 0000000000..891619d99e --- /dev/null +++ b/app/Jobs/ReactivatePostmarkEmail.php @@ -0,0 +1,29 @@ +bounceId = $bounceId; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + if (! config('services.postmark')) { + return false; + } + + $postmark = new PostmarkClient(config('services.postmark')); + $response = $postmark->activateBounce($this->bounceId); + } +} diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 96545e9004..1d55584551 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2750,6 +2750,8 @@ $LANG = array( 'loading' => 'Loading', 'no_messages_found' => 'No messages found', 'processing' => 'Processing', + 'reactivate' => 'Reactivate', + 'reactivated_email' => 'The email address has been reactivated', ); diff --git a/resources/views/clients/show.blade.php b/resources/views/clients/show.blade.php index a10eff553c..dea131c736 100644 --- a/resources/views/clients/show.blade.php +++ b/resources/views/clients/show.blade.php @@ -337,6 +337,7 @@

@@ -397,10 +398,21 @@ } function showEmailHistory(email) { + window.emailBounceId = false; $('#emailHistoryModal .panel-body').html("{{ trans('texts.loading') }}..."); + $('#reactivateButton').hide(); $('#emailHistoryModal').modal('show'); $.post('{{ url('/email_history') }}', {email: email}, function(data) { - $('#emailHistoryModal .panel-body').html(data); + $('#emailHistoryModal .panel-body').html(data.str); + window.emailBounceId = data.bounce_id; + $('#reactivateButton').toggle(!! window.emailBounceId); + }) + } + + function onReactivateClick() { + $.post('{{ url('/reactivate_email') }}/' + window.emailBounceId, function(data) { + $('#emailHistoryModal').modal('hide'); + swal("{{ trans('texts.reactivated_email') }}") }) } diff --git a/routes/web.php b/routes/web.php index 2c9ddd55de..9ba3648d28 100644 --- a/routes/web.php +++ b/routes/web.php @@ -141,12 +141,13 @@ Route::group(['middleware' => ['lookup:user', 'auth:user']], function () { Route::get('settings/enable_two_factor', 'TwoFactorController@setupTwoFactor'); Route::post('settings/enable_two_factor', 'TwoFactorController@enableTwoFactor'); - Route::post('email_history', 'ClientController@getEmailHistory'); Route::resource('clients', 'ClientController'); Route::get('api/clients', 'ClientController@getDatatable'); Route::get('api/activities/{client_id?}', 'ActivityController@getDatatable'); Route::post('clients/bulk', 'ClientController@bulk'); Route::get('clients/statement/{client_id}/{status_id?}/{start_date?}/{end_date?}', 'ClientController@statement'); + Route::post('email_history', 'ClientController@getEmailHistory'); + Route::post('reactivate_email/{bounce_id}', 'ClientController@reactivateEmail'); Route::get('time_tracker', 'TimeTrackerController@index'); Route::get('tasks/kanban/{client_id?}/{project_id?}', 'TaskKanbanController@index');