From 6378bc183df414175a9aee49c94780247ef27b94 Mon Sep 17 00:00:00 2001 From: Michael Bianco Date: Sun, 23 May 2021 12:19:46 -0600 Subject: [PATCH] feat: search notes when searching through contacts (#5103) --- app/Helpers/SearchHelper.php | 3 +++ app/Models/Contact/Contact.php | 29 ++++++++++++++++++++ tests/Unit/Helpers/SearchHelperTest.php | 36 +++++++++++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/app/Helpers/SearchHelper.php b/app/Helpers/SearchHelper.php index 4c2a4737b12..c9bd101976f 100644 --- a/app/Helpers/SearchHelper.php +++ b/app/Helpers/SearchHelper.php @@ -23,6 +23,7 @@ public static function searchContacts(string $needle, string $orderByColumn, str { $accountId = Auth::user()->account_id; + // match against `field: string` queries if (preg_match('/(.{1,})[:](.{1,})/', $needle, $matches)) { $search_field = $matches[1]; $search_term = $matches[2]; @@ -47,6 +48,8 @@ public static function searchContacts(string $needle, string $orderByColumn, str } return Contact::search($needle, $accountId, $orderByColumn, $orderByDirection) + ->notes($accountId, $needle) + ->introductionAdditionalInformation($needle) ->addressBook($accountId, $addressBookName); } } diff --git a/app/Models/Contact/Contact.php b/app/Models/Contact/Contact.php index 895dec029a9..6d1764badc9 100644 --- a/app/Models/Contact/Contact.php +++ b/app/Models/Contact/Contact.php @@ -584,6 +584,35 @@ public function scopeNotActive($query) return $query->where('is_active', 0); } + /** + * Scope a query to include contacts whose notes contain the search phrase. + * + * @param Builder $query + * @return Builder + */ + public function scopeNotes($query, int $accountId = null, string $needle) + { + return $query->orWhereHas('notes', function ($query) use ($accountId, $needle) { + return $query->where([ + ['account_id', $accountId], + ['body', 'like', "%$needle%"], + ]); + }); + } + + /** + * Scope a query to include contacts whose introduction notes contain the search phrase. + * + * @param Builder $query + * @return Builder + */ + public function scopeIntroductionAdditionalInformation($query, string $needle) + { + return $query->orWhere([ + ['first_met_additional_info', 'like', "%$needle%"], + ]); + } + /** * Scope a query to only include contacts from given address book. * 'null' value for address book is the default address book. diff --git a/tests/Unit/Helpers/SearchHelperTest.php b/tests/Unit/Helpers/SearchHelperTest.php index 651dded672d..ca8bb7a15be 100644 --- a/tests/Unit/Helpers/SearchHelperTest.php +++ b/tests/Unit/Helpers/SearchHelperTest.php @@ -3,6 +3,7 @@ namespace Tests\Unit\Helpers; use Tests\FeatureTestCase; +use App\Models\Contact\Note; use App\Helpers\SearchHelper; use App\Models\Contact\Contact; use Illuminate\Foundation\Testing\DatabaseTransactions; @@ -27,6 +28,41 @@ public function searching_for_contacts_returns_a_collection_with_pagination() $this->assertCount(1, $searchResults); } + /** @test */ + public function searching_with_notes() + { + $user = $this->signin(); + + $note = factory(Note::class)->create([ + 'account_id' => $user->account_id, + 'body' => 'we met on github and talked about monica', + ]); + + $searchResults = SearchHelper::searchContacts('monica', 'created_at') + ->paginate(1); + + $this->assertNotNull($searchResults); + $this->assertInstanceOf('Illuminate\Pagination\LengthAwarePaginator', $searchResults); + $this->assertCount(1, $searchResults); + } + + /** @test */ + public function searching_with_introduction_information() + { + $user = $this->signin(); + + $contact = factory(Contact::class)->create([ + 'account_id' => $user->account_id, + 'first_met_additional_info' => 'github', + ]); + $searchResults = SearchHelper::searchContacts($contact->first_met_additional_info, 'created_at') + ->paginate(1); + + $this->assertNotNull($searchResults); + $this->assertInstanceOf('Illuminate\Pagination\LengthAwarePaginator', $searchResults); + $this->assertCount(1, $searchResults); + } + /** @test */ public function searching_with_wrong_search_field() {