Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions app/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down Expand Up @@ -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,
Expand All @@ -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),
]);
}
}
}
}
Expand Down
18 changes: 2 additions & 16 deletions app/Console/Commands/SetupProduction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Console\Commands;

use DB;
use App\Account;
use Illuminate\Console\Command;

class SetupProduction extends Command
Expand Down Expand Up @@ -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('-----------------------------');
Expand Down
21 changes: 2 additions & 19 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
15 changes: 5 additions & 10 deletions app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
27 changes: 27 additions & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App;

use Carbon\Carbon;
use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -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.
*
Expand Down
1 change: 0 additions & 1 deletion database/factories/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@

$factory->define(App\ContactFieldType::class, function (Faker\Generator $faker) {
return [
'id' => 1,
'account_id' => 1,
'name' => 'Email',
'protocol' => 'mailto:',
Expand Down
44 changes: 5 additions & 39 deletions database/seeds/FakeContentTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]',
'password' => bcrypt('admin'),
'timezone' => config('app.timezone'),
]);

$this->account = $account;
$this->account = Account::createDefault('John', 'Doe', '[email protected]', 'admin');

$this->faker = Faker::create();

Expand All @@ -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;
Expand Down Expand Up @@ -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' => '[email protected]',
'password' => bcrypt('blank'),
'timezone' => config('app.timezone'),
'remember_token' => str_random(10),
]);
Account::createDefault('Blank', 'State', '[email protected]', 'blank');
}

public function populateFoodPreferencies()
Expand Down Expand Up @@ -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(),
]);

Expand Down Expand Up @@ -491,8 +459,6 @@ public function populateCalls()

public function getRandomGender()
{
$genders = $this->account->genders;

return $genders->random();
return $this->account->genders->random();
}
}
34 changes: 3 additions & 31 deletions database/seeds/FakeUserTableSeeder.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Account;
use Illuminate\Database\Seeder;

class FakeUserTableSeeder extends Seeder
Expand All @@ -11,36 +12,7 @@ class FakeUserTableSeeder extends Seeder
*/
public function run()
{
// 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 protected]',
'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' => '[email protected]',
'password' => bcrypt('blank'),
'timezone' => config('app.timezone'),
'remember_token' => str_random(10),
]);
Account::createDefault('John', 'Doe', '[email protected]', 'admin');
Account::createDefault('Blank', 'State', '[email protected]', 'blank');
}
}
3 changes: 3 additions & 0 deletions tests/Helper/VCardHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 2 additions & 16 deletions tests/Unit/ImportVCardsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,8 @@ public function testItImportsContacts()

private function getUser()
{
$user = new User();
$user->first_name = 'John';
$user->last_name = 'Doe';
$user->email = '[email protected]';
$user->password = bcrypt('secret');
$account = Account::createDefault('John', 'Doe', '[email protected]', '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();
}
}