From e63cf9c48f6d8138f1e7dedb890d812f7c5c108b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 21 Sep 2022 22:02:29 +1000 Subject: [PATCH] Do not serialize company after migration --- app/Jobs/Util/Import.php | 11 ++++++++++- app/Mail/MigrationCompleted.php | 15 ++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index 7a987808d1..5fad49fe6e 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -264,7 +264,7 @@ class Import implements ShouldQueue $t->replace(Ninja::transformTranslations($this->company->settings)); Mail::to($this->user->email, $this->user->name()) - ->send(new MigrationCompleted($this->company, implode("
",$check_data))); + ->send(new MigrationCompleted($this->company->id, $this->company->db, implode("
",$check_data))); } catch(\Exception $e) { nlog($e->getMessage()); @@ -715,6 +715,15 @@ class Import implements ShouldQueue Client::reguard(); + Client::with('contacts')->where('company_id', $this->company->id)->cursor()->each(function ($client){ + + $contact = $client->contacts->sortByDesc('is_primary')->first(); + $contact->is_primary = true; + $contact->save(); + + }); + + /*Improve memory handling by setting everything to null when we have finished*/ $data = null; $contact_repository = null; diff --git a/app/Mail/MigrationCompleted.php b/app/Mail/MigrationCompleted.php index d8dfb3ed8e..daddca816c 100644 --- a/app/Mail/MigrationCompleted.php +++ b/app/Mail/MigrationCompleted.php @@ -2,6 +2,7 @@ namespace App\Mail; +use App\Libraries\MultiDB; use App\Models\Company; use App\Utils\Ninja; use Illuminate\Bus\Queueable; @@ -13,19 +14,23 @@ class MigrationCompleted extends Mailable { // use Queueable, SerializesModels; - public $company; + public $company_id; + + public $db; public $check_data; + public $company; /** * Create a new message instance. * * @return void */ - public function __construct(Company $company, $check_data = '') + public function __construct(int $company_id, string $db, $check_data = '') { - $this->company = $company; + $this->company_id = $company_id; $this->check_data = $check_data; + $this->db = $db; } /** @@ -35,6 +40,10 @@ class MigrationCompleted extends Mailable */ public function build() { + + MultiDB::setDb($this->db); + $this->company = Company::find($this->company_id); + App::forgetInstance('translator'); $t = app('translator'); $t->replace(Ninja::transformTranslations($this->company->settings));