Skip to content

Commit

Permalink
feat(api): add filter score_qualitee_minimum on list services and sea…
Browse files Browse the repository at this point in the history
…rch services route
  • Loading branch information
hlecuyer committed Dec 19, 2024
1 parent 83e26a2 commit 04ac25b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
18 changes: 18 additions & 0 deletions api/src/data_inclusion/api/inclusion_data/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ def list_services_endpoint(
Chaque résultat renvoyé a (au moins) une typologie dans cette liste."""
),
] = None,
score_qualite_minimum: Annotated[
Optional[float],
fastapi.Query(
description="""[BETA] Score de qualité minimum.
Les résultats renvoyés ont un score de qualité supérieur ou égal à ce
score."""
),
] = None,
inclure_suspendus: Annotated[
Optional[bool],
fastapi.Query(
Expand All @@ -251,6 +259,7 @@ def list_services_endpoint(
profils=profils,
modes_accueil=modes_accueil,
types=types,
score_qualite_minimum=score_qualite_minimum,
include_outdated=inclure_suspendus,
)
background_tasks.add_task(
Expand Down Expand Up @@ -384,6 +393,14 @@ def search_services_endpoint(
Chaque résultat renvoyé a (au moins) une typologie dans cette liste."""
),
] = None,
score_qualite_minimum: Annotated[
Optional[float],
fastapi.Query(
description="""Score de qualité minimum.
Les résultats renvoyés ont un score de qualité supérieur ou égal à ce
score."""
),
] = None,
inclure_suspendus: Annotated[
Optional[bool],
fastapi.Query(
Expand Down Expand Up @@ -445,6 +462,7 @@ def search_services_endpoint(
profils=profils,
types=types,
search_point=search_point,
score_qualite_minimum=score_qualite_minimum,
include_outdated=inclure_suspendus,
)

Expand Down
15 changes: 15 additions & 0 deletions api/src/data_inclusion/api/inclusion_data/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ def filter_services_by_types(
)


def filter_services_by_score_qualite(
query: sqla.Select,
score_qualite_minimum: float,
):
return query.filter(models.Service.score_qualite >= score_qualite_minimum)


def filter_outdated_services(
query: sqla.Select,
):
Expand Down Expand Up @@ -265,6 +272,7 @@ def filter_services(
profils: list[di_schema.Profil] | None = None,
modes_accueil: list[di_schema.ModeAccueil] | None = None,
types: list[di_schema.TypologieService] | None = None,
score_qualite_minimum: float | None = None,
include_outdated: bool | None = False,
) -> sqla.Select:
"""Common filters for services."""
Expand All @@ -287,6 +295,9 @@ def filter_services(
if types is not None:
query = filter_services_by_types(query, types)

if score_qualite_minimum is not None:
query = filter_services_by_score_qualite(query, score_qualite_minimum)

if not include_outdated:
query = filter_outdated_services(query)

Expand All @@ -305,6 +316,7 @@ def list_services(
profils: list[di_schema.Profil] | None = None,
modes_accueil: list[di_schema.ModeAccueil] | None = None,
types: list[di_schema.TypologieService] | None = None,
score_qualite_minimum: float | None = None,
include_outdated: bool | None = False,
):
query = (
Expand Down Expand Up @@ -332,6 +344,7 @@ def list_services(
profils=profils,
modes_accueil=modes_accueil,
types=types,
score_qualite_minimum=score_qualite_minimum,
include_outdated=include_outdated,
)

Expand All @@ -354,6 +367,7 @@ def search_services(
profils: list[di_schema.Profil] | None = None,
types: list[di_schema.TypologieService] | None = None,
search_point: str | None = None,
score_qualite_minimum: float | None = None,
include_outdated: bool | None = False,
):
query = (
Expand Down Expand Up @@ -454,6 +468,7 @@ def search_services(
profils=profils,
modes_accueil=modes_accueil,
types=types,
score_qualite_minimum=score_qualite_minimum,
include_outdated=include_outdated,
)

Expand Down
22 changes: 22 additions & 0 deletions api/tests/e2e/api/__snapshots__/test_inclusion_data.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,17 @@
},
"description": "Une liste de typologies de service.\n Chaque résultat renvoyé a (au moins) une typologie dans cette liste."
},
{
"name": "score_qualite_minimum",
"in": "query",
"required": false,
"schema": {
"type": "number",
"description": "Score de qualité minimum.\n Les résultats renvoyés ont un score de qualité supérieur ou égal à ce\n score.",
"title": "Score Qualite Minimum"
},
"description": "Score de qualité minimum.\n Les résultats renvoyés ont un score de qualité supérieur ou égal à ce\n score."
},
{
"name": "inclure_suspendus",
"in": "query",
Expand Down Expand Up @@ -666,6 +677,17 @@
},
"description": "Une liste de typologies de service.\n Chaque résultat renvoyé a (au moins) une typologie dans cette liste."
},
{
"name": "score_qualite_minimum",
"in": "query",
"required": false,
"schema": {
"type": "number",
"description": "Score de qualité minimum.\n Les résultats renvoyés ont un score de qualité supérieur ou égal à ce\n score.",
"title": "Score Qualite Minimum"
},
"description": "Score de qualité minimum.\n Les résultats renvoyés ont un score de qualité supérieur ou égal à ce\n score."
},
{
"name": "inclure_suspendus",
"in": "query",
Expand Down
21 changes: 21 additions & 0 deletions api/tests/e2e/api/test_inclusion_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,27 @@ def test_list_services_by_types(api_client, url):
assert_paginated_response_data(response.json(), total=0)


@pytest.mark.with_token
@pytest.mark.parametrize("url", ["/api/v0/services", "/api/v0/search/services"])
def test_list_services_by_score_qualite(api_client, url):
service_1 = factories.ServiceFactory(score_qualite=0.5)
service_2 = factories.ServiceFactory(score_qualite=0.7)
factories.ServiceFactory(score_qualite=0.2)

response = api_client.get(
url,
params={"score_qualite_minimum": service_1.score_qualite},
)

assert response.status_code == 200
resp_data = response.json()
assert_paginated_response_data(resp_data, total=2)
assert {d["id"] for d in list_resources_data(resp_data)} == {
service_1.id,
service_2.id,
}


@pytest.mark.with_token
@pytest.mark.parametrize("url", ["/api/v0/services", "/api/v0/search/services"])
def test_can_filter_services_by_frais(api_client, url):
Expand Down

0 comments on commit 04ac25b

Please sign in to comment.