working on transformers

This commit is contained in:
David Bomba 2019-03-28 13:36:36 +11:00
parent 47b1adf3a4
commit 88f96be135
8 changed files with 256 additions and 19 deletions

View File

@ -28,8 +28,6 @@ trait VerifiesUserEmail
$user->confirmation_code = null;
$user->save();
$this->setCurrentCompanyId($user->companies()->first()->account->default_company_id);
Auth::loginUsingId($user->id, true);
return redirect()->route('dashboard.index')->with('message', ctrans('texts.security_confirmation'));

View File

@ -61,6 +61,11 @@ class User extends Authenticatable implements MustVerifyEmail
'slack_webhook_url',
];
public function account()
{
return $this->belongsTo(Account::class);
}
public function token()
{
return $this->tokens()->first();
@ -114,7 +119,7 @@ class User extends Authenticatable implements MustVerifyEmail
public function user_company()
{
return $this->user_companies->where('company_id', $this->getCurrentCompanyId())->first();
return $this->user_companies->where('company_id', $this->company()->id)->first();
}
@ -138,7 +143,7 @@ class User extends Authenticatable implements MustVerifyEmail
public function permissions()
{
$permissions = json_decode($this->company()->permissions);
$permissions = json_decode($this->user_company()->permissions);
if (! $permissions)
return [];
@ -154,7 +159,7 @@ class User extends Authenticatable implements MustVerifyEmail
public function settings()
{
return json_decode($this->company()->settings);
return json_decode($this->user_company()->settings);
}

View File

@ -1,6 +1,6 @@
<?php
namespace App\Ninja\Transformers;
namespace App\Transformers;
use App\Models\Account;
use App\Models\Payment;

View File

@ -1,12 +1,15 @@
<?php
namespace App\Ninja\Transformers;
namespace App\Transformers;
use App\Models\Account;
use App\Models\Client;
use App\Models\User;
use App\Utils\Traits\MakesHash;
/**
* Class AccountTransformer.
* Class AccountTranCompanyTransformersformer.
*/
class CompanyTransformer extends EntityTransformer
{
@ -74,4 +77,33 @@ class CompanyTransformer extends EntityTransformer
}
public function includeUsers(Company $company)
{
$transformer = new UserTransformer($this->serializer);
return $this->includeCollection($company->users, $transformer, User::class);
}
public function includeClients(Company $company)
{
$transformer = new ClientTransformer($this->serializer);
return $this->includeCollection($company->clients, $transformer, Client::class);
}
public function includeInvoices(Company $company)
{
$transformer = new InvoiceTransformer($this->serializer);
return $this->includeCollection($company->invoices, $transformer, Invoice::class);
}
public function includeAccount(Company $company)
{
$transformer = new AccountTransformer($this->serializer);
return $this->includeItem($company->account, $transformer, Account::class);
}
}

View File

@ -0,0 +1,62 @@
<?php
namespace App\Transformers;
use App\Models\Company;
use App\Models\CompanyUser;
use App\Models\User;
/**
* @SWG\Definition(definition="CompanyUser", @SWG\Xml(name="CompanyUser"))
*/
class CompanyUserTransformer extends EntityTransformer
{
/**
* @var array
*/
protected $defaultIncludes = [
];
/**
* @var array
*/
protected $availableIncludes = [
'user',
'company'
];
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,
'is_admin' => (bool) $company_user->is_admin,
'is_locked' => (bool) $company_user->is_locked,
'updated_at' => $company_user->updated_at,
'deleted_at' => $company_user->deleted_at,
];
}
public function includeCompany(CompanyUser $company_user)
{
$transformer = new CompanyTransformer($this->serializer);
return $this->includeItem($company_user->company, $transformer, Company::class);
}
public function includeUser(CompanyUser $company_user)
{
$transformer = new UserTransformer($this->serializer);
return $this->includeItem($company_user->user, $transformer, User::class);
}
}

View File

@ -0,0 +1,63 @@
<?php
namespace App\Ninja\Transformers;
use Auth;
use League\Fractal\TransformerAbstract;
class EntityTransformer extends TransformerAbstract
{
protected $serializer;
public function __construct($serializer = null)
{
$this->serializer = $serializer;
}
protected function includeCollection($data, $transformer, $entityType)
{
if ($this->serializer && $this->serializer != API_SERIALIZER_JSON) {
$entityType = null;
}
return $this->collection($data, $transformer, $entityType);
}
protected function includeItem($data, $transformer, $entityType)
{
if ($this->serializer && $this->serializer != API_SERIALIZER_JSON) {
$entityType = null;
}
return $this->item($data, $transformer, $entityType);
}
protected function getTimestamp($date)
{
if (method_exists($date, 'getTimestamp')) {
return $date->getTimestamp();
} elseif (is_string($date)) {
return strtotime($date);
} else {
return null;
}
}
public function getDefaultIncludes()
{
return $this->defaultIncludes;
}
protected function getDefaults($entity)
{
$data = [
'is_owner' => (bool) (Auth::check() && Auth::user()->owns($entity)),
];
if ($entity->relationLoaded('user')) {
$data['user_id'] = (int) $entity->user->public_id + 1;
}
return $data;
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace App\Ninja\Transformers;
namespace App\Transformers;
use App\Models\Account;
use App\Models\Client;
@ -26,6 +26,9 @@ class PaymentTransformer extends EntityTransformer
* @SWG\Property(property="exchange_rate", type="number", format="float", example=10)
* @SWG\Property(property="exchange_currency_id", type="integer", example=1)
*/
protected $serializer;
protected $defaultIncludes = [];
protected $availableIncludes = [
@ -33,31 +36,31 @@ class PaymentTransformer extends EntityTransformer
'invoice',
];
public function __construct($account = null, $serializer = null, $invoice = null)
public function __construct($serializer = null)
{
parent::__construct($account, $serializer);
$this->invoice = $invoice;
$this->serializer = $serializer;
}
public function includeInvoice(Payment $payment)
{
$transformer = new InvoiceTransformer($this->account, $this->serializer);
$transformer = new InvoiceTransformer($this->serializer);
return $this->includeItem($payment->invoice, $transformer, 'invoice');
return $this->includeItem($payment->invoice, $transformer, Invoice::class);
}
public function includeClient(Payment $payment)
{
$transformer = new ClientTransformer($this->account, $this->serializer);
$transformer = new ClientTransformer($this->serializer);
return $this->includeItem($payment->client, $transformer, 'client');
return $this->includeItem($payment->client, $transformer, Client::class);
}
//todo incomplete
public function transform(Payment $payment)
{
return array_merge($this->getDefaults($payment), [
'id' => (int) $payment->public_id,
return [
'id' => (int) $payment->id,
'amount' => (float) $payment->amount,
'transaction_reference' => $payment->transaction_reference ?: '',
'payment_date' => $payment->payment_date ?: '',

View File

@ -0,0 +1,74 @@
<?php
namespace App\Transformers;
use App\Models\Account;
use App\Models\User;
/**
* @SWG\Definition(definition="User", @SWG\Xml(name="User"))
*/
class UserTransformer extends EntityTransformer
{
/**
* @SWG\Property(property="id", type="integer", example=1, readOnly=true)
* @SWG\Property(property="first_name", type="string", example="John")
* @SWG\Property(property="last_name", type="string", example="Doe")
* @SWG\Property(property="email", type="string", example="johndoe@isp.com")
* @SWG\Property(property="account_key", type="string", example="123456")
* @SWG\Property(property="updated_at", type="integer", example=1451160233, readOnly=true)
* @SWG\Property(property="deleted_at", type="integer", example=1451160233, readOnly=true)
* @SWG\Property(property="phone", type="string", example="(212) 555-1212")
* @SWG\Property(property="registered", type="boolean", example=false)
* @SWG\Property(property="confirmed", type="boolean", example=false)
* @SWG\Property(property="oauth_user_id", type="integer", example=1)
* @SWG\Property(property="oauth_provider_id", type="integer", example=1)
* @SWG\Property(property="notify_sent", type="boolean", example=false)
* @SWG\Property(property="notify_viewed", type="boolean", example=false)
* @SWG\Property(property="notify_paid", type="boolean", example=false)
* @SWG\Property(property="notify_approved", type="boolean", example=false)
* @SWG\Property(property="is_admin", type="boolean", example=false)
* @SWG\Property(property="permissions", type="integer", example=1)
*/
/**
* @var array
*/
protected $defaultIncludes = [
];
/**
* @var array
*/
protected $availableIncludes = [
'user_company',
];
public function transform(User $user)
{
return [
'id' => (int) $user->id,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'email' => $user->email,
'updated_at' => $user->updated_at,
'deleted_at' => $user->deleted_at,
'phone' => $user->phone,
'email_verified_at' => $user->email_verified_at,
'oauth_user_id' => $user->oauth_user_id,
'oauth_provider_id' => $user->oauth_provider_id,
'signature' => $user->signature,
];
}
public function includeUserCompany(User $user)
{
$transformer = new UserCompanyTransformer($this->serializer);
return $this->includeItem($user->user_company(), $transformer, CompanyUser::class);
}
}