Browse Source

Add owner global scope to payment and project model

pull/1/head
Nafies Luthfi 8 years ago
parent
commit
c99f7884a4
  1. 21
      app/Entities/Payments/Payment.php
  2. 1
      app/Entities/Payments/PaymentsRepository.php
  3. 21
      app/Entities/Projects/Project.php
  4. 1
      app/Entities/Projects/ProjectsRepository.php
  5. 19
      tests/Feature/InvoiceEntryTest.php
  6. 6
      tests/Feature/ManageFeaturesTest.php
  7. 4
      tests/Feature/ManageProjectsTest.php
  8. 16
      tests/Feature/ManageSubscriptionsTest.php
  9. 20
      tests/Feature/ManageTasksTest.php
  10. 22
      tests/Feature/Payments/ManagePaymentsTest.php
  11. 13
      tests/Feature/Payments/PaymentSearchTest.php
  12. 4
      tests/Unit/Models/InvoiceTest.php

21
app/Entities/Payments/Payment.php

@ -5,6 +5,7 @@ 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;
@ -16,6 +17,26 @@ class Payment extends Model
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);

1
app/Entities/Payments/PaymentsRepository.php

@ -30,7 +30,6 @@ class PaymentsRepository extends BaseRepository
} }
}) })
->with('partner', 'project') ->with('partner', 'project')
->whereOwnerId(auth()->id())
->paginate($this->_paginate); ->paginate($this->_paginate);
} }

21
app/Entities/Projects/Project.php

@ -9,6 +9,7 @@ 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 Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Laracasts\Presenter\PresentableTrait; use Laracasts\Presenter\PresentableTrait;
@ -21,6 +22,26 @@ class Project extends Model
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();
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 nameLink() public function nameLink()
{ {
return link_to_route('projects.show', $this->name, [$this->id]); return link_to_route('projects.show', $this->name, [$this->id]);

1
app/Entities/Projects/ProjectsRepository.php

@ -32,7 +32,6 @@ class ProjectsRepository extends BaseRepository
}) })
->withCount('payments') ->withCount('payments')
->with('customer') ->with('customer')
->whereOwnerId(auth()->id())
->paginate($this->_paginate); ->paginate($this->_paginate);
} }

19
tests/Feature/InvoiceEntryTest.php

@ -16,7 +16,7 @@ class InvoiceEntryTest extends TestCase
$this->adminUserSigningIn(); $this->adminUserSigningIn();
// Add new draft to collection // Add new draft to collection
$cart = new InvoiceDraftCollection();
$cart = new InvoiceDraftCollection();
$draft = $cart->add(new InvoiceDraft()); $draft = $cart->add(new InvoiceDraft());
$this->visit(route('invoices.create')); $this->visit(route('invoices.create'));
@ -31,7 +31,7 @@ class InvoiceEntryTest extends TestCase
$this->visit(route('invoices.create')); $this->visit(route('invoices.create'));
$this->press(trans('invoice.create')); $this->press(trans('invoice.create'));
$cart = new InvoiceDraftCollection();
$cart = new InvoiceDraftCollection();
$draft = $cart->content()->last(); $draft = $cart->content()->last();
$this->seePageIs(route('invoices.create', $draft->draftKey)); $this->seePageIs(route('invoices.create', $draft->draftKey));
} }
@ -41,7 +41,7 @@ class InvoiceEntryTest extends TestCase
{ {
$this->adminUserSigningIn(); $this->adminUserSigningIn();
$cart = new InvoiceDraftCollection();
$cart = new InvoiceDraftCollection();
$draft = new InvoiceDraft(); $draft = new InvoiceDraft();
$cart->add($draft); $cart->add($draft);
@ -99,8 +99,9 @@ class InvoiceEntryTest extends TestCase
/** @test */ /** @test */
public function user_can_update_draft_invoice_detail_and_get_confirm_page() public function user_can_update_draft_invoice_detail_and_get_confirm_page()
{ {
$project = factory(Project::class)->create();
$cart = new InvoiceDraftCollection();
$user = $this->adminUserSigningIn();
$project = factory(Project::class)->create(['owner_id' => $user->agency->id]);
$cart = new InvoiceDraftCollection();
$draft = $cart->add(new InvoiceDraft()); $draft = $cart->add(new InvoiceDraft());
@ -111,7 +112,6 @@ class InvoiceEntryTest extends TestCase
$cart->addItemToDraft($draft->draftKey, $item1); $cart->addItemToDraft($draft->draftKey, $item1);
$cart->addItemToDraft($draft->draftKey, $item2); $cart->addItemToDraft($draft->draftKey, $item2);
$this->adminUserSigningIn();
$this->visit(route('invoices.create', $draft->draftKey)); $this->visit(route('invoices.create', $draft->draftKey));
$this->type($project->id, 'project_id'); $this->type($project->id, 'project_id');
@ -136,7 +136,9 @@ class InvoiceEntryTest extends TestCase
$item1 = new Item(['description' => 'Deskripsi item invoice', 'amount' => 1000]); $item1 = new Item(['description' => 'Deskripsi item invoice', 'amount' => 1000]);
$item2 = new Item(['description' => 'Deskripsi item invoice', 'amount' => 2000]); $item2 = new Item(['description' => 'Deskripsi item invoice', 'amount' => 2000]);
$project = factory(Project::class)->create();
$user = $this->adminUserSigningIn();
$project = factory(Project::class)->create(['owner_id' => $user->agency->id]);
// Add items to draft // Add items to draft
$cart->addItemToDraft($draft->draftKey, $item1); $cart->addItemToDraft($draft->draftKey, $item1);
@ -144,11 +146,10 @@ class InvoiceEntryTest extends TestCase
$draftAttributes = [ $draftAttributes = [
'project_id' => $project->id, 'project_id' => $project->id,
'notes' => 'Catatan',
'notes' => 'Catatan',
]; ];
$cart->updateDraftAttributes($draft->draftKey, $draftAttributes); $cart->updateDraftAttributes($draft->draftKey, $draftAttributes);
$user = $this->adminUserSigningIn();
$this->visit(route('invoices.create', [$draft->draftKey, 'action' => 'confirm'])); $this->visit(route('invoices.create', [$draft->draftKey, 'action' => 'confirm']));
$this->press(trans('invoice.save')); $this->press(trans('invoice.save'));

6
tests/Feature/ManageFeaturesTest.php

@ -2,6 +2,7 @@
namespace Tests\Feature; namespace Tests\Feature;
use App\Entities\Agencies\Agency;
use App\Entities\Projects\Feature; use App\Entities\Projects\Feature;
use App\Entities\Projects\Project; use App\Entities\Projects\Project;
use App\Entities\Projects\Task; use App\Entities\Projects\Task;
@ -45,10 +46,11 @@ class ManageFeaturesTest extends TestCase
/** @test */ /** @test */
public function admin_can_edit_feature_data() public function admin_can_edit_feature_data()
{ {
$user = factory(User::class, 3)->create();
$user = factory(User::class, 3)->create();
$agency = factory(Agency::class)->create(['owner_id' => $user[0]->id]);
$this->actingAs($user[0]); $this->actingAs($user[0]);
$project = factory(Project::class)->create(['owner_id' => $user[0]->id]);
$project = factory(Project::class)->create(['owner_id' => $agency->id]);
$feature = factory(Feature::class)->create(['worker_id' => $user[1]->id, 'project_id' => $project->id]); $feature = factory(Feature::class)->create(['worker_id' => $user[1]->id, 'project_id' => $project->id]);

4
tests/Feature/ManageProjectsTest.php

@ -77,10 +77,10 @@ class ManageProjectsTest extends TestCase
{ {
$user = $this->adminUserSigningIn(); $user = $this->adminUserSigningIn();
$project = factory(Project::class)->create(['owner_id' => $user->id]);
$project = factory(Project::class)->create(['owner_id' => $user->agency->id]);
$feature = factory(Feature::class)->create(['project_id' => $project->id]); $feature = factory(Feature::class)->create(['project_id' => $project->id]);
$task = factory(Task::class)->create(['feature_id' => $feature->id]); $task = factory(Task::class)->create(['feature_id' => $feature->id]);
$payment = factory(Payment::class)->create(['project_id' => $project->id]);
$payment = factory(Payment::class)->create(['project_id' => $project->id, 'owner_id' => $user->agency->id]);
$this->visit('projects/'.$project->id); $this->visit('projects/'.$project->id);
$this->click(trans('app.edit')); $this->click(trans('app.edit'));

16
tests/Feature/ManageSubscriptionsTest.php

@ -14,7 +14,7 @@ class ManageSubscriptionsTest extends TestCase
{ {
$user = $this->adminUserSigningIn(); $user = $this->adminUserSigningIn();
$vendor = factory(Partner::class)->create(); $vendor = factory(Partner::class)->create();
$project = factory(Project::class)->create();
$project = factory(Project::class)->create(['owner_id' => $user->agency->id]);
$customer = factory(Partner::class)->create(); $customer = factory(Partner::class)->create();
$this->visit(route('subscriptions.index')); $this->visit(route('subscriptions.index'));
@ -55,7 +55,7 @@ class ManageSubscriptionsTest extends TestCase
$user = $this->adminUserSigningIn(); $user = $this->adminUserSigningIn();
$vendor = factory(Partner::class)->create(); $vendor = factory(Partner::class)->create();
$eppCode = str_random(10); $eppCode = str_random(10);
$project = factory(Project::class)->create();
$project = factory(Project::class)->create(['owner_id' => $user->agency->id]);
$customer = factory(Partner::class)->create(); $customer = factory(Partner::class)->create();
$subscription = factory(Subscription::class)->create(['customer_id' => $customer->id, 'project_id' => $project->id]); $subscription = factory(Subscription::class)->create(['customer_id' => $customer->id, 'project_id' => $project->id]);
@ -94,9 +94,9 @@ class ManageSubscriptionsTest extends TestCase
/** @test */ /** @test */
public function admin_can_delete_a_subscription() public function admin_can_delete_a_subscription()
{ {
$user = $this->adminUserSigningIn();
$subscription = factory(Subscription::class)->create();
$user = $this->adminUserSigningIn();
$project = factory(Project::class)->create(['owner_id' => $user->agency->id]);
$subscription = factory(Subscription::class)->create(['project_id' => $project->id]);
$this->visit(route('subscriptions.edit', $subscription->id)); $this->visit(route('subscriptions.edit', $subscription->id));
$this->click(trans('subscription.delete')); $this->click(trans('subscription.delete'));
@ -110,9 +110,9 @@ class ManageSubscriptionsTest extends TestCase
/** @test */ /** @test */
public function admin_can_see_a_subscription() public function admin_can_see_a_subscription()
{ {
$user = $this->adminUserSigningIn();
$subscription = factory(Subscription::class)->create();
$user = $this->adminUserSigningIn();
$project = factory(Project::class)->create(['owner_id' => $user->agency->id]);
$subscription = factory(Subscription::class)->create(['project_id' => $project->id]);
$this->visit(route('subscriptions.show', $subscription->id)); $this->visit(route('subscriptions.show', $subscription->id));

20
tests/Feature/ManageTasksTest.php

@ -3,8 +3,8 @@
namespace Tests\Feature; namespace Tests\Feature;
use App\Entities\Projects\Feature; use App\Entities\Projects\Feature;
use App\Entities\Projects\Project;
use App\Entities\Projects\Task; use App\Entities\Projects\Task;
use App\Entities\Users\User;
use Tests\TestCase; use Tests\TestCase;
class ManageTasksTest extends TestCase class ManageTasksTest extends TestCase
@ -12,10 +12,10 @@ class ManageTasksTest extends TestCase
/** @test */ /** @test */
public function admin_can_entry_task() public function admin_can_entry_task()
{ {
$user = factory(User::class)->create();
$this->actingAs($user);
$user = $this->adminUserSigningIn();
$project = factory(Project::class)->create(['owner_id' => $user->agency->id]);
$feature = factory(Feature::class)->create(['worker_id' => $user->id]);
$feature = factory(Feature::class)->create(['worker_id' => $user->id, 'project_id' => $project->id]);
$this->visit('features/'.$feature->id); $this->visit('features/'.$feature->id);
$this->seePageIs('features/'.$feature->id); $this->seePageIs('features/'.$feature->id);
$this->see(trans('feature.tasks')); $this->see(trans('feature.tasks'));
@ -41,10 +41,10 @@ class ManageTasksTest extends TestCase
/** @test */ /** @test */
public function admin_can_edit_task_data() public function admin_can_edit_task_data()
{ {
$user = factory(User::class)->create();
$this->actingAs($user);
$user = $this->adminUserSigningIn();
$project = factory(Project::class)->create(['owner_id' => $user->agency->id]);
$feature = factory(Feature::class)->create(['worker_id' => $user->id]);
$feature = factory(Feature::class)->create(['worker_id' => $user->id, 'project_id' => $project->id]);
$task = factory(Task::class)->create(['feature_id' => $feature->id]); $task = factory(Task::class)->create(['feature_id' => $feature->id]);
@ -71,10 +71,10 @@ class ManageTasksTest extends TestCase
/** @test */ /** @test */
public function admin_can_delete_a_task() public function admin_can_delete_a_task()
{ {
$user = factory(User::class)->create();
$this->actingAs($user);
$user = $this->adminUserSigningIn();
$project = factory(Project::class)->create(['owner_id' => $user->agency->id]);
$feature = factory(Feature::class)->create(['worker_id' => $user->id]);
$feature = factory(Feature::class)->create(['worker_id' => $user->id, 'project_id' => $project->id]);
$task = factory(Task::class)->create(['feature_id' => $feature->id]); $task = factory(Task::class)->create(['feature_id' => $feature->id]);

22
tests/Feature/Payments/ManagePaymentsTest.php

@ -13,8 +13,8 @@ class ManagePaymentsTest extends TestCase
public function admin_can_entry_project_an_income_payment() public function admin_can_entry_project_an_income_payment()
{ {
$user = $this->adminUserSigningIn(); $user = $this->adminUserSigningIn();
$customer = factory(Partner::class)->create();
$project = factory(Project::class)->create();
$customer = factory(Partner::class)->create(['owner_id' => $user->agency->id]);
$project = factory(Project::class)->create(['owner_id' => $user->agency->id]);
$this->visit(route('payments.index')); $this->visit(route('payments.index'));
$this->seePageIs(route('payments.index')); $this->seePageIs(route('payments.index'));
@ -44,8 +44,8 @@ class ManagePaymentsTest extends TestCase
public function admin_can_entry_project_an_expanse_payment() public function admin_can_entry_project_an_expanse_payment()
{ {
$user = $this->adminUserSigningIn(); $user = $this->adminUserSigningIn();
$vendor = factory(Partner::class)->create();
$project = factory(Project::class)->create();
$vendor = factory(Partner::class)->create(['owner_id' => $user->agency->id]);
$project = factory(Project::class)->create(['owner_id' => $user->agency->id]);
$this->visit(route('payments.index')); $this->visit(route('payments.index'));
$this->seePageIs(route('payments.index')); $this->seePageIs(route('payments.index'));
@ -76,13 +76,13 @@ class ManagePaymentsTest extends TestCase
public function admin_can_edit_payment_data() public function admin_can_edit_payment_data()
{ {
$user = $this->adminUserSigningIn(); $user = $this->adminUserSigningIn();
$customer = factory(Partner::class)->create();
$project = factory(Project::class)->create();
$customer = factory(Partner::class)->create(['owner_id' => $user->agency->id]);
$project = factory(Project::class)->create(['owner_id' => $user->agency->id]);
$payment = factory(Payment::class)->create([ $payment = factory(Payment::class)->create([
'partner_id' => $customer->id, 'partner_id' => $customer->id,
'project_id' => $project->id, 'project_id' => $project->id,
'owner_id' => $user->id,
'owner_id' => $user->agency->id,
]); ]);
$this->visit(route('payments.edit', $payment->id)); $this->visit(route('payments.edit', $payment->id));
@ -107,9 +107,13 @@ class ManagePaymentsTest extends TestCase
/** @test */ /** @test */
public function admin_can_delete_a_payment() public function admin_can_delete_a_payment()
{ {
$user = $this->adminUserSigningIn();
$user = $this->adminUserSigningIn();
$project = factory(Project::class)->create(['owner_id' => $user->agency->id]);
$payment = factory(Payment::class)->create([
'project_id' => $project->id,
'owner_id' => $user->agency->id,
]);
$payment = factory(Payment::class)->create(['owner_id' => $user->id]);
$this->visit(route('payments.index')); $this->visit(route('payments.index'));
$this->click(trans('app.edit')); $this->click(trans('app.edit'));
$this->click(trans('payment.delete')); $this->click(trans('payment.delete'));

13
tests/Feature/Payments/PaymentSearchTest.php

@ -12,9 +12,10 @@ class PaymentSearchTest extends TestCase
public function user_can_find_payment_by_project_name() public function user_can_find_payment_by_project_name()
{ {
$admin = $this->adminUserSigningIn(); $admin = $this->adminUserSigningIn();
$project = factory(Project::class)->create(['name' => 'Project']);
$payment = factory(Payment::class)->create(['owner_id' => $admin->id, 'project_id' => $project->id]);
$unShownPayment = factory(Payment::class)->create(['owner_id' => $admin->id]);
$project = factory(Project::class)->create(['owner_id' => $admin->agency->id, 'name' => 'Project']);
$payment = factory(Payment::class)->create(['owner_id' => $admin->agency->id, 'project_id' => $project->id]);
$project2 = factory(Project::class)->create(['owner_id' => $admin->agency->id]);
$unShownPayment = factory(Payment::class)->create(['owner_id' => $admin->agency->id, 'project_id' => $project2->id]);
$this->visit(route('payments.index')); $this->visit(route('payments.index'));
$this->submitForm(trans('app.search'), [ $this->submitForm(trans('app.search'), [
@ -31,8 +32,10 @@ class PaymentSearchTest extends TestCase
public function partner_find_payment_by_customer_id() public function partner_find_payment_by_customer_id()
{ {
$admin = $this->adminUserSigningIn(); $admin = $this->adminUserSigningIn();
$payment = factory(Payment::class)->create(['owner_id' => $admin->id]);
$unShownPayment = factory(Payment::class)->create(['owner_id' => $admin->id]);
$project = factory(Project::class)->create(['owner_id' => $admin->agency->id, 'name' => 'Project']);
$payment = factory(Payment::class)->create(['owner_id' => $admin->agency->id, 'project_id' => $project->id]);
$project2 = factory(Project::class)->create(['owner_id' => $admin->agency->id]);
$unShownPayment = factory(Payment::class)->create(['owner_id' => $admin->agency->id, 'project_id' => $project2->id]);
$this->visit(route('payments.index')); $this->visit(route('payments.index'));
$this->submitForm(trans('app.search'), [ $this->submitForm(trans('app.search'), [

4
tests/Unit/Models/InvoiceTest.php

@ -19,9 +19,9 @@ class InvoiceTest extends TestCase
public function it_generates_its_own_number() public function it_generates_its_own_number()
{ {
$invoice1 = factory(Invoice::class)->create(); $invoice1 = factory(Invoice::class)->create();
$this->assertEquals(date('ym') . '001', $invoice1->number);
$this->assertEquals(date('ym').'001', $invoice1->number);
$invoice2 = factory(Invoice::class)->create(); $invoice2 = factory(Invoice::class)->create();
$this->assertEquals(date('ym') . '002', $invoice2->number);
$this->assertEquals(date('ym').'002', $invoice2->number);
} }
} }
Loading…
Cancel
Save