diff --git a/forms-flow-api/src/formsflow_api/models/application.py b/forms-flow-api/src/formsflow_api/models/application.py index 57308f4cf..7285be871 100644 --- a/forms-flow-api/src/formsflow_api/models/application.py +++ b/forms-flow-api/src/formsflow_api/models/application.py @@ -4,7 +4,6 @@ from flask_sqlalchemy.query import Query from formsflow_api_utils.utils import ( - DRAFT_APPLICATION_STATUS, FILTER_MAPS, validate_sort_order_and_order_by, ) @@ -457,7 +456,7 @@ def find_all_by_form_id_count(cls, form_id: str): FormProcessMapper, cls.form_process_mapper_id == FormProcessMapper.id ).filter( cls.latest_form_id == form_id, - cls.application_status != DRAFT_APPLICATION_STATUS, + cls.is_draft.is_(False), ) return FormProcessMapper.tenant_authorization(query=query).count() @@ -705,7 +704,7 @@ def get_total_application_corresponding_to_mapper_id( FormProcessMapper.id == Application.form_process_mapper_id, ) .filter(FormProcessMapper.id == form_process_mapper_id) - .filter(Application.application_status != DRAFT_APPLICATION_STATUS) + .filter(Application.is_draft.is_(False)) .one_or_none() ) # returns a list of one element with count of applications diff --git a/forms-flow-api/src/formsflow_api/models/draft.py b/forms-flow-api/src/formsflow_api/models/draft.py index 38ee44723..639babd7a 100644 --- a/forms-flow-api/src/formsflow_api/models/draft.py +++ b/forms-flow-api/src/formsflow_api/models/draft.py @@ -5,7 +5,6 @@ import uuid from formsflow_api_utils.utils import ( - DRAFT_APPLICATION_STATUS, FILTER_MAPS, validate_sort_order_and_order_by, ) @@ -206,7 +205,7 @@ def get_draft_count(cls, **kwargs): query = query.filter(Application.created_by == user_id) query = query.filter( and_( - Application.application_status == DRAFT_APPLICATION_STATUS, + Application.is_draft.is_(True), Draft.status == str(DraftStatus.ACTIVE.value), ) ) @@ -234,7 +233,7 @@ def get_draft_by_parent_form_id(cls, parent_form_id: str) -> Draft: Application.form_process_mapper_id.in_( [id for id, in get_all_mapper_id] ), - Application.application_status == DRAFT_APPLICATION_STATUS, + Application.is_draft.is_(True), ) ) return FormProcessMapper.tenant_authorization(result).all() diff --git a/forms-flow-api/src/formsflow_api/resources/application.py b/forms-flow-api/src/formsflow_api/resources/application.py index f17dcc5a0..d62dfeb02 100644 --- a/forms-flow-api/src/formsflow_api/resources/application.py +++ b/forms-flow-api/src/formsflow_api/resources/application.py @@ -130,7 +130,7 @@ class ApplicationsResource(Resource): "description": "Filter resources by application name.", "type": "string", }, - "id": { + "Id": { "in": "query", "description": "Filter resources by id.", "type": "int", @@ -145,6 +145,46 @@ class ApplicationsResource(Resource): "description": "Filter resources by modified to.", "type": "string", }, + "createdBy": { + "in": "query", + "description": "Filter resources by created by.", + "type": "string", + }, + "createdFrom": { + "in": "query", + "description": "Filter resources by created from.", + "type": "string", + }, + "createdTo": { + "in": "query", + "description": "Filter resources by created to.", + "type": "string", + }, + "applicationStatus": { + "in": "query", + "description": "Filter resources by application status.", + "type": "string", + }, + "parentFormId": { + "in": "query", + "description": "Filter resources by parent form id.", + "type": "string", + }, + "createdUserSubmissions": { + "in": "query", + "description": "Return user created submissions.", + "type": "bool", + }, + "includeDrafts": { + "in": "query", + "description": "Return submissions and drafts/Specific to client permission.", + "type": "bool", + }, + "onlyDrafts": { + "in": "query", + "description": "Return only drafts/Specific to client permission.", + "type": "bool", + }, } ) @API.response(200, "OK:- Successful request.", model=application_list_model) @@ -159,22 +199,27 @@ class ApplicationsResource(Resource): def get(): # pylint:disable=too-many-locals """Get applications.""" dict_data = ApplicationListRequestSchema().load(request.args) or {} - page_no = dict_data.get("page_no") - limit = dict_data.get("limit") - order_by = dict_data.get("order_by", "id") - application_id = dict_data.get("application_id") - application_name = dict_data.get("application_name") - application_status = dict_data.get("application_status") - created_by = dict_data.get("created_by") - created_from_date = dict_data.get("created_from_date") - created_to_date = dict_data.get("created_to_date") - modified_from_date = dict_data.get("modified_from_date") - modified_to_date = dict_data.get("modified_to_date") - sort_order = dict_data.get("sort_order", "desc") + # Common parameters + common_filters = { + "page_no": dict_data.get("page_no"), + "limit": dict_data.get("limit"), + "order_by": dict_data.get("order_by", "id"), + "application_id": dict_data.get("application_id"), + "application_name": dict_data.get("application_name"), + "application_status": dict_data.get("application_status"), + "created_by": dict_data.get("created_by"), + "created_from": dict_data.get("created_from_date"), + "created_to": dict_data.get("created_to_date"), + "modified_from": dict_data.get("modified_from_date"), + "modified_to": dict_data.get("modified_to_date"), + "sort_order": dict_data.get("sort_order", "desc"), + "parent_form_id": dict_data.get("parent_form_id"), + } + + # Flags + include_drafts = dict_data.get("include_drafts", False) + only_drafts = dict_data.get("only_drafts", False) created_user_submissions = dict_data.get("created_user_submissions", False) - parent_form_id = dict_data.get("parent_form_id") - include_drafts = dict_data.get("include_drafts") - only_drafts = dict_data.get("only_drafts") if auth.has_role([VIEW_TASKS, MANAGE_TASKS]) and not created_user_submissions: ( @@ -182,19 +227,7 @@ def get(): # pylint:disable=too-many-locals application_count, draft_count, ) = ApplicationService.get_auth_applications_and_count( - created_from=created_from_date, - created_to=created_to_date, - modified_from=modified_from_date, - modified_to=modified_to_date, - order_by=order_by, - sort_order=sort_order, - created_by=created_by, - application_id=application_id, - application_name=application_name, - application_status=application_status, - page_no=page_no, - limit=limit, - parent_form_id=parent_form_id, + filters=common_filters ) else: ( @@ -202,19 +235,7 @@ def get(): # pylint:disable=too-many-locals application_count, draft_count, ) = ApplicationService.get_all_applications_by_user( - page_no=page_no, - limit=limit, - order_by=order_by, - sort_order=sort_order, - created_from=created_from_date, - created_to=created_to_date, - modified_from=modified_from_date, - modified_to=modified_to_date, - created_by=created_by, - application_id=application_id, - application_name=application_name, - application_status=application_status, - parent_form_id=parent_form_id, + filters=common_filters, include_drafts=include_drafts, only_drafts=only_drafts, ) @@ -224,8 +245,8 @@ def get(): # pylint:disable=too-many-locals "applications": application_schema_dump, "totalCount": application_count, "draftCount": draft_count, - "limit": limit, - "pageNo": page_no, + "limit": common_filters["limit"], + "pageNo": common_filters["page_no"], } ), HTTPStatus.OK, diff --git a/forms-flow-api/src/formsflow_api/services/application.py b/forms-flow-api/src/formsflow_api/services/application.py index 6c3da9939..dff23e03b 100644 --- a/forms-flow-api/src/formsflow_api/services/application.py +++ b/forms-flow-api/src/formsflow_api/services/application.py @@ -182,22 +182,29 @@ def _application_access(token: str) -> bool: resource_list, ) + @staticmethod + def extract_common_filters(filters: dict) -> dict: + """Extract common filter parameters from the filters dictionary.""" + return { + "application_id": filters.get("application_id"), + "application_name": filters.get("application_name"), + "application_status": filters.get("application_status"), + "created_by": filters.get("created_by"), + "page_no": filters.get("page_no"), + "limit": filters.get("limit"), + "order_by": filters.get("order_by"), + "sort_order": filters.get("sort_order"), + "created_from": filters.get("created_from"), + "created_to": filters.get("created_to"), + "modified_from": filters.get("modified_from"), + "modified_to": filters.get("modified_to"), + "parent_form_id": filters.get("parent_form_id"), + } + @staticmethod @user_context def get_auth_applications_and_count( # pylint: disable=too-many-arguments,too-many-locals,too-many-positional-arguments - page_no: int, - limit: int, - order_by: str, - created_from: datetime, - created_to: datetime, - modified_from: datetime, - modified_to: datetime, - application_id: int, - application_name: str, - application_status: str, - created_by: str, - sort_order: str, - parent_form_id: str, + filters: dict, **kwargs, ): """Get applications only from authorized groups.""" @@ -213,25 +220,15 @@ def get_auth_applications_and_count( # pylint: disable=too-many-arguments,too-m ) for form in forms: form_ids.append(form.resource_id) + + common_filters = ApplicationService.extract_common_filters(filters) ( applications, get_all_applications_count, ) = Application.find_applications_by_auth_formids_user( - application_id=application_id, - application_name=application_name, - application_status=application_status, - created_by=created_by, - page_no=page_no, - limit=limit, - order_by=order_by, - modified_from=modified_from, - modified_to=modified_to, - sort_order=sort_order, - created_from=created_from, - created_to=created_to, + **common_filters, form_ids=form_ids, user_name=user_name, - parent_form_id=parent_form_id, ) draft_count = Draft.get_draft_count() return ( @@ -270,41 +267,18 @@ def get_auth_by_application_id(application_id: int, **kwargs): @staticmethod @user_context def get_all_applications_by_user( # pylint: disable=too-many-arguments,too-many-locals,too-many-positional-arguments - page_no: int, - limit: int, - order_by: str, - sort_order: str, - created_from: datetime, - created_to: datetime, - modified_from: datetime, - modified_to: datetime, - created_by: str, - application_status: str, - application_name: str, - application_id: int, - parent_form_id: str, - include_drafts: str, + filters: dict, + include_drafts: bool, only_drafts: bool, **kwargs, ): """Get all applications based on user.""" user: UserContext = kwargs["user"] user_id: str = user.user_name + common_filters = ApplicationService.extract_common_filters(filters) applications, get_all_applications_count = Application.find_all_by_user( user_id=user_id, - page_no=page_no, - limit=limit, - order_by=order_by, - sort_order=sort_order, - application_id=application_id, - application_name=application_name, - application_status=application_status, - created_by=created_by, - modified_from=modified_from, - modified_to=modified_to, - created_from=created_from, - created_to=created_to, - parent_form_id=parent_form_id, + **common_filters, include_drafts=include_drafts, only_drafts=only_drafts, )