1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00

Merge pull request #5710 from turbo124/v5-develop

Fixes for domains
This commit is contained in:
David Bomba 2021-05-15 14:29:35 +10:00 committed by GitHub
commit 6defeb0831
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 109 additions and 8 deletions

View File

@ -1 +1 @@
5.1.61 5.1.62

View File

@ -33,6 +33,7 @@ use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use ZipArchive; use ZipArchive;
use ZipStream\Option\Archive; use ZipStream\Option\Archive;
use ZipStream\ZipStream; use ZipStream\ZipStream;
@ -41,15 +42,45 @@ class CompanyImport implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesHash; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesHash;
protected $current_app_version;
public $company; public $company;
private $account;
public $file_path; public $file_path;
private $backup_file; private $backup_file;
public $import_company; public $import_company;
public $ids = [];
private $options = ''; 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. * Create a new job instance.
* *
@ -62,6 +93,7 @@ class CompanyImport implements ShouldQueue
$this->company = $company; $this->company = $company;
$this->file_path = $file_path; $this->file_path = $file_path;
$this->options = $options; $this->options = $options;
$this->current_app_version = config('ninja.app_version');
} }
public function handle() public function handle()
@ -69,10 +101,19 @@ class CompanyImport implements ShouldQueue
MultiDB::setDb($this->company->db); MultiDB::setDb($this->company->db);
$this->company =Company::where('company_key', $this->company->company_key)->firstOrFail(); $this->company =Company::where('company_key', $this->company->company_key)->firstOrFail();
$this->account = $this->company->account;
$this->unzipFile() $this->unzipFile()
->preFlightChecks(); ->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() private function preFlightChecks()
{ {
//check the file version and perform any necessary adjustments to the file in order to proceed - needed when we change schema //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; return $this;
} }
@ -118,10 +161,39 @@ class CompanyImport implements ShouldQueue
return $this; 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}"];
} }
} }

View File

@ -443,7 +443,7 @@ class Company extends BaseModel
public function domain() public function domain()
{ {
if (Ninja::isNinja()) { if (Ninja::isHosted()) {
if($this->portal_mode == 'domain') if($this->portal_mode == 'domain')
return $this->portal_domain; return $this->portal_domain;

View File

@ -53,4 +53,33 @@ class ImportCompanyTest extends TestCase
$this->assertTrue(is_array(json_decode(file_get_contents($backup_json_file),1))); $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();
}
} }