Skip to content

Commit

Permalink
Add active filter in task types endpoint
Browse files Browse the repository at this point in the history
By default it fetches only active task types. We'll need the inactive
ones only for managing task types.
  • Loading branch information
anarute committed Dec 11, 2023
1 parent 7ced7ef commit 231534b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
5 changes: 3 additions & 2 deletions api/routers/v1/timelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@


@router.get(
"/task_types/",
"/task_types",
response_model=List[TaskTypeItem],
dependencies=[Depends(PermissionsValidator(required_permissions=["task_type:read"]))],
)
async def get_task_types(
current_user=Depends(get_current_user),
db: Session = Depends(get_db),
active: bool = True,
skip: int = 0,
limit: int = 100,
):
items = TaskTypeService(db).get_items()
items = TaskTypeService(db).get_items(active)
return items


Expand Down
7 changes: 5 additions & 2 deletions api/services/timelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@


class TaskTypeService(AppService):
def get_items(self) -> List[TaskType]:
task_types = self.db.query(TaskType).all() or []
def get_items(self, active) -> List[TaskType]:
query = self.db.query(TaskType)
if active:
query = query.filter(TaskType.active)
task_types = query.order_by(TaskType.name).all() or []
return task_types

def slug_is_valid(self, slug: str) -> bool:
Expand Down
20 changes: 19 additions & 1 deletion api/tests/routers/v1/test_timelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,31 @@
def test_get_task_types_authenticated(client: TestClient, get_regular_user_token_headers: Dict[str, str]) -> None:
expected_types = [
{"slug": "meeting", "name": "Meeting", "active": True},
{"slug": "project", "name": "Project time", "active": True},
]

response = client.get(
f"{API_BASE_URL}/v1/timelog/task_types",
headers=get_regular_user_token_headers,
)
assert response.status_code == HTTPStatus.OK
task_types = response.json()
assert task_types == expected_types


def test_get_task_types_including_inactive_ones(
client: TestClient, get_regular_user_token_headers: Dict[str, str]
) -> None:
expected_types = [
{"slug": "deprecated", "name": "Deprecated Type", "active": False},
{"slug": "meeting", "name": "Meeting", "active": True},
{"slug": "project", "name": "Project time", "active": True},
]

response = client.get(
f"{API_BASE_URL}/v1/timelog/task_types/",
f"{API_BASE_URL}/v1/timelog/task_types",
headers=get_regular_user_token_headers,
params={"active": False},
)
assert response.status_code == HTTPStatus.OK
task_types = response.json()
Expand Down
2 changes: 1 addition & 1 deletion web/services/getTaskTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
break;
}

$taskTypes = makeAPIRequest("/v1/timelog/task_types/");
$taskTypes = makeAPIRequest("/v1/timelog/task_types");
if (array_key_exists('token_refresh_error', $taskTypes)) {
$response['success'] = false;
$response['error'] = 'token_refresh_error';
Expand Down

0 comments on commit 231534b

Please sign in to comment.