diff --git a/app/Http/Controllers/MigrationController.php b/app/Http/Controllers/MigrationController.php index bbc361c41321..964af9163952 100644 --- a/app/Http/Controllers/MigrationController.php +++ b/app/Http/Controllers/MigrationController.php @@ -14,7 +14,8 @@ namespace App\Http\Controllers; use App\Console\Commands\ImportMigrations; use App\DataMapper\CompanySettings; -use App\Jobs\Mail\MailRouter; +use App\Jobs\Mail\NinjaMailerJob; +use App\Jobs\Mail\NinjaMailerObject; use App\Jobs\Util\StartMigration; use App\Mail\ExistingMigration; use App\Models\Company; @@ -248,7 +249,13 @@ class MigrationController extends BaseController if ($checks['existing_company'] == true && $checks['force'] == false) { nlog('Migrating: Existing company without force. (CASE_01)'); - MailRouter::dispatch(new ExistingMigration(), $existing_company, $user); + $nmo = new NinjaMailerObject; + $nmo->mailable = new ExistingMigration(); + $nmo->company = $existing_company; + $nmo->settings = $existing_company->settings; + $nmo->to_user = $user; + + NinjaMailerJob::dispatch($nmo); return response()->json([ '_id' => Str::uuid(), diff --git a/app/Jobs/Import/CSVImport.php b/app/Jobs/Import/CSVImport.php index 5a5d20643bd6..3e9df315bf6d 100644 --- a/app/Jobs/Import/CSVImport.php +++ b/app/Jobs/Import/CSVImport.php @@ -17,7 +17,8 @@ use App\Factory\PaymentFactory; use App\Http\Requests\Invoice\StoreInvoiceRequest; use App\Import\ImportException; use App\Import\Transformers\BaseTransformer; -use App\Jobs\Mail\MailRouter; +use App\Jobs\Mail\NinjaMailerJob; +use App\Jobs\Mail\NinjaMailerObject; use App\Libraries\MultiDB; use App\Mail\Import\ImportCompleted; use App\Models\Client; @@ -91,9 +92,10 @@ class CSVImport implements ShouldQueue { MultiDB::setDb( $this->company->db ); - $this->company->owner()->setCompany( $this->company ); Auth::login( $this->company->owner(), true ); + $this->company->owner()->setCompany( $this->company ); + $this->buildMaps(); nlog( "import " . $this->import_type ); @@ -127,7 +129,13 @@ class CSVImport implements ShouldQueue { 'company' => $this->company, ]; - MailRouter::dispatch( new ImportCompleted( $data ), $this->company, auth()->user() ); + $nmo = new NinjaMailerObject; + $nmo->mailable = new ImportCompleted( $data ); + $nmo->company = $this->company; + $nmo->settings = $this->company->settings; + $nmo->to_user = $this->company->owner(); + + NinjaMailerJob::dispatch($nmo); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/app/Jobs/Mail/BaseMailerJob.php b/app/Jobs/Mail/BaseMailerJob.php deleted file mode 100644 index c11abe32ccc2..000000000000 --- a/app/Jobs/Mail/BaseMailerJob.php +++ /dev/null @@ -1,120 +0,0 @@ -settings)); - - switch ($this->settings->email_sending_method) { - case 'default': - break; - case 'gmail': - $this->setGmailMailer(); - break; - default: - break; - } - } - - public function setGmailMailer() - { - $sending_user = $this->settings->gmail_sending_user_id; - - $user = User::find($this->decodePrimaryKey($sending_user)); - - $google = (new Google())->init(); - $google->getClient()->setAccessToken(json_encode($user->oauth_user_token)); - - if ($google->getClient()->isAccessTokenExpired()) { - $google->refreshToken($user); - } - - /* - * Now that our token is refreshed and valid we can boot the - * mail driver at runtime and also set the token which will persist - * just for this request. - */ - - // config(['mail.driver' => 'gmail']); - // config(['services.gmail.token' => $user->oauth_user_token->access_token]); - // config(['mail.from.address' => $user->email]); - // config(['mail.from.name' => $user->present()->name()]); - - //(new MailServiceProvider(app()))->register(); - - nlog("after registering mail service provider"); - nlog(config('services.gmail.token')); - } - - public function logMailError($errors, $recipient_object) - { - SystemLogger::dispatch( - $errors, - SystemLog::CATEGORY_MAIL, - SystemLog::EVENT_MAIL_SEND, - SystemLog::TYPE_FAILURE, - $recipient_object - ); - } - - public function failed($exception = null) - { - nlog('mailer job failed'); - nlog($exception->getMessage()); - - $job_failure = new EmailFailure(); - $job_failure->string_metric5 = get_parent_class($this); - $job_failure->string_metric6 = $exception->getMessage(); - - LightLogs::create($job_failure) - ->batch(); - } -} diff --git a/app/Jobs/Mail/MailRouter.php b/app/Jobs/Mail/MailRouter.php deleted file mode 100644 index c2673d4e050f..000000000000 --- a/app/Jobs/Mail/MailRouter.php +++ /dev/null @@ -1,83 +0,0 @@ -mailable = $mailable; - - $this->company = $company; - - $this->to_user = $to_user; - - $this->sending_method = $sending_method; - - if ($to_user instanceof ClientContact) { - $this->settings = $to_user->client->getMergedSettings(); - } else { - $this->settings = $this->company->settings; - } - } - - public function handle() - { - /*If we are migrating data we don't want to fire these notification*/ - if ($this->company->is_disabled) { - return true; - } - - MultiDB::setDb($this->company->db); - - //if we need to set an email driver do it now - $this->setMailDriver(); - - //send email - try { - Mail::to($this->to_user->email) - ->send($this->mailable); - } catch (\Exception $e) { - //$this->failed($e); - - if ($this->to_user instanceof ClientContact) { - $this->logMailError($e->getMessage(), $this->to_user->client); - } - } - } -} diff --git a/app/Jobs/Mail/PaymentFailureMailer.php b/app/Jobs/Mail/PaymentFailureMailer.php index 5bb1ef5dbfde..1fb0f000db5c 100644 --- a/app/Jobs/Mail/PaymentFailureMailer.php +++ b/app/Jobs/Mail/PaymentFailureMailer.php @@ -28,7 +28,7 @@ use Illuminate\Support\Facades\Mail; /*Multi Mailer implemented*/ -class PaymentFailureMailer extends BaseMailerJob implements ShouldQueue +class PaymentFailureMailer implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, UserNotifies;