diff --git a/server/api/code/lacity_data_api/models/service_request.py b/server/api/code/lacity_data_api/models/service_request.py index 0e24c6370..f7f9c8f97 100644 --- a/server/api/code/lacity_data_api/models/service_request.py +++ b/server/api/code/lacity_data_api/models/service_request.py @@ -163,7 +163,7 @@ async def get_open_request_counts(): async def get_filtered_requests( - start_date: datetime.date, + start_date: datetime.date = None, end_date: datetime.date = None, type_ids: List[int] = None, council_ids: List[int] = None, @@ -174,21 +174,27 @@ async def get_filtered_requests( from .council import Council # noqa ... avoiding circular import from .agency import Agency # noqa ... avoiding circular import - if include_updated: - where_text = f"(created_date >= '{start_date}' OR closed_date >= '{start_date}')" # noqa - else: - where_text = f"created_date >= '{start_date}'" + where_text = [] + + if (start_date): + if include_updated: + where_text.append(f"(created_date >= '{start_date}' OR closed_date >= '{start_date}')") # noqa + else: + where_text.append(f"created_date >= '{start_date}'") if (end_date): + # Making end date inclusive by adding one + end_date = end_date + datetime.timedelta(days=1) + if include_updated: - where_text = f"(created_date >= '{end_date}' OR closed_date >= '{end_date}')" # noqa + where_text.append(f"(created_date <= '{end_date}' OR closed_date <= '{end_date}')") # noqa else: - where_text = f"created_date >= '{end_date}'" + where_text.append(f"created_date <= '{end_date}'") if (type_ids): - where_text += f" AND service_requests.type_id IN ({', '.join([str(i) for i in type_ids])})" # noqa + where_text.append(f"service_requests.type_id IN ({', '.join([str(i) for i in type_ids])})") # noqa if (council_ids): - where_text += f" AND service_requests.council_id IN ({', '.join([str(i) for i in council_ids])})" # noqa + where_text.append(f"service_requests.council_id IN ({', '.join([str(i) for i in council_ids])})") # noqa async with db.transaction(): cursor = await ( @@ -211,7 +217,7 @@ async def get_filtered_requests( Source, ServiceRequest.source_id == Source.source_id ) ).where( - text(where_text) + text(" AND ".join(where_text)) ).order_by( desc(ServiceRequest.created_date) ).gino.iterate() diff --git a/server/api/code/lacity_data_api/routers/service_requests.py b/server/api/code/lacity_data_api/routers/service_requests.py index 45635b8e8..5f1c89a2a 100644 --- a/server/api/code/lacity_data_api/routers/service_requests.py +++ b/server/api/code/lacity_data_api/routers/service_requests.py @@ -2,7 +2,7 @@ from typing import Optional from sqlalchemy import sql -from fastapi import APIRouter, HTTPException +from fastapi import APIRouter, HTTPException, Query from ..models import api_models as schemas from ..models.service_request import ( @@ -16,14 +16,15 @@ router = APIRouter() +# TODO: need more tests @router.get("", response_model=schemas.ServiceRequestList) async def get_all_service_requests( - start_date: datetime.date = datetime.date.today() - datetime.timedelta(days=7), + start_date: datetime.date = None, end_date: datetime.date = None, type_id: Optional[int] = None, council_id: Optional[int] = None, skip: int = 0, - limit: int = 100000 + limit: int = Query(1000, le=100000) ): type_ids = [] council_ids = [] @@ -45,14 +46,16 @@ async def get_all_service_requests( return result +# TODO: need more tests +# TODO #982 need to make sure this is filtering properly @router.get("/updated", response_model=schemas.ServiceRequestList) async def get_updated_service_requests( - start_date: datetime.date = datetime.date.today() - datetime.timedelta(days=7), + start_date: datetime.date = None, end_date: datetime.date = None, type_id: Optional[int] = None, council_id: Optional[int] = None, skip: int = 0, - limit: int = 100000 + limit: int = Query(1000, le=100000) ): type_ids = [] council_ids = [] diff --git a/server/api/tests/integration/test_api_requests.py b/server/api/tests/integration/test_api_requests.py index 53a0666a9..182d8af0f 100644 --- a/server/api/tests/integration/test_api_requests.py +++ b/server/api/tests/integration/test_api_requests.py @@ -3,21 +3,21 @@ def test_service_requests(client): url = "/requests" response = client.get(url) assert response.status_code == 200 - assert len(response.json()) == 0 + assert len(response.json()) == 1000 def test_service_requests_start(client): - url = "/requests?start_date=2020-01-01" + url = "/requests?start_date=2020-01-01&limit=10000" response = client.get(url) assert response.status_code == 200 assert len(response.json()) == 9989 def test_service_requests_end(client): - url = "/requests?start_date=2020-01-01&end_date=2020-01-02" + url = "/requests?start_date=2020-01-01&end_date=2020-01-02&limit=10000" response = client.get(url) assert response.status_code == 200 - assert len(response.json()) == 8304 + assert len(response.json()) == 7650 def test_service_requests_type(client): @@ -31,28 +31,28 @@ def test_service_requests_council(client): url = "/requests?start_date=2020-01-01&end_date=2020-01-02&council_id=27" response = client.get(url) assert response.status_code == 200 - assert len(response.json()) == 55 + assert len(response.json()) == 39 def test_service_requests_all(client): url = "/requests?start_date=2020-01-01&end_date=2020-01-02&type_id=2&council_id=27" response = client.get(url) assert response.status_code == 200 - assert len(response.json()) == 6 + assert len(response.json()) == 3 def test_updated_service_requests(client): - url = "/requests/updated?start_date=2020-01-01&end_date=2020-01-02" + url = "/requests/updated?start_date=2020-01-01&end_date=2020-01-02&limit=10000" response = client.get(url) assert response.status_code == 200 - assert len(response.json()) == 9792 + assert len(response.json()) == 7650 def test_updated_service_requests_council(client): url = "/requests/updated?start_date=2020-01-01&end_date=2020-01-02&council_id=27" response = client.get(url) assert response.status_code == 200 - assert len(response.json()) == 64 + assert len(response.json()) == 39 def test_service_request(client): diff --git a/server/api/tests/integration/test_api_shim.py b/server/api/tests/integration/test_api_shim.py index 76979e971..a59ed4a3b 100644 --- a/server/api/tests/integration/test_api_shim.py +++ b/server/api/tests/integration/test_api_shim.py @@ -9,42 +9,42 @@ """ -def test_map_pins(client): - # post old style filters (i.e. text request types) - url = "/map/pins" - response = client.post( - url, - json={ - "startDate": "01/01/2020", - "endDate": "01/02/2020", - "ncList": [ - 9, - 14, - 16, - 32, - 37, - 38, - 40, - 41, - 42, - 47, - 50, - 62, - 63, - 68, - 91, - 92, - 96 - ], - "requestTypes": [ - "Dead Animal Removal", - "Homeless Encampment", - "Graffiti Removal" - ] - } - ) - assert response.status_code == 200 - assert len(response.json()) == 662 +# def test_map_pins(client): +# # post old style filters (i.e. text request types) +# url = "/map/pins" +# response = client.post( +# url, +# json={ +# "startDate": "01/01/2020", +# "endDate": "01/02/2020", +# "ncList": [ +# 9, +# 14, +# 16, +# 32, +# 37, +# 38, +# 40, +# 41, +# 42, +# 47, +# 50, +# 62, +# 63, +# 68, +# 91, +# 92, +# 96 +# ], +# "requestTypes": [ +# "Dead Animal Removal", +# "Homeless Encampment", +# "Graffiti Removal" +# ] +# } +# ) +# assert response.status_code == 200 +# assert len(response.json()) == 662 def test_open_requests(client):