From 390242da235eeffcdeac84da688d812599da47b1 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sat, 28 Oct 2017 20:12:26 +0800 Subject: [PATCH] Add owner_id on partners table --- app/Entities/Partners/Partner.php | 2 +- app/Entities/Projects/ProjectsRepository.php | 7 ++++--- app/Http/Controllers/Partners/PartnersController.php | 2 ++ database/factories/PartnerFactory.php | 6 +++++- tests/Feature/ManagePartnersTest.php | 15 ++++++++------- tests/Feature/ManageProjectsTest.php | 3 ++- tests/TestCase.php | 2 ++ 7 files changed, 24 insertions(+), 13 deletions(-) diff --git a/app/Entities/Partners/Partner.php b/app/Entities/Partners/Partner.php index 0b4af50..711ad7c 100644 --- a/app/Entities/Partners/Partner.php +++ b/app/Entities/Partners/Partner.php @@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Model; class Partner extends Model { - protected $fillable = ['name', 'email', 'phone', 'pic', 'address', 'notes', 'is_active']; + protected $fillable = ['name', 'email', 'phone', 'pic', 'address', 'notes', 'is_active', 'owner_id']; public function owner() { diff --git a/app/Entities/Projects/ProjectsRepository.php b/app/Entities/Projects/ProjectsRepository.php index 575fafb..e93ad4b 100755 --- a/app/Entities/Projects/ProjectsRepository.php +++ b/app/Entities/Projects/ProjectsRepository.php @@ -61,9 +61,10 @@ class ProjectsRepository extends BaseRepository public function createNewCustomer($customerName, $customerEmail) { - $newCustomer = new Partner; - $newCustomer->name = $customerName; - $newCustomer->email = $customerEmail; + $newCustomer = new Partner; + $newCustomer->name = $customerName; + $newCustomer->email = $customerEmail; + $newCustomer->owner_id = auth()->user()->agency->id; $newCustomer->save(); return $newCustomer; diff --git a/app/Http/Controllers/Partners/PartnersController.php b/app/Http/Controllers/Partners/PartnersController.php index ba56dbe..8cbd995 100644 --- a/app/Http/Controllers/Partners/PartnersController.php +++ b/app/Http/Controllers/Partners/PartnersController.php @@ -44,6 +44,8 @@ class PartnersController extends Controller 'notes' => 'nullable|max:255', ]); + $newPartnerData['owner_id'] = auth()->user()->agency->id; + Partner::create($newPartnerData); flash(trans('partner.created'), 'success'); diff --git a/database/factories/PartnerFactory.php b/database/factories/PartnerFactory.php index e37d1b8..4dd757b 100644 --- a/database/factories/PartnerFactory.php +++ b/database/factories/PartnerFactory.php @@ -1,11 +1,15 @@ define(Partner::class, function (Faker $faker) { return [ - 'name' => $faker->company, + 'name' => $faker->company, + 'owner_id' => function () { + return factory(Agency::class)->create()->id; + }, ]; }); diff --git a/tests/Feature/ManagePartnersTest.php b/tests/Feature/ManagePartnersTest.php index 6771bd8..bd0bc18 100644 --- a/tests/Feature/ManagePartnersTest.php +++ b/tests/Feature/ManagePartnersTest.php @@ -25,7 +25,7 @@ class ManagePartnersTest extends TestCase /** @test */ public function user_can_create_a_partner() { - $this->adminUserSigningIn(); + $user = $this->adminUserSigningIn(); $this->visit(route('partners.index')); $this->click(trans('partner.create')); @@ -43,12 +43,13 @@ class ManagePartnersTest extends TestCase $this->seePageIs(route('partners.index')); $this->seeInDatabase('partners', [ - 'name' => 'Partner 1 name', - 'email' => 'partner1@mail.com', - 'phone' => '081234567890', - 'pic' => 'Nama PIC Partner', - 'address' => 'Alamat partner 1', - 'notes' => null, + 'name' => 'Partner 1 name', + 'email' => 'partner1@mail.com', + 'phone' => '081234567890', + 'pic' => 'Nama PIC Partner', + 'address' => 'Alamat partner 1', + 'notes' => null, + 'owner_id' => $user->agency->id, ]); } diff --git a/tests/Feature/ManageProjectsTest.php b/tests/Feature/ManageProjectsTest.php index a574807..499c0b4 100644 --- a/tests/Feature/ManageProjectsTest.php +++ b/tests/Feature/ManageProjectsTest.php @@ -36,7 +36,7 @@ class ManageProjectsTest extends TestCase /** @test */ public function admin_can_input_new_project_with_new_partner() { - $this->adminUserSigningIn(); + $user = $this->adminUserSigningIn(); $this->visit(route('projects.index')); $this->seePageIs(route('projects.index')); @@ -68,6 +68,7 @@ class ManageProjectsTest extends TestCase 'name' => 'Project Baru', 'proposal_value' => '2000000', 'customer_id' => $newPartner->id, + 'owner_id' => $user->agency->id, ]); } diff --git a/tests/TestCase.php b/tests/TestCase.php index b04f597..2376318 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,6 +2,7 @@ namespace Tests; +use App\Entities\Agencies\Agency; use App\Entities\Users\User; use Laravel\BrowserKitTesting\TestCase as BaseTestCase; use Tests\Traits\DatabaseMigrateSeeds; @@ -23,6 +24,7 @@ abstract class TestCase extends BaseTestCase protected function adminUserSigningIn() { $user = $this->createUser(); + factory(Agency::class)->create(['owner_id' => $user->id]); $this->actingAs($user); return $user;