diff --git a/app/Account.php b/app/Account.php index 96f5f1f3d39..d66e876def7 100644 --- a/app/Account.php +++ b/app/Account.php @@ -3,6 +3,7 @@ namespace App; use DB; +use Carbon\Carbon; use Laravel\Cashier\Billable; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; @@ -29,6 +30,32 @@ class Account extends Model 'has_access_to_paid_version_for_free' => 'boolean', ]; + /** + * Create a new account and associate a new User. + * + * @param string $first_name + * @param string $last_name + * @param string $email + * @param string $password + * @return this + */ + public static function createDefault($first_name, $last_name, $email, $password) + { + // create new account + $account = new self; + $account->api_key = str_random(30); + $account->created_at = Carbon::now(); + $account->save(); + + $account->populateContactFieldTypeTable(); + $account->populateDefaultGendersTable(); + + // create the first user for this account + User::createDefault($account->id, $first_name, $last_name, $email, $password); + + return $account; + } + /** * Get the activity records associated with the account. * @@ -398,7 +425,7 @@ public function populateContactFieldTypeTable($ignoreMigratedTable = false) $defaultContactFieldTypes = DB::table('default_contact_field_types')->get(); foreach ($defaultContactFieldTypes as $defaultContactFieldType) { - if ($ignoreMigratedTable == false) { + if (! $ignoreMigratedTable || $defaultContactFieldType->migrated == 0) { $contactFieldType = ContactFieldType::create([ 'account_id' => $this->id, 'name' => $defaultContactFieldType->name, @@ -407,17 +434,6 @@ public function populateContactFieldTypeTable($ignoreMigratedTable = false) 'delible' => $defaultContactFieldType->delible, 'type' => (is_null($defaultContactFieldType->type) ? null : $defaultContactFieldType->type), ]); - } else { - if ($defaultContactFieldType->migrated == 0) { - $contactFieldType = ContactFieldType::create([ - 'account_id' => $this->id, - 'name' => $defaultContactFieldType->name, - 'fontawesome_icon' => (is_null($defaultContactFieldType->fontawesome_icon) ? null : $defaultContactFieldType->fontawesome_icon), - 'protocol' => (is_null($defaultContactFieldType->protocol) ? null : $defaultContactFieldType->protocol), - 'delible' => $defaultContactFieldType->delible, - 'type' => (is_null($defaultContactFieldType->type) ? null : $defaultContactFieldType->type), - ]); - } } } } diff --git a/app/Console/Commands/SetupProduction.php b/app/Console/Commands/SetupProduction.php index 85e5d029468..8543bfd6641 100644 --- a/app/Console/Commands/SetupProduction.php +++ b/app/Console/Commands/SetupProduction.php @@ -2,7 +2,7 @@ namespace App\Console\Commands; -use DB; +use App\Account; use Illuminate\Console\Command; class SetupProduction extends Command @@ -63,21 +63,7 @@ public function handle() $email = $this->ask('Account creation: what should be your email address to login?'); $password = $this->secret('Please choose a password:'); - // populate account table - $accountID = DB::table('accounts')->insertGetId([ - 'api_key' => str_random(30), - ]); - - // populate user table - $userId = DB::table('users')->insertGetId([ - 'account_id' => $accountID, - 'first_name' => 'John', - 'last_name' => 'Doe', - 'email' => $email, - 'password' => bcrypt($password), - 'timezone' => config('app.timezone'), - 'remember_token' => str_random(10), - ]); + Account::createDefault('John', 'Doe', $email, $password); $this->line(''); $this->line('-----------------------------'); diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index a72d200f3aa..fb99281c398 100644 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -6,7 +6,6 @@ use App\User; use Validator; use App\Account; -use Carbon\Carbon; use App\Jobs\SendNewUserAlert; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\RegistersUsers; @@ -81,24 +80,8 @@ protected function validator(array $data) */ protected function create(array $data) { - // create a new account - $account = new Account; - $account->api_key = str_random(30); - $account->created_at = Carbon::now(); - $account->save(); - - $user = new User; - $user->first_name = $data['first_name']; - $user->last_name = $data['last_name']; - $user->email = $data['email']; - $user->password = bcrypt($data['password']); - $user->timezone = config('app.timezone'); - $user->created_at = Carbon::now(); - $user->account_id = $account->id; - $user->save(); - - $account->populateContactFieldTypeTable(); - $account->populateDefaultGendersTable(); + $account = Account::createDefault($data['first_name'], $data['last_name'], $data['email'], $data['password']); + $user = $account->users()->first(); // send me an alert dispatch(new SendNewUserAlert($user)); diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index 01d51a22c92..bde4b7fd3e5 100644 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -7,7 +7,6 @@ use App\Tag; use App\User; use App\ImportJob; -use Carbon\Carbon; use App\Invitation; use Illuminate\Http\Request; use App\Jobs\SendNewUserAlert; @@ -359,15 +358,11 @@ public function storeAcceptedInvitation(Request $request, $key) return redirect()->back()->withErrors(trans('settings.users_error_email_not_similar'))->withInput(); } - $user = new User; - $user->first_name = $request->input('first_name'); - $user->last_name = $request->input('last_name'); - $user->email = $request->input('email'); - $user->password = bcrypt($request->input('password')); - $user->timezone = config('app.timezone'); - $user->created_at = Carbon::now(); - $user->account_id = $invitation->account_id; - $user->save(); + $user = User::createDefault($invitation->account_id, + $request->input('first_name'), + $request->input('last_name'), + $request->input('email'), + $request->input('password')); $invitation->delete(); diff --git a/app/User.php b/app/User.php index 4114bbd5e33..edb96b579fa 100644 --- a/app/User.php +++ b/app/User.php @@ -2,6 +2,7 @@ namespace App; +use Carbon\Carbon; use Laravel\Passport\HasApiTokens; use Illuminate\Notifications\Notifiable; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -35,6 +36,32 @@ class User extends Authenticatable 'password', 'remember_token', 'google2fa_secret', ]; + /** + * Create a new User. + * + * @param int $account_id + * @param string $first_name + * @param string $last_name + * @param string $email + * @param string $password + * @return this + */ + public static function createDefault($account_id, $first_name, $last_name, $email, $password) + { + // create the user + $user = new self; + $user->account_id = $account_id; + $user->first_name = $first_name; + $user->last_name = $last_name; + $user->email = $email; + $user->password = bcrypt($password); + $user->timezone = config('app.timezone'); + $user->created_at = Carbon::now(); + $user->save(); + + return $user; + } + /** * Get the account record associated with the user. * diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index e806f2bb944..1f306312616 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -187,7 +187,6 @@ $factory->define(App\ContactFieldType::class, function (Faker\Generator $faker) { return [ - 'id' => 1, 'account_id' => 1, 'name' => 'Email', 'protocol' => 'mailto:', diff --git a/database/seeds/FakeContentTableSeeder.php b/database/seeds/FakeContentTableSeeder.php index cc006ac2ab7..51ccd97dcdf 100644 --- a/database/seeds/FakeContentTableSeeder.php +++ b/database/seeds/FakeContentTableSeeder.php @@ -22,26 +22,7 @@ class FakeContentTableSeeder extends Seeder */ public function run() { - // populate account table - $accountID = DB::table('accounts')->insertGetId([ - 'api_key' => str_random(30), - ]); - - $account = Account::find($accountID); - $account->populateContactFieldTypeTable(); - $account->populateDefaultGendersTable(); - - // populate user table - $userId = DB::table('users')->insertGetId([ - 'account_id' => $accountID, - 'first_name' => 'John', - 'last_name' => 'Doe', - 'email' => 'admin@admin.com', - 'password' => bcrypt('admin'), - 'timezone' => config('app.timezone'), - ]); - - $this->account = $account; + $this->account = Account::createDefault('John', 'Doe', 'admin@admin.com', 'admin'); $this->faker = Faker::create(); @@ -63,7 +44,7 @@ public function run() $gender = (rand(1, 2) == 1) ? 'male' : 'female'; $this->contact = new Contact; - $this->contact->account_id = $accountID; + $this->contact->account_id = $this->account->id; $this->contact->gender_id = $this->getRandomGender()->id; $this->contact->first_name = $this->faker->firstName($gender); $this->contact->last_name = (rand(1, 2) == 1) ? $this->faker->lastName : null; @@ -106,20 +87,7 @@ public function run() $progress->finish(); // create the second test, blank account - $accountID = DB::table('accounts')->insertGetId([ - 'api_key' => str_random(30), - ]); - - // populate user table - $userId = DB::table('users')->insertGetId([ - 'account_id' => $accountID, - 'first_name' => 'Blank', - 'last_name' => 'State', - 'email' => 'blank@blank.com', - 'password' => bcrypt('blank'), - 'timezone' => config('app.timezone'), - 'remember_token' => str_random(10), - ]); + Account::createDefault('Blank', 'State', 'blank@blank.com', 'blank'); } public function populateFoodPreferencies() @@ -295,7 +263,7 @@ public function populateNotes() $note = $this->contact->notes()->create([ 'body' => $this->faker->realText(rand(40, 500)), 'account_id' => $this->contact->account_id, - 'is_favorited' => (rand(1, 3) == 1 ? true : false), + 'is_favorited' => rand(1, 3) == 1, 'favorited_at' => $this->faker->dateTimeThisCentury(), ]); @@ -491,8 +459,6 @@ public function populateCalls() public function getRandomGender() { - $genders = $this->account->genders; - - return $genders->random(); + return $this->account->genders->random(); } } diff --git a/database/seeds/FakeUserTableSeeder.php b/database/seeds/FakeUserTableSeeder.php index d139017125f..c2cce33912d 100644 --- a/database/seeds/FakeUserTableSeeder.php +++ b/database/seeds/FakeUserTableSeeder.php @@ -1,5 +1,6 @@ insertGetId([ - 'api_key' => str_random(30), - ]); - - // populate user table - $userId = DB::table('users')->insertGetId([ - 'account_id' => $accountID, - 'first_name' => 'John', - 'last_name' => 'Doe', - 'email' => 'admin@admin.com', - 'password' => bcrypt('admin'), - 'timezone' => config('app.timezone'), - 'remember_token' => str_random(10), - ]); - - // create the second test, blank account - $accountID = DB::table('accounts')->insertGetId([ - 'api_key' => str_random(30), - ]); - - // populate user table - $userId = DB::table('users')->insertGetId([ - 'account_id' => $accountID, - 'first_name' => 'Blank', - 'last_name' => 'State', - 'email' => 'blank@blank.com', - 'password' => bcrypt('blank'), - 'timezone' => config('app.timezone'), - 'remember_token' => str_random(10), - ]); + Account::createDefault('John', 'Doe', 'admin@admin.com', 'admin'); + Account::createDefault('Blank', 'State', 'blank@blank.com', 'blank'); } } diff --git a/tests/Helper/VCardHelperTest.php b/tests/Helper/VCardHelperTest.php index ad8abfa427c..d56d3e421ab 100644 --- a/tests/Helper/VCardHelperTest.php +++ b/tests/Helper/VCardHelperTest.php @@ -22,6 +22,7 @@ public function test_it_fetches_all_contact_fields() $contactField = factory(\App\ContactField::class)->create([ 'contact_id' => $contact->id, 'account_id' => $account->id, + 'contact_field_type_id' => $contactFieldType->id, ]); $contactFields = VCardHelper::getAllEntriesOfASpecificContactFieldType($contact, 'email'); @@ -80,6 +81,7 @@ public function test_it_adds_contact_fields_in_vcard() $contactField = factory(\App\ContactField::class)->create([ 'contact_id' => $contact->id, 'account_id' => $account->id, + 'contact_field_type_id' => $contactFieldType->id, ]); $vCard = VCardHelper::addContactFieldEntriesInVCard($contact, $vCard, 'email'); @@ -180,6 +182,7 @@ public function test_it_prepares_an_complete_vcard() $contactField = factory(\App\ContactField::class)->create([ 'contact_id' => $contact->id, 'account_id' => $account->id, + 'contact_field_type_id' => $contactFieldType->id, ]); $vCard = VCardHelper::prepareVCard($contact); diff --git a/tests/Unit/ImportVCardsTest.php b/tests/Unit/ImportVCardsTest.php index 0be7720bb92..da9d78543b2 100644 --- a/tests/Unit/ImportVCardsTest.php +++ b/tests/Unit/ImportVCardsTest.php @@ -99,22 +99,8 @@ public function testItImportsContacts() private function getUser() { - $user = new User(); - $user->first_name = 'John'; - $user->last_name = 'Doe'; - $user->email = 'johndoe@example.com'; - $user->password = bcrypt('secret'); + $account = Account::createDefault('John', 'Doe', 'johndoe@example.com', 'secret'); - $account = new Account(); - $account->api_key = str_random(30); - $account->save(); - - $account->populateContactFieldTypeTable(); - $account->populateDefaultGendersTable(); - - $user->account_id = $account->id; - $user->save(); - - return $user; + return $account->users()->first(); } }