1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Refactor for Signup via API

This commit is contained in:
David Bomba 2019-04-18 21:57:22 +10:00
parent 8ad045ecc4
commit ac382fe3e7
11 changed files with 57 additions and 34 deletions

View File

@ -4,6 +4,8 @@ namespace App\Http\Controllers;
use App\Http\Requests\Account\CreateAccountRequest;
use App\Jobs\Account\CreateAccount;
use App\Models\Account;
use App\Transformers\AccountTransformer;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
@ -12,6 +14,10 @@ class AccountController extends BaseController
{
use DispatchesJobs;
protected $entity_type = Account::class;
protected $entity_transformer = AccountTransformer::class;
public function __construct()
{
parent::__construct();
@ -49,9 +55,10 @@ class AccountController extends BaseController
public function store(CreateAccountRequest $request)
{
$user = CreateAccount::dispatchNow($request->all());
$account = CreateAccount::dispatchNow($request->all());
return $this->itemResponse($account);
return response()->json($user);
}
/**

View File

@ -37,7 +37,7 @@ class CreateAccount
*
* @return void
*/
public function handle() : ?User
public function handle() : ?Account
{
/*
* Create account
@ -57,7 +57,7 @@ class CreateAccount
/*
* Create user
*/
$user = CreateUser::dispatchNow($this->request, $account, $company);
$user = CreateUser::dispatchNow($this->request, $account, $company, true); //make user company_owner
/*
* Create token
@ -67,13 +67,13 @@ class CreateAccount
/*
* Login user
*/
Auth::loginUsingId($user->id, true);
//Auth::loginUsingId($user->id, true);
/*
* Fire related events
*/
event(new AccountCreated($user));
return $user;
return $account;
}
}

View File

@ -27,11 +27,12 @@ class CreateUser
* @return void
*/
public function __construct(array $request, $account, $company)
public function __construct(array $request, $account, $company, $company_owner = false)
{
$this->request = $request;
$this->account = $account;
$this->company = $company;
$this->company_owner = $company_owner;
}
/**
@ -41,7 +42,7 @@ class CreateUser
*/
public function handle() : ?User
{
$x = mt_rand(1,100);
$x = mt_rand(1,10000);
$email = 'turbo124+'. $x .'@gmail.com';
@ -56,7 +57,7 @@ class CreateUser
$user->companies()->attach($this->company->id, [
'account_id' => $this->account->id,
'is_owner' => 1,
'is_owner' => $this->company_owner,
'is_admin' => 1,
'is_locked' => 0,
'permissions' => json_encode([]),

View File

@ -44,6 +44,9 @@ class Account extends BaseModel
'discount_expires',
];
protected $with = [
];
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/

View File

@ -21,6 +21,7 @@ use App\Models\Traits\AccountTrait;
use App\Models\User;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use Laracasts\Presenter\PresentableTrait;
class Company extends BaseModel
@ -190,4 +191,11 @@ class Company extends BaseModel
return $this->hasMany(CompanyUser::class);
}
public function owner()
{
$c = $this->company_users->where('is_owner',true)->first();
return User::find($c->user_id);
}
}

View File

@ -3,7 +3,11 @@
namespace App\Transformers;
use App\Models\Account;
use App\Models\Company;
use App\Models\Payment;
use App\Models\User;
use App\Transformers\CompanyTransformer;
use App\Transformers\UserTransformer;
use App\Utils\Traits\MakesHash;
/**
@ -20,6 +24,8 @@ class AccountTransformer extends EntityTransformer
* @var array
*/
protected $defaultIncludes = [
'default_company',
'user',
];
/**
@ -50,4 +56,12 @@ class AccountTransformer extends EntityTransformer
return $this->includeItem($account->default_company, $transformer, Company::class);
}
public function includeUser(Account $account)
{
$transformer = new UserTransformer($this->serializer);
return $this->includeItem($account->default_company->owner(), $transformer, User::class);
}
}

View File

@ -42,7 +42,7 @@ class UserTransformer extends EntityTransformer
*/
protected $defaultIncludes = [
'company_token',
'companies',
];
/**
@ -50,6 +50,7 @@ class UserTransformer extends EntityTransformer
*/
protected $availableIncludes = [
'user_company',
'companies',
];
@ -83,14 +84,14 @@ class UserTransformer extends EntityTransformer
{
$transformer = new CompanyTransformer($this->serializer);
return $this->includeCollection($user->companies(), $transformer, Company::class)
return $this->includeCollection($user->companies(), $transformer, Company::class);
}
public function includeCompanyToken(User $user)
{
$transformer = new CompanyTokenTransformer($this->serializer);
return $this->includeItem($user->token(), $transformer, CompanyToken::class)
return $this->includeItem($user->token(), $transformer, CompanyToken::class);
}

View File

@ -50,10 +50,7 @@ class AccountTest extends TestCase
$response = $this->post('/signup', $data);
$response->assertStatus(200)
->assertJson([
'first_name' => $data['first_name'],
]);
$response->assertStatus(200);
}
@ -65,7 +62,6 @@ class AccountTest extends TestCase
'last_name' => $this->faker->lastName,
'email' => $this->faker->unique()->safeEmail,
'password' => 'ALongAndBrilliantPassword123',
//'_token' => csrf_token(),
'privacy_policy' => 1,
'terms_of_service' => 1
];
@ -75,10 +71,7 @@ class AccountTest extends TestCase
'X-API-SECRET' => config('ninja.api_secret'),
])->post('/api/v1/signup', $data);
$response->assertStatus(200)
->assertJson([
'first_name' => $data['first_name'],
]);
$response->assertStatus(200);
}

View File

@ -51,14 +51,12 @@ class ClientTest extends TestCase
])->post('/api/v1/signup', $data);
$response->assertStatus(200)
->assertJson([
'first_name' => $data['first_name'],
]);
$response->assertStatus(200);
$acc = $response->json();
$account = Account::find($acc['id']);
$account = Account::find($this->decodePrimaryKey($acc['data']['id']));
$token = $account->default_company->tokens->first()->token;
@ -94,9 +92,10 @@ class ClientTest extends TestCase
$acc = $response->json();
$account = Account::find($acc['id']);
$account = Account::find($this->decodePrimaryKey($acc['data']['id']));
$company_token = $account->default_company->tokens()->first();
$token = $company_token->token;
$company = $company_token->company;

View File

@ -54,7 +54,7 @@ class InvoiceTest extends TestCase
$acc = $response->json();
$account = Account::find($acc['id']);
$account = Account::find($this->decodePrimaryKey($acc['data']['id']));
$company_token = $account->default_company->tokens()->first();
$token = $company_token->token;
@ -117,7 +117,7 @@ class InvoiceTest extends TestCase
$acc = $response->json();
$account = Account::find($acc['id']);
$account = Account::find($this->decodePrimaryKey($acc['data']['id']));
$company_token = $account->default_company->tokens()->first();
$token = $company_token->token;

View File

@ -52,14 +52,11 @@ class ProductTest extends TestCase
])->post('/api/v1/signup', $data);
$response->assertStatus(200)
->assertJson([
'first_name' => $data['first_name'],
]);
$response->assertStatus(200);
$acc = $response->json();
$account = Account::find($acc['id']);
$account = Account::find($this->decodePrimaryKey($acc['data']['id']));
$token = $account->default_company->tokens->first()->token;