From 2f042dec4b52c3839a8f6b9a6fad7ead436143be Mon Sep 17 00:00:00 2001 From: EnesSacid-Buker <73346401+EnesSacid-Buker@users.noreply.github.com> Date: Fri, 3 Jun 2022 13:49:23 +0300 Subject: [PATCH 1/4] Tests fixed. --- app/Abstracts/Factory.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/Abstracts/Factory.php b/app/Abstracts/Factory.php index 179226337..eda6e3924 100644 --- a/app/Abstracts/Factory.php +++ b/app/Abstracts/Factory.php @@ -56,9 +56,7 @@ abstract class Factory extends BaseFactory public function setCompany(): void { - $company_id = $this->getRawAttribute('company_id'); - - $this->company = !empty($company_id) ? Company::find($company_id) : $this->user->companies()->first(); + $this->company = $this->user->companies()->first(); $this->company->makeCurrent(); From a669aa2b2089d7c97302228ce30d455745e19a72 Mon Sep 17 00:00:00 2001 From: EnesSacid-Buker <73346401+EnesSacid-Buker@users.noreply.github.com> Date: Fri, 3 Jun 2022 17:18:01 +0300 Subject: [PATCH 2/4] fixed changeable company id bug --- app/Abstracts/Factory.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/Abstracts/Factory.php b/app/Abstracts/Factory.php index eda6e3924..1f14d9e8c 100644 --- a/app/Abstracts/Factory.php +++ b/app/Abstracts/Factory.php @@ -5,9 +5,10 @@ namespace App\Abstracts; use App\Models\Auth\User; use App\Models\Common\Company; use App\Traits\Jobs; -use Closure; +use App\Utilities\Date; use Illuminate\Database\Eloquent\Factories\Factory as BaseFactory; use Illuminate\Database\Eloquent\Model as EloquentModel; +use Illuminate\Support\Facades\Cache; abstract class Factory extends BaseFactory { @@ -44,6 +45,8 @@ abstract class Factory extends BaseFactory public function company(int $id): static { + Cache::put('factory_company_id', $id, Date::now()->addHour(6)); + return $this->state([ 'company_id' => $id, ]); @@ -56,7 +59,11 @@ abstract class Factory extends BaseFactory public function setCompany(): void { - $this->company = $this->user->companies()->first(); + if (is_null(Cache::get('factory_company_id'))) { + $this->company = $this->user->companies()->first(); + } else { + $this->company = company(Cache::get('factory_company_id')); + } $this->company->makeCurrent(); From 0f637ebda78565d9f6fd8d11541bf37eb4ef3896 Mon Sep 17 00:00:00 2001 From: EnesSacid-Buker <73346401+EnesSacid-Buker@users.noreply.github.com> Date: Fri, 3 Jun 2022 17:51:22 +0300 Subject: [PATCH 3/4] Code refactoring. --- app/Abstracts/Factory.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/Abstracts/Factory.php b/app/Abstracts/Factory.php index 1f14d9e8c..f633e9b8a 100644 --- a/app/Abstracts/Factory.php +++ b/app/Abstracts/Factory.php @@ -59,10 +59,12 @@ abstract class Factory extends BaseFactory public function setCompany(): void { - if (is_null(Cache::get('factory_company_id'))) { - $this->company = $this->user->companies()->first(); + $id = Cache::get('factory_company_id'); + + if (! is_null($id)) { + $this->company = company($id); } else { - $this->company = company(Cache::get('factory_company_id')); + $this->company = $this->user->companies()->first(); } $this->company->makeCurrent(); From 7407747294c9f3c395f75d34df36911b652f6c66 Mon Sep 17 00:00:00 2001 From: EnesSacid-Buker <73346401+EnesSacid-Buker@users.noreply.github.com> Date: Fri, 3 Jun 2022 17:57:57 +0300 Subject: [PATCH 4/4] Code refactoring. --- app/Abstracts/Factory.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/app/Abstracts/Factory.php b/app/Abstracts/Factory.php index f633e9b8a..8a42fe0cf 100644 --- a/app/Abstracts/Factory.php +++ b/app/Abstracts/Factory.php @@ -45,7 +45,7 @@ abstract class Factory extends BaseFactory public function company(int $id): static { - Cache::put('factory_company_id', $id, Date::now()->addHour(6)); + Cache::put('state_company_id', $id, Date::now()->addHour(6)); return $this->state([ 'company_id' => $id, @@ -59,17 +59,13 @@ abstract class Factory extends BaseFactory public function setCompany(): void { - $id = Cache::get('factory_company_id'); + $state_id = Cache::get('state_company_id'); - if (! is_null($id)) { - $this->company = company($id); - } else { - $this->company = $this->user->companies()->first(); - } + $this->company = ! is_null($state_id) ? company($state_id) : $this->user->companies()->first(); $this->company->makeCurrent(); - app('url')->defaults(['company_id' => company_id()]); + app('url')->defaults(['company_id' => $this->company->id]); } public function getRawAttribute($key)