Merge pull request #5971 from turbo124/v5-develop

Fixes for Company Importer
This commit is contained in:
David Bomba 2021-06-09 11:50:31 +10:00 committed by GitHub
commit bf2d3284b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 16 deletions

View File

@ -75,7 +75,7 @@ class Kernel extends ConsoleKernel
} }
if(config('queue.default') == 'database' && Ninja::isSelfHost() && config('ninja.internal_queue_enabled')) { if(config('queue.default') == 'database' && Ninja::isSelfHost() && config('ninja.internal_queue_enabled') && !config('ninja.is_docker')) {
$schedule->command('queue:work')->everyMinute()->withoutOverlapping(); $schedule->command('queue:work')->everyMinute()->withoutOverlapping();
$schedule->command('queue:restart')->everyFiveMinutes()->withoutOverlapping(); $schedule->command('queue:restart')->everyFiveMinutes()->withoutOverlapping();

View File

@ -93,6 +93,10 @@ class CompanyImport implements ShouldQueue
public $pre_flight_checks_pass = true; public $pre_flight_checks_pass = true;
public $force_user_coalesce = false;
public $company_owner;
private $importables = [ private $importables = [
// 'company', // 'company',
'users', 'users',
@ -141,6 +145,7 @@ class CompanyImport implements ShouldQueue
public function __construct(Company $company, User $user, string $hash, array $request_array) public function __construct(Company $company, User $user, string $hash, array $request_array)
{ {
$this->company = $company; $this->company = $company;
$this->user = $user;
$this->hash = $hash; $this->hash = $hash;
$this->request_array = $request_array; $this->request_array = $request_array;
$this->current_app_version = config('ninja.app_version'); $this->current_app_version = config('ninja.app_version');
@ -152,6 +157,7 @@ class CompanyImport implements ShouldQueue
$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->account = $this->company->account;
$this->company_owner = $this->company->owner();
nlog("Company ID = {$this->company->id}"); nlog("Company ID = {$this->company->id}");
nlog("Hash ID = {$this->hash}"); nlog("Hash ID = {$this->hash}");
@ -167,7 +173,7 @@ class CompanyImport implements ShouldQueue
$this->checkUserCount(); $this->checkUserCount();
if(array_key_exists('import_settings', $this->request_array) && $this->request_array['import_settings'] == 'true') { if(array_key_exists('import_settings', $this->request_array) && $this->request_array['import_settings'] == 'true') {
$this->preFlightChecks()->importSettings(); $this->preFlightChecks()->importSettings();
} }
@ -207,22 +213,21 @@ class CompanyImport implements ShouldQueue
$company_users = $this->company->users; $company_users = $this->company->users;
$company_owner = $this->company->owner();
nlog("This is a free account"); nlog("This is a free account");
nlog("Backup user count = ".count($backup_users)); nlog("Backup user count = ".count($backup_users));
if(count($backup_users) > 1){ if(count($backup_users) > 1){
$this->message = 'Only one user can be in the import for a Free Account'; // $this->message = 'Only one user can be in the import for a Free Account';
$this->pre_flight_checks_pass = false; // $this->pre_flight_checks_pass = false;
$this->force_user_coalesce = true;
} }
nlog("backup users email = " . $backup_users[0]->email); nlog("backup users email = " . $backup_users[0]->email);
if(count($backup_users) == 1 && $company_owner->email != $backup_users[0]->email) { if(count($backup_users) == 1 && $this->company_owner->email != $backup_users[0]->email) {
$this->message = 'Account emails do not match. Account owner email must match backup user email'; // $this->message = 'Account emails do not match. Account owner email must match backup user email';
$this->pre_flight_checks_pass = false; // $this->pre_flight_checks_pass = false;
$this->force_user_coalesce = true;
} }
$backup_users_emails = array_column($backup_users, 'email'); $backup_users_emails = array_column($backup_users, 'email');
@ -236,8 +241,9 @@ class CompanyImport implements ShouldQueue
if($existing_user_count > 1){ if($existing_user_count > 1){
if($this->account->plan == 'pro'){ if($this->account->plan == 'pro'){
$this->message = 'Pro plan is limited to one user, you have multiple users in the backup file'; // $this->message = 'Pro plan is limited to one user, you have multiple users in the backup file';
$this->pre_flight_checks_pass = false; // $this->pre_flight_checks_pass = false;
$this->force_user_coalesce = true;
} }
if($this->account->plan == 'enterprise'){ if($this->account->plan == 'enterprise'){
@ -268,10 +274,6 @@ class CompanyImport implements ShouldQueue
} }
nlog($this->message);
nlog($this->pre_flight_checks_pass);
return $this; return $this;
} }
@ -1216,11 +1218,22 @@ class CompanyImport implements ShouldQueue
return implode(",", $tmp_arr); return implode(",", $tmp_arr);
} }
/* Transform all IDs from old to new
*
* In the case of users - we need to check if the system
* is attempting to migrate resources above their quota,
*
* ie. > 50 clients or more than 1 user
*/
private function transformId(string $resource, ?string $old): ?int private function transformId(string $resource, ?string $old): ?int
{ {
if(empty($old)) if(empty($old))
return null; return null;
if ($resource == 'users' && $this->force_user_coalesce){
return $this->company_owner->id;
}
if (! array_key_exists($resource, $this->ids)) { if (! array_key_exists($resource, $this->ids)) {
// nlog($this->ids); // nlog($this->ids);
throw new \Exception("Resource {$resource} not available."); throw new \Exception("Resource {$resource} not available.");