diff --git a/app/Repositories/VendorRepository.php b/app/Repositories/VendorRepository.php index f1eeb0e25962..51a2a5d960df 100644 --- a/app/Repositories/VendorRepository.php +++ b/app/Repositories/VendorRepository.php @@ -55,19 +55,15 @@ class VendorRepository extends BaseRepository if ($vendor->number == '' || ! $vendor->number) { $vendor->number = $this->getNextVendorNumber($vendor); - } //todo write tests for this and make sure that custom vendor numbers also works as expected from here + } $vendor->saveQuietly(); - if (isset($data['contacts'])) { + if (isset($data['contacts']) || $vendor->contacts()->count() == 0) { $this->contact_repo->save($data, $vendor); } - if (empty($data['name'])) { - $data['name'] = $vendor->present()->name(); - } - - if (array_key_exists('documents', $data)) { + if (array_key_exists('documents', $data) && count($data['documents']) >= 1) { $this->saveDocuments($data['documents'], $vendor); } diff --git a/tests/Feature/VendorApiTest.php b/tests/Feature/VendorApiTest.php index e517d2672574..f42178a26a10 100644 --- a/tests/Feature/VendorApiTest.php +++ b/tests/Feature/VendorApiTest.php @@ -47,6 +47,24 @@ class VendorApiTest extends TestCase Model::reguard(); } + + public function testVendorContactCreation() + { + $data = [ + 'name' => 'hewwo', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/vendors', $data); + + $arr = $response->json(); + + $this->assertEquals('hewwo', $arr['data']['name']); + $this->assertEquals(1, count($arr['data']['contacts'])); + } + public function testVendorLoggedInEvents() { $v = \App\Models\Vendor::factory()->create([ @@ -69,12 +87,6 @@ class VendorApiTest extends TestCase Event::assertDispatched(VendorContactLoggedIn::class); - // $vc->fresh(); - // $v->fresh(); - - // $this->assertNotNull($vc->fresh()->last_login); - // $this->assertNotNull($v->fresh()->last_login); - } public function testVendorLocale()