diff --git a/app/Console/Commands/CreateAccount.php b/app/Console/Commands/CreateAccount.php new file mode 100644 index 000000000000..053b7661400b --- /dev/null +++ b/app/Console/Commands/CreateAccount.php @@ -0,0 +1,163 @@ +info(date('r').' Create Single Account...'); + + $this->warmCache(); + + $this->createAccount(); + + } + + private function createAccount() + { + + $account = Account::factory()->create(); + $company = Company::factory()->create([ + 'account_id' => $account->id, + ]); + + $account->default_company_id = $company->id; + $account->save(); + + $email = $this->option('email') ?? 'admin@example.com'; + $password = $this->option('password') ?? 'changeme!'; + + $user = User::factory()->create([ + 'account_id' => $account->id, + 'email' => $email, + 'password' => Hash::make($password), + 'confirmation_code' => $this->createDbHash(config('database.default')), + 'email_verified_at' => now(), + ]); + + $company_token = new CompanyToken; + $company_token->user_id = $user->id; + $company_token->company_id = $company->id; + $company_token->account_id = $account->id; + $company_token->name = 'User Token'; + $company_token->token = Str::random(64); + $company_token->is_system = true; + + $company_token->save(); + + $user->companies()->attach($company->id, [ + 'account_id' => $account->id, + 'is_owner' => 1, + 'is_admin' => 1, + 'is_locked' => 0, + 'notifications' => CompanySettings::notificationDefaults(), + 'settings' => null, + ]); + + CreateCompanyPaymentTerms::dispatchNow($company, $user); + CreateCompanyTaskStatuses::dispatchNow($company, $user); + VersionCheck::dispatchNow(); + + } + + private function warmCache() + { + /* Warm up the cache !*/ + $cached_tables = config('ninja.cached_tables'); + + foreach ($cached_tables as $name => $class) { + if (! Cache::has($name)) { + // check that the table exists in case the migration is pending + if (! Schema::hasTable((new $class())->getTable())) { + continue; + } + if ($name == 'payment_terms') { + $orderBy = 'num_days'; + } elseif ($name == 'fonts') { + $orderBy = 'sort_order'; + } elseif (in_array($name, ['currencies', 'industries', 'languages', 'countries', 'banks'])) { + $orderBy = 'name'; + } else { + $orderBy = 'id'; + } + $tableData = $class::orderBy($orderBy)->get(); + if ($tableData->count()) { + Cache::forever($name, $tableData); + } + } + } + } + +}