From 6e58f75ead48993e55c7c2fbbc79bf338b121a7f Mon Sep 17 00:00:00 2001 From: Hrvoje Fekete Date: Tue, 21 Nov 2023 12:42:26 -0800 Subject: [PATCH] Parametrizing the service. --- auth-api/src/auth_api/models/org.py | 10 +++++++++- auth-api/src/auth_api/resources/v1/org.py | 14 ++++++-------- auth-api/src/auth_api/services/org.py | 12 +++++++++--- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/auth-api/src/auth_api/models/org.py b/auth-api/src/auth_api/models/org.py index 1ac6fa4046..5e670d4ece 100644 --- a/auth-api/src/auth_api/models/org.py +++ b/auth-api/src/auth_api/models/org.py @@ -15,6 +15,7 @@ Basic users will have an internal Org that is not created explicitly, but implicitly upon User account creation. """ +from typing import List from flask import current_app from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer, String, and_, cast, event, func, text from sqlalchemy.orm import contains_eager, relationship @@ -158,12 +159,19 @@ def search_org(cls, search: OrgSearch, environment: str): return pagination.items, pagination.total @classmethod - def search_orgs_by_business_identifier(cls, business_identifier, pagination_info: PaginationInfo, environment): + def search_orgs_by_business_identifier(cls, + business_identifier, + pagination_info: PaginationInfo, + environment, + excluded_org_types: List[str] = None + ): """Find all orgs affiliated with provided business identifier.""" query = db.session.query(Org) query = cls._search_for_statuses(query, []) query = cls._search_by_business_identifier(query, business_identifier, environment) + if excluded_org_types: + query = query.filter(Org.type_code.notin_(excluded_org_types)) pagination = query.order_by(Org.name.desc()) \ .paginate(per_page=pagination_info.limit, page=pagination_info.page) diff --git a/auth-api/src/auth_api/resources/v1/org.py b/auth-api/src/auth_api/resources/v1/org.py index e41670e79b..cc146353aa 100644 --- a/auth-api/src/auth_api/resources/v1/org.py +++ b/auth-api/src/auth_api/resources/v1/org.py @@ -38,7 +38,7 @@ from auth_api.services.authorization import Authorization as AuthorizationService from auth_api.tracer import Tracer from auth_api.utils.endpoints_enums import EndpointEnum -from auth_api.utils.enums import AccessType, NotificationType, PatchActions, Status +from auth_api.utils.enums import AccessType, NotificationType, OrgType, PatchActions, Status from auth_api.utils.role_validator import validate_roles from auth_api.utils.roles import ALL_ALLOWED_ROLES, CLIENT_ADMIN_ROLES, STAFF, USER, Role # noqa: I005 from auth_api.utils.util import get_request_environment @@ -396,16 +396,14 @@ def get_org_details_by_affiliation(business_identifier): limit=int(request.args.get('limit', 10)), page=int(request.args.get('page', 1)) ) - + excluded_org_types = [OrgType.STAFF.value, OrgType.SBC_STAFF.value] try: - data = OrgService.search_orgs_by_affiliation(business_identifier, pagination_info, environment) + data = OrgService.search_orgs_by_affiliation( + business_identifier, pagination_info, environment, excluded_org_types + ) org_details = \ - [ - {'name': org.name, 'uuid': org.uuid, 'branchName': org.branch_name} for org - in data['orgs'] - if org.type_code.upper() not in ['SBC_STAFF', 'STAFF'] - ] + [{'name': org.name, 'uuid': org.uuid, 'branchName': org.branch_name} for org in data['orgs']] response, status = {'orgs_details': org_details}, http_status.HTTP_200_OK except BusinessException as exception: diff --git a/auth-api/src/auth_api/services/org.py b/auth-api/src/auth_api/services/org.py index 660890c290..06d6069404 100644 --- a/auth-api/src/auth_api/services/org.py +++ b/auth-api/src/auth_api/services/org.py @@ -730,10 +730,16 @@ def search_orgs(search: OrgSearch, environment): # pylint: disable=too-many-loc return orgs_result @staticmethod - def search_orgs_by_affiliation(business_identifier, - pagination_info: PaginationInfo, environment): + def search_orgs_by_affiliation( + business_identifier, pagination_info: PaginationInfo, environment, excluded_org_types + ): """Search for orgs based on input parameters.""" - orgs, total = OrgModel.search_orgs_by_business_identifier(business_identifier, pagination_info, environment) + orgs, total = OrgModel.search_orgs_by_business_identifier( + business_identifier, + pagination_info, + environment, + excluded_org_types + ) return { 'orgs': orgs,