Browse Source

Move project owned by agency global scope into trait

pull/1/head
Nafies Luthfi 8 years ago
parent
commit
11effeea10
  1. 22
      app/Entities/Payments/Payment.php
  2. 23
      app/Entities/Projects/Project.php
  3. 19
      app/Traits/OwnedByAgency.php
  4. 14
      tests/Feature/Api/ApiEventsTest.php
  5. 7
      tests/Feature/Api/ApiManageProjectsTest.php
  6. 2
      tests/Feature/Auth/MemberLoginTest.php

22
app/Entities/Payments/Payment.php

@ -5,38 +5,16 @@ namespace App\Entities\Payments;
use App\Entities\Partners\Partner; use App\Entities\Partners\Partner;
use App\Entities\Payments\PaymentPresenter; use App\Entities\Payments\PaymentPresenter;
use App\Entities\Projects\Project; use App\Entities\Projects\Project;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Laracasts\Presenter\PresentableTrait; use Laracasts\Presenter\PresentableTrait;
class Payment extends Model class Payment extends Model
{ {
use PresentableTrait; use PresentableTrait;
protected $presenter = PaymentPresenter::class; protected $presenter = PaymentPresenter::class;
protected $guarded = ['id', 'created_at', 'updated_at']; protected $guarded = ['id', 'created_at', 'updated_at'];
/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();
if (auth()->user()) {
static::addGlobalScope('by_owner', function (Builder $builder) {
if ( ! is_null(auth()->user()->agency)) {
$builder->where('owner_id', auth()->user()->agency->id);
} else {
$builder->where('owner_id', 0);
}
});
}
}
public function project() public function project()
{ {
return $this->belongsTo(Project::class); return $this->belongsTo(Project::class);

23
app/Entities/Projects/Project.php

@ -9,37 +9,18 @@ use App\Entities\Payments\Payment;
use App\Entities\Projects\ProjectPresenter; use App\Entities\Projects\ProjectPresenter;
use App\Entities\Projects\Task; use App\Entities\Projects\Task;
use App\Entities\Subscriptions\Subscription; use App\Entities\Subscriptions\Subscription;
use Illuminate\Database\Eloquent\Builder;
use App\Traits\OwnedByAgency;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Laracasts\Presenter\PresentableTrait; use Laracasts\Presenter\PresentableTrait;
class Project extends Model class Project extends Model
{ {
use PresentableTrait;
use PresentableTrait, OwnedByAgency;
protected $presenter = ProjectPresenter::class; protected $presenter = ProjectPresenter::class;
protected $guarded = ['id', 'created_at', 'updated_at']; protected $guarded = ['id', 'created_at', 'updated_at'];
// protected $dates = ['start_date','end_date']; // protected $dates = ['start_date','end_date'];
/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::addGlobalScope('by_owner', function (Builder $builder) {
if ( ! is_null(auth()->user()->agency)) {
$builder->where('owner_id', auth()->user()->agency->id);
} else {
$builder->where('owner_id', 0);
}
});
}
public function nameLink() public function nameLink()
{ {
return link_to_route('projects.show', $this->name, [$this->id]); return link_to_route('projects.show', $this->name, [$this->id]);

19
app/Traits/OwnedByAgency.php

@ -0,0 +1,19 @@
<?php
namespace App\Traits;
use Illuminate\Database\Eloquent\Builder;
trait OwnedByAgency
{
public static function bootOwnedByAgency()
{
static::addGlobalScope('by_owner', function (Builder $builder) {
if ( ! is_null(auth()->user()->agency)) {
$builder->where('owner_id', auth()->user()->agency->id);
} else {
$builder->where('owner_id', 0);
}
});
}
}

14
tests/Feature/Api/ApiEventsTest.php

@ -16,7 +16,7 @@ class ApiEventsTest extends TestCase
$events = factory(Event::class, 2)->create(['user_id' => $user->id]); $events = factory(Event::class, 2)->create(['user_id' => $user->id]);
$this->getJson(route('api.events.index'), [ $this->getJson(route('api.events.index'), [
'Authorization' => 'Bearer '.$user->api_token
'Authorization' => 'Bearer '.$user->api_token,
]); ]);
$this->seeStatusCode(200); $this->seeStatusCode(200);
@ -30,7 +30,7 @@ class ApiEventsTest extends TestCase
'end', 'end',
'allDay', 'allDay',
'project_id', 'project_id',
]
],
]); ]);
} }
@ -46,11 +46,9 @@ class ApiEventsTest extends TestCase
'start' => '2016-07-21 12:20:00', 'start' => '2016-07-21 12:20:00',
'project_id' => $project->id, 'project_id' => $project->id,
], [ ], [
'Authorization' => 'Bearer '.$user->api_token
'Authorization' => 'Bearer '.$user->api_token,
]); ]);
// $this->dump();
$this->seeStatusCode(201); $this->seeStatusCode(201);
$this->seeJson([ $this->seeJson([
@ -89,7 +87,7 @@ class ApiEventsTest extends TestCase
'body' => 'New Event Body', 'body' => 'New Event Body',
'is_allday' => 'true', 'is_allday' => 'true',
], [ ], [
'Authorization' => 'Bearer '.$user->api_token
'Authorization' => 'Bearer '.$user->api_token,
]); ]);
$this->seeStatusCode(200); $this->seeStatusCode(200);
@ -117,7 +115,7 @@ class ApiEventsTest extends TestCase
$event = factory(Event::class)->create(['user_id' => $user->id]); $event = factory(Event::class)->create(['user_id' => $user->id]);
$this->deleteJson(route('api.events.destroy'), ['id' => $event->id], [ $this->deleteJson(route('api.events.destroy'), ['id' => $event->id], [
'Authorization' => 'Bearer '.$user->api_token
'Authorization' => 'Bearer '.$user->api_token,
]); ]);
$this->seeStatusCode(200); $this->seeStatusCode(200);
@ -138,7 +136,7 @@ class ApiEventsTest extends TestCase
'start' => '2016-11-07 13:00:00', 'start' => '2016-11-07 13:00:00',
'end' => '2016-11-07 15:00:00', 'end' => '2016-11-07 15:00:00',
], [ ], [
'Authorization' => 'Bearer '.$user->api_token
'Authorization' => 'Bearer '.$user->api_token,
]); ]);
// $this->dump(); // $this->dump();

7
tests/Feature/Api/ApiManageProjectsTest.php

@ -3,7 +3,6 @@
namespace Tests\Feature\Api; namespace Tests\Feature\Api;
use App\Entities\Projects\Project; use App\Entities\Projects\Project;
use App\Entities\Users\User;
use Tests\TestCase; use Tests\TestCase;
class ApiManageProjectsTest extends TestCase class ApiManageProjectsTest extends TestCase
@ -11,11 +10,11 @@ class ApiManageProjectsTest extends TestCase
/** @test */ /** @test */
public function user_can_get_project_lists() public function user_can_get_project_lists()
{ {
$user = factory(User::class)->create();
$project = factory(Project::class, 5)->create(['owner_id' => $user->id]);
$user = $this->adminUserSigningIn();
$project = factory(Project::class, 1)->create(['owner_id' => $user->agency->id]);
$this->getJson(route('api.projects.index'), [ $this->getJson(route('api.projects.index'), [
'Authorization' => 'Bearer '.$user->api_token
'Authorization' => 'Bearer '.$user->api_token,
]); ]);
$this->seeStatusCode(200); $this->seeStatusCode(200);

2
tests/Feature/Auth/MemberLoginTest.php

@ -2,6 +2,7 @@
namespace Tests\Feature\Auth; namespace Tests\Feature\Auth;
use App\Entities\Agencies\Agency;
use App\Entities\Users\User; use App\Entities\Users\User;
use Tests\TestCase; use Tests\TestCase;
@ -11,6 +12,7 @@ class MemberLoginTest extends TestCase
public function user_can_login_and_logout() public function user_can_login_and_logout()
{ {
$user = factory(User::class)->create(['name' => 'Nama Member', 'email' => 'email@mail.com']); $user = factory(User::class)->create(['name' => 'Nama Member', 'email' => 'email@mail.com']);
factory(Agency::class)->create(['owner_id' => $user->id]);
$this->visit(route('auth.login')); $this->visit(route('auth.login'));

Loading…
Cancel
Save