Skip to content

Commit

Permalink
Prend en compte le nom de la Company pour la recherche de Person (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
polosson committed Mar 25, 2021
1 parent 5df1c93 commit 98d8399
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Ce projet adhère au principe du [Semantic Versioning](https://semver.org/spec/v
- Ajoute quelques informations (dates, bénéficiaires, techniciens) au survol des événements dans le calendrier (#117).
- Augmente le zoom maximum du calendrier à 6 mois pour élargir la vision globale de la frise temporelle (#118).
- Ajoute le titre des pages dans l'onglet du navigateur.
- Améliore le système de recherche des bénéficiaires pour inclure aussi le nom de la structure associée à la personne (#119).

## 0.11.0 (2021-01-14)

Expand Down
24 changes: 22 additions & 2 deletions server/src/App/Models/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use Robert2\API\Config\Config;
use Robert2\API\Models\Traits\Taggable;
use Robert2\API\Validation\Validator as V;
Expand All @@ -21,8 +22,15 @@ class Person extends BaseModel

protected $orderField = 'last_name';

protected $allowedSearchFields = ['full_name', 'first_name', 'last_name', 'nickname', 'email'];
protected $searchField = 'full_name';
protected $allowedSearchFields = [
'first_name',
'last_name',
'full_name',
'full_name_or_company',
'nickname',
'email',
];
protected $searchField = 'full_name_or_company';

public function __construct(array $attributes = [])
{
Expand Down Expand Up @@ -234,6 +242,18 @@ protected function _setSearchConditions(Builder $builder): Builder
return $builder->where($group);
}

if ($this->searchField === 'full_name_or_company') {
$group = function (Builder $query) use ($term) {
$query->orWhere('first_name', 'like', $term)
->orWhere('last_name', 'like', $term);
};
return $builder
->where($group)
->orWhereHas('company', function (Builder $subQuery) use ($term) {
$subQuery->where('companies.legal_name', 'like', $term);
});
}

return $builder->where($this->searchField, 'like', $term);
}
}
7 changes: 7 additions & 0 deletions server/tests/models/PersonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public function testSetSearch(): void
$result = $this->model->getAll()->get()->toArray();
$this->assertCount(1, $result);
$this->assertEquals('Jean Fountain', $result[0]['full_name']);

// - Search by company name
$this->model->setSearch('Testing');
$result = $this->model->getAll()->get()->toArray();
$this->assertCount(1, $result);
$this->assertEquals('Jean Fountain', $result[0]['full_name']);
$this->assertEquals(1, $result[0]['company_id']);
}

public function testGetAllFilteredOrTagged(): void
Expand Down

0 comments on commit 98d8399

Please sign in to comment.