Skip to content

Commit

Permalink
should be able to filter work as active/inactive/all (#2130)
Browse files Browse the repository at this point in the history
  • Loading branch information
dinesh-aot authored Apr 18, 2024
1 parent e5e951b commit 6409930
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 27 deletions.
6 changes: 3 additions & 3 deletions epictrack-api/src/api/resources/work.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ class Works(Resource):
@profiletime
def get():
"""Return all active works."""
args = request.args
is_active = args.get("is_active", False, bool)
include_indigenous_nations = args.get('include_indigenous_nations', False, bool)
request_args = req.WorkQueryParameterSchema().load(request.args)
is_active = request_args.get("is_active", None)
include_indigenous_nations = request_args.get('include_indigenous_nations')
works = WorkService.find_all_works(is_active)
exclude = [] if include_indigenous_nations else ['indigenous_works']
works_schema = res.WorkResponseSchema(many=True, exclude=exclude)
Expand Down
20 changes: 16 additions & 4 deletions epictrack-api/src/api/schemas/request/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,19 @@
from .type_request import TypeIdPathParameterSchema
from .user_group_request import UserGroupBodyParamSchema, UserGroupPathParamSchema
from .work_request import (
WorkBodyParameterSchema, WorkExistenceQueryParamSchema, WorkFirstNationImportBodyParamSchema,
WorkFirstNationNotesBodySchema, WorkIdPathParameterSchema, WorkIdPhaseIdPathParameterSchema,
WorkIssuesCreateParameterSchema, WorkIssuesParameterSchema, WorkIssuesUpdateCloneSchema, WorkIssuesUpdateEditSchema,
WorkNotesBodySchema, WorkPlanDownloadQueryParamSchema, WorkStatusParameterSchema, WorkTypeIdQueryParamSchema)
WorkBodyParameterSchema,
WorkExistenceQueryParamSchema,
WorkFirstNationImportBodyParamSchema,
WorkFirstNationNotesBodySchema,
WorkIdPathParameterSchema,
WorkIdPhaseIdPathParameterSchema,
WorkIssuesCreateParameterSchema,
WorkIssuesParameterSchema,
WorkIssuesUpdateCloneSchema,
WorkIssuesUpdateEditSchema,
WorkNotesBodySchema,
WorkPlanDownloadQueryParamSchema,
WorkStatusParameterSchema,
WorkTypeIdQueryParamSchema,
WorkQueryParameterSchema,
)
13 changes: 13 additions & 0 deletions epictrack-api/src/api/schemas/request/work_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,16 @@ class WorkNotesBodySchema(RequestBodyParameterSchema):
validate=validate.OneOf(['status_notes', 'issue_notes']), # Add your predefined types
required=True,
)


class WorkQueryParameterSchema(RequestQueryParameterSchema):
"""Work Query parameters"""

is_active = fields.Bool(
metadata={"description": "query active or inactive ones"},
allow_none=True
)

include_indigenous_nations = fields.Bool(
metadata={"description": "Indicate if the result should have indigenous nations"}
)
4 changes: 2 additions & 2 deletions epictrack-api/src/api/services/work.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ def check_existence(cls, title, work_id=None):
return Work.check_existence(title=title, work_id=work_id)

@classmethod
def find_all_works(cls, default_filters=False):
def find_all_works(cls, is_active=False):
"""Find all non-deleted works"""
works = Work.find_all(default_filters)
works = Work.find_all(is_active)
return works

@classmethod
Expand Down
Binary file not shown.
23 changes: 21 additions & 2 deletions epictrack-api/tests/unit/apis/test_works.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@
from api.services.role import RoleService
from tests.utilities.factory_scenarios import TestRoleEnum, TestWorkFirstNationEnum, TestWorkInfo, TestWorkNotesEnum
from tests.utilities.factory_utils import (
factory_first_nation_model, factory_staff_model, factory_staff_work_role_model, factory_work_first_nation_model,
factory_work_model)
factory_first_nation_model,
factory_staff_model,
factory_staff_work_role_model,
factory_work_first_nation_model,
factory_work_model,
)
from tests.utilities.helpers import prepare_work_payload
from api.utils import util

Expand All @@ -42,7 +46,22 @@
def test_get_works(client, auth_header):
"""Test get works."""
url = urljoin(API_BASE_URL, "works")
factory_work_model()
factory_work_model(TestWorkInfo.work_in_active.value)
result = client.get(url, headers=auth_header)
in_active_works = [work for work in result.json if work['is_active'] is False]
assert len(in_active_works) > 0
assert result.status_code == HTTPStatus.OK


def test_get_active_works(client, auth_header):
"""Test get works."""
url = urljoin(API_BASE_URL, "works?is_active=true")
factory_work_model()
factory_work_model(TestWorkInfo.work_in_active.value)
result = client.get(url, headers=auth_header)
in_active_works = [work for work in result.json if work['is_active'] is False]
assert len(in_active_works) == 0
assert result.status_code == HTTPStatus.OK


Expand Down
14 changes: 14 additions & 0 deletions epictrack-api/tests/utilities/factory_scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ class TestWorkInfo(Enum):
"simple_title": fake.word()
}

work_in_active = {
"report_description": fake.sentence(),
"epic_description": fake.paragraph(),
"is_active": False,
"start_date": fake.date_time_this_decade(tzinfo=CANADA_TIMEZONE).isoformat(),
"ministry_id": 1,
"ea_act_id": 3,
"eao_team_id": 1,
"federal_involvement_id": 1,
"work_type_id": 1,
"substitution_act_id": 1,
"simple_title": fake.word()
}

validation_work = {
"title": fake.word(),
}
Expand Down
33 changes: 18 additions & 15 deletions epictrack-api/tests/utilities/factory_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,24 @@

def factory_project_model(project_data: dict = TestProjectInfo.project1.value):
"""Produce a participant model."""
project = ProjectModel(
name=project_data["name"],
description=project_data["description"],
address=project_data["address"],
type_id=project_data["type_id"],
sub_type_id=project_data["sub_type_id"],
proponent_id=project_data["proponent_id"],
region_id_env=project_data["region_id_env"],
region_id_flnro=project_data["region_id_flnro"],
latitude=project_data["latitude"],
longitude=project_data["longitude"],
abbreviation=project_data["abbreviation"],
project_state=ProjectStateEnum.PRE_WORK.value,
)
project.save()
project_result = ProjectModel.find_by_params({"name": project_data["name"]})
project = project_result[0] if project_result else None
if not project:
project = ProjectModel(
name=project_data["name"],
description=project_data["description"],
address=project_data["address"],
type_id=project_data["type_id"],
sub_type_id=project_data["sub_type_id"],
proponent_id=project_data["proponent_id"],
region_id_env=project_data["region_id_env"],
region_id_flnro=project_data["region_id_flnro"],
latitude=project_data["latitude"],
longitude=project_data["longitude"],
abbreviation=project_data["abbreviation"],
project_state=ProjectStateEnum.PRE_WORK.value,
)
project.save()
return project


Expand Down
2 changes: 1 addition & 1 deletion epictrack-web/src/services/workService/workService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Work } from "../../models/work";
import { WorkType } from "../../models/workType";

class WorkService implements ServiceBase {
async getAll(is_active = false) {
async getAll(is_active = undefined) {
return await http.GetRequest<Work[]>(Endpoints.Works.WORKS, { is_active });
}

Expand Down

0 comments on commit 6409930

Please sign in to comment.