1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-05 18:52:44 +01:00

Refactoring the order of data displayed when creating accounts and companies

This commit is contained in:
David Bomba 2019-06-25 13:55:02 +10:00
parent 518d5f12e7
commit de4e3f5655
9 changed files with 89 additions and 32 deletions

View File

@ -14,7 +14,9 @@ namespace App\Http\Controllers;
use App\Http\Requests\Account\CreateAccountRequest;
use App\Jobs\Account\CreateAccount;
use App\Models\Account;
use App\Models\CompanyUser;
use App\Transformers\AccountTransformer;
use App\Transformers\CompanyUserTransformer;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
@ -65,8 +67,11 @@ class AccountController extends BaseController
{
$account = CreateAccount::dispatchNow($request->all());
$this->entity_type = CompanyUser::class;
$this->entity_transformer = CompanyUserTransformer::class;
return $this->itemResponse($account);
return $this->listResponse($account->default_company->users);
}

View File

@ -94,7 +94,7 @@ class CompanyController extends BaseController
*/
public function store(StoreCompanyRequest $request)
{
// $this->forced_includes = ['company_user'];
$this->forced_includes = ['company_user'];
$company = CreateCompany::dispatchNow($request->all(), auth()->user()->company()->account);
@ -110,10 +110,7 @@ class CompanyController extends BaseController
/*
* Required dependencies
*/
if($user)
auth()->login($user, false);
$user->setCompany($company);
auth()->user()->setCompany($company);
/*
* Create token

View File

@ -53,9 +53,6 @@ class Account extends BaseModel
'discount_expires',
];
protected $with = [
];
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
@ -71,4 +68,14 @@ class Account extends BaseModel
{
return $this->belongsTo(Payment::class);
}
public function companies()
{
return $this->hasMany(Company::class);
}
public function company_users()
{
return $this->hasMany(CompanyUser::class);
}
}

View File

@ -11,6 +11,8 @@
namespace App\Models;
use App\Models\CompanyToken;
use App\Models\User;
use Illuminate\Database\Eloquent\Relations\Pivot;
class CompanyUser extends Pivot
@ -33,13 +35,37 @@ class CompanyUser extends Pivot
return $this->hasOne(Account::class);
}
public function user()
public function user_pivot()
{
return $this->hasOne(User::class)->withPivot('permissions', 'settings', 'is_admin', 'is_owner', 'is_locked');
}
public function company()
public function company_pivot()
{
return $this->hasOne(Company::class)->withPivot('permissions', 'settings', 'is_admin', 'is_owner', 'is_locked');
}
public function user()
{
return $this->hasOne(User::class, 'id', 'user_id');
}
public function company()
{
return $this->hasOne(Company::class, 'id', 'company_id');
}
public function token()
{
return $this->hasOneThrough(
CompanyToken::class,
CompanyUser::class,
'user_id', // Foreign key on CompanyUser table...
'company_id', // Foreign key on CompanyToken table...
'user_id', // Local key on CompanyToken table...
'company_id' // Local key on CompanyUser table...
);
}
}

View File

@ -87,17 +87,6 @@ class User extends Authenticatable implements MustVerifyEmail
return $this->belongsTo(Account::class);
}
/**
* Returns all one company token.
*
* @return Collection
*/
public function token()
{
return $this->tokens->whereCompanyId($this->company()->id)->first();
//return $this->hasOne(CompanyToken::class)->orderBy('id', 'ASC');
}
/**
* Returns all company tokens.
*

View File

@ -13,9 +13,11 @@ namespace App\Transformers;
use App\Models\Account;
use App\Models\Company;
use App\Models\CompanyUser;
use App\Models\Payment;
use App\Models\User;
use App\Transformers\CompanyTransformer;
use App\Transformers\CompanyUserTransformer;
use App\Transformers\UserTransformer;
use App\Utils\Traits\MakesHash;
@ -33,8 +35,9 @@ class AccountTransformer extends EntityTransformer
* @var array
*/
protected $defaultIncludes = [
'default_company',
'user',
//'default_company',
//'user',
'company_users'
];
/**
@ -42,6 +45,8 @@ class AccountTransformer extends EntityTransformer
*/
protected $availableIncludes = [
'default_company',
'company_users',
'companies',
];
@ -54,20 +59,34 @@ class AccountTransformer extends EntityTransformer
*/
public function transform(Account $account)
{
return [
'id' => $this->encodePrimaryKey($account->id),
];
}
public function includeCompanyUsers(Account $account)
{
$transformer = new CompanyUserTransformer($this->serializer);
return $this->includeCollection($account->company_users, $transformer, CompanyUser::class);
}
public function includeDefaultCompany(Account $account)
{
$transformer = new CompanyTransformer($this->serializer);
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,11 +42,11 @@ class CompanyTokenTransformer extends EntityTransformer
public function transform(CompanyToken $company_token)
{
return [
'id' => $this->encodePrimaryKey($company_token->id),
// 'id' => $this->encodePrimaryKey($company_token->id),
'token' => $company_token->token,
'name' => $company_token->name,
'user_id' => $company_token->user_id,
'company_id' => $company_token->company_id,
// 'user_id' => $company_token->user_id,
// 'company_id' => $company_token->company_id,
];
}

View File

@ -12,8 +12,10 @@
namespace App\Transformers;
use App\Models\Company;
use App\Models\CompanyToken;
use App\Models\CompanyUser;
use App\Models\User;
use App\Transformers\CompanyTokenTransformer;
/**
* @SWG\Definition(definition="CompanyUser", @SWG\Xml(name="CompanyUser"))
@ -25,6 +27,9 @@ class CompanyUserTransformer extends EntityTransformer
* @var array
*/
protected $defaultIncludes = [
'company',
'user',
'token'
];
/**
@ -32,14 +37,14 @@ class CompanyUserTransformer extends EntityTransformer
*/
protected $availableIncludes = [
'user',
'company'
'company',
'token'
];
public function transform(CompanyUser $company_user)
{
return [
'id' => (int) $company_user->id,
'permissions' => $company_user->permissions,
'settings' => $company_user->settings,
'is_owner' => (bool) $company_user->is_owner,
@ -68,4 +73,13 @@ class CompanyUserTransformer extends EntityTransformer
}
public function includeToken(CompanyUser $company_user)
{
$transformer = new CompanyTokenTransformer($this->serializer);
return $this->includeItem($company_user->token, $transformer, CompanyToken::class);
}
}

View File

@ -52,8 +52,8 @@ class UserTransformer extends EntityTransformer
* @var array
*/
protected $defaultIncludes = [
// 'company_token',
'token',
// 'company_token',
// 'token',
];
/**
@ -105,7 +105,7 @@ class UserTransformer extends EntityTransformer
$transformer = new CompanyTokenTransformer($this->serializer);
return $this->includeItem($user->token(), $transformer, CompanyToken::class);
return $this->includeItem($user->token, $transformer, CompanyToken::class);
}