Skip to content

Commit

Permalink
feat: search notes when searching through contacts (#5103)
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveitaly committed May 23, 2021
1 parent b9c8181 commit 6378bc1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/Helpers/SearchHelper.php
Expand Up @@ -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];
Expand All @@ -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);
}
}
29 changes: 29 additions & 0 deletions app/Models/Contact/Contact.php
Expand Up @@ -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.
Expand Down
36 changes: 36 additions & 0 deletions tests/Unit/Helpers/SearchHelperTest.php
Expand Up @@ -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;
Expand All @@ -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()
{
Expand Down

0 comments on commit 6378bc1

Please sign in to comment.