1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-09 12:42:36 +01:00

Support reactivating email addresses

This commit is contained in:
Hillel Coren 2018-02-25 13:51:20 +02:00
parent 8d7eb709be
commit 82b298c3d4
6 changed files with 143 additions and 33 deletions

View File

@ -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('<b>%s</b><br/>', $details['subject']);
public function reactivateEmail()
{
$result = dispatch(new ReactivatePostmarkEmail(request()->bounce_id));
if (count($details['messageevents'])) {
$event = $details['messageevents'][0];
$str .= sprintf('%s | %s<br/>', $event['Type'], $account->getDateTime($event['ReceivedAt'], true));
if ($message = $event['Details']['DeliveryMessage']) {
$str .= sprintf('<span class="text-muted">%s</span><br/>', $message);
}
if ($server = $event['Details']['DestinationServer']) {
$str .= sprintf('<span class="text-muted">%s</span><br/>', $server);
}
} else {
$str .= trans('texts.processing') . '...';
}
$str .= '<p/>';
}
}
if (! $str) {
$str = trans('texts.no_messages_found');
}
return $str;
return response()->json($result);
}
}

View File

@ -0,0 +1,88 @@
<?php
namespace App\Jobs;
use App\Jobs\Job;
use Postmark\PostmarkClient;
use stdClass;
class LoadPostmarkHistory extends Job
{
public function __construct($email)
{
$this->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('<b>%s</b><br/>', $bounce['subject']);
$str .= sprintf('<span class="text-danger">%s</span> | %s<br/>', $bounce['type'], $this->account->getDateTime($bounce['bouncedat'], true));
$str .= sprintf('<span class="text-muted">%s %s</span><p/>', $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('<b>%s</b><br/>', $details['subject']);
if (count($details['messageevents'])) {
$event = $details['messageevents'][0];
$str .= sprintf('%s | %s<br/>', $event['Type'], $this->account->getDateTime($event['ReceivedAt'], true));
if ($message = $event['Details']['DeliveryMessage']) {
$str .= sprintf('<span class="text-muted">%s</span><br/>', $message);
}
if ($server = $event['Details']['DestinationServer']) {
$str .= sprintf('<span class="text-muted">%s</span><br/>', $server);
}
} else {
$str .= trans('texts.processing') . '...';
}
$str .= '<p/>';
}
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace App\Jobs;
use App\Jobs\Job;
use Postmark\PostmarkClient;
class ReactivatePostmarkEmail extends Job
{
public function __construct($bounceId)
{
$this->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);
}
}

View File

@ -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',
);

View File

@ -337,6 +337,7 @@
<div class="modal-footer" id="signUpFooter" style="margin-top: 0px">
<button type="button" class="btn btn-default" data-dismiss="modal">{{ trans('texts.close') }} </button>
<button type="button" class="btn btn-danger" onclick="onReactivateClick()" id="reactivateButton" style="display:none;">{{ trans('texts.reactivate') }} </button>
</div>
</div>
</div>
@ -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') }}")
})
}

View File

@ -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');