diff --git a/app/Services/Client/Merge.php b/app/Services/Client/Merge.php index eeef8e5632e5..a2f830a1bb8a 100644 --- a/app/Services/Client/Merge.php +++ b/app/Services/Client/Merge.php @@ -53,7 +53,7 @@ class Merge extends AbstractService /* Loop through contacts an only merge distinct contacts by email */ $this->mergable_client->contacts->each(function ($contact) { $exist = $this->client->contacts->contains(function ($client_contact) use ($contact) { - return $client_contact->email == $contact->email; + return $client_contact->email == $contact->email || empty($contact->email) || $contact->email == ' '; }); if ($exist) { diff --git a/tests/Feature/ClientTest.php b/tests/Feature/ClientTest.php index 17927bb747fb..83c84201617a 100644 --- a/tests/Feature/ClientTest.php +++ b/tests/Feature/ClientTest.php @@ -63,6 +63,62 @@ class ClientTest extends TestCase $this->makeTestData(); } + + public function testClientMergeContactDrop() + { + + $c = Client::factory()->create(['user_id' => $this->user->id, 'company_id' => $this->company->id]); + + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $c->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + ]); + + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $c->id, + 'company_id' => $this->company->id, + ]); + + + $c1 = Client::factory()->create(['user_id' => $this->user->id, 'company_id' => $this->company->id]); + + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $c1->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + ]); + + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $c1->id, + 'company_id' => $this->company->id, + ]); + + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $c1->id, + 'company_id' => $this->company->id, + 'email' => '' + ]); + + + $this->assertEquals(2, $c->contacts->count()); + $this->assertEquals(3, $c1->contacts->count()); + + $c->service()->merge($c1); + + $c = $c->fresh(); + + nlog($c->contacts->pluck('email')); + + $this->assertEquals(4, $c->contacts->count()); + + } + private function buildLineItems($number = 2) { $line_items = [];