From 2eca9a286fd931b3531d8324f3945af31eaa5592 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 15 May 2021 14:29:19 +1000 Subject: [PATCH] Fixes for domains --- VERSION.txt | 2 +- app/Jobs/Company/CompanyImport.php | 84 ++++++++++++++++++++-- app/Models/Company.php | 2 +- tests/Feature/Import/ImportCompanyTest.php | 29 ++++++++ 4 files changed, 109 insertions(+), 8 deletions(-) diff --git a/VERSION.txt b/VERSION.txt index 3b4031cf52..258f432622 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1 +1 @@ -5.1.61 \ No newline at end of file +5.1.62 \ No newline at end of file diff --git a/app/Jobs/Company/CompanyImport.php b/app/Jobs/Company/CompanyImport.php index d40d7be0ef..55177cd455 100644 --- a/app/Jobs/Company/CompanyImport.php +++ b/app/Jobs/Company/CompanyImport.php @@ -33,6 +33,7 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Str; use ZipArchive; use ZipStream\Option\Archive; use ZipStream\ZipStream; @@ -41,15 +42,45 @@ class CompanyImport implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesHash; + protected $current_app_version; + public $company; + private $account; + public $file_path; private $backup_file; public $import_company; + public $ids = []; + private $options = ''; + + private $importables = [ + 'company', + 'users', + // 'payment_terms', + // 'tax_rates', + // 'clients', + // 'company_gateways', + // 'client_gateway_tokens', + // 'vendors', + // 'projects', + // 'products', + // 'credits', + // 'invoices', + // 'recurring_invoices', + // 'quotes', + // 'payments', + // 'expense_categories', + // 'task_statuses', + // 'expenses', + // 'tasks', + // 'documents', + ]; + /** * Create a new job instance. * @@ -62,6 +93,7 @@ class CompanyImport implements ShouldQueue $this->company = $company; $this->file_path = $file_path; $this->options = $options; + $this->current_app_version = config('ninja.app_version'); } public function handle() @@ -69,10 +101,19 @@ class CompanyImport implements ShouldQueue MultiDB::setDb($this->company->db); $this->company =Company::where('company_key', $this->company->company_key)->firstOrFail(); + $this->account = $this->company->account; $this->unzipFile() ->preFlightChecks(); + foreach($this->importables as $import){ + + $method = Str::ucfirst(Str::camel($import)); + + $this->{$method}(); + + } + } @@ -85,10 +126,12 @@ class CompanyImport implements ShouldQueue private function preFlightChecks() { //check the file version and perform any necessary adjustments to the file in order to proceed - needed when we change schema - // - $app_version = $this->backup_file->app_version; + + if($this->current_app_version != $this->backup_file->app_version) + { + //perform some magic here + } - nlog($app_version); return $this; } @@ -118,10 +161,39 @@ class CompanyImport implements ShouldQueue return $this; } - private function importData() + private function importUsers() { - // $this->import_company = Company::where('company_key', $this->company->company_key)->firstOrFail(); + User::unguard(); - return $this; + foreach ($this->backup_file->users as $user) + { + + $new_user = User::firstOrNew( + ['email' => $user->email], + (array)$user, + ); + + $new_user->account_id = $this->account->id; + $new_user->save(['timestamps' => false]); + + $this->ids['users']["{$user->id}"] = $new_user->id; + } + + Expense::reguard(); + + } + + + public function transformId(string$resource, string $old): int + { + if (! array_key_exists($resource, $this->ids)) { + throw new \Exception("Resource {$resource} not available."); + } + + if (! array_key_exists("{$old}", $this->ids[$resource])) { + throw new \Exception("Missing resource key: {$old}"); + } + + return $this->ids[$resource]["{$old}"]; } } \ No newline at end of file diff --git a/app/Models/Company.php b/app/Models/Company.php index 14c2335b2d..4c2f012b30 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -443,7 +443,7 @@ class Company extends BaseModel public function domain() { - if (Ninja::isNinja()) { + if (Ninja::isHosted()) { if($this->portal_mode == 'domain') return $this->portal_domain; diff --git a/tests/Feature/Import/ImportCompanyTest.php b/tests/Feature/Import/ImportCompanyTest.php index 01b4e9f916..d8134c6e52 100644 --- a/tests/Feature/Import/ImportCompanyTest.php +++ b/tests/Feature/Import/ImportCompanyTest.php @@ -53,4 +53,33 @@ class ImportCompanyTest extends TestCase $this->assertTrue(is_array(json_decode(file_get_contents($backup_json_file),1))); } + public function testImportUsers() + { + + $backup_json_file = base_path().'/tests/Feature/Import/backup.json'; + + $backup_json_file = json_decode(file_get_contents($backup_json_file)); + + $this->assertTrue(property_exists($backup_json_file, 'app_version')); + + // User::unguard(); + + // foreach ($this->backup_file->users as $user) + // { + + // $new_user = User::firstOrNew( + // ['email' => $user->email], + // (array)$user, + // ); + + // $new_user->account_id = $this->account->id; + // $new_user->save(['timestamps' => false]); + + // $this->ids['users']["{$user->id}"] = $new_user->id; + // } + + // User::reguard(); + } + + }