"""Reference Transaction Type Group handlers.""" from http import HTTPStatus from common_apispec import doc, marshal_with from flask import Blueprint, request from owsrequest import flask_request from owsresponse import response from owsresponse.adaptors.flask import flaskify from abacus_contract.constants import error from abacus_contract.logic import reference_transaction_type_group as logic from abacus_contract.schemas.reference_transaction_type_group import ( ReferenceTransactionTypeGroupDataloaderSchema, ) from abacus_contract.utils import authorization from abacus_contract.utils.format_error import validation_error from abacus_contract.utils.request import get_optional_numeric_list_from_params from core.cache import cache_reference_data from core.config import Config reference_transaction_type_group_api = Blueprint( 'reference_transaction_type_group_api', __name__, ) @reference_transaction_type_group_api.route( '/reference-transaction-type-group/group-admin/', methods=['GET'], ) @cache_reference_data def get_transaction_type_groups_by_group_admin(group_admin: str): """Get list of reference_transaction_type_groups by group_admin. Args: group_admin (str): one of REFERENCE_TRANSACTION_TYPE_GROUP_ADMIN """ access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: authorized = authorization.pdp_authorize_resource( # Id of 0 means "all reference transaction types" # This resource type is all or nothing, permissions-wise. resource_id=0, resource_type='reference_transaction_type', ) if not authorized: return flaskify( response.create_error_response( code=error.ERROR_CODE_FORBIDDEN, message=error.ERROR_MESSAGE_FORBIDDEN_USER, status=403, ) ) return flaskify(logic.get_groups_by_group_admin(group_admin)) @reference_transaction_type_group_api.route( '/reference-transaction-type-group//' 'reference-transaction-types', methods=['GET'], ) @cache_reference_data def get_transaction_types_by_group(reference_transaction_type_group_id: int): """Get reference_transaction_types by parent reference_transaction_type_group. Args: reference_transaction_type_group_id (int): ID of the parent group """ access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: authorized = authorization.pdp_authorize_resource( # Id of 0 means "all reference transaction types" # This resource type is all or nothing, permissions-wise. resource_id=0, resource_type='reference_transaction_type', ) if not authorized: return flaskify( response.create_error_response( code=error.ERROR_CODE_FORBIDDEN, message=error.ERROR_MESSAGE_FORBIDDEN_USER, status=403, ) ) return flaskify( logic.get_transaction_types_by_group(reference_transaction_type_group_id) ) @reference_transaction_type_group_api.route( '/reference-transaction-type-group/' 'reference_transaction_type_group_transaction_types', methods=['GET'], ) @cache_reference_data def get_transaction_type_group_transaction_types(): """Get a list of reference_transaction_types and their transaction_type_group.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: authorized = authorization.pdp_authorize_resource( # Id of 0 means "all reference transaction types" # This resource type is all or nothing, permissions-wise. resource_id=0, resource_type='reference_transaction_type', ) if not authorized: return flaskify( response.create_error_response( code=error.ERROR_CODE_FORBIDDEN, message=error.ERROR_MESSAGE_FORBIDDEN_USER, status=403, ) ) return flaskify(logic.get_transaction_type_group_transaction_types()) @reference_transaction_type_group_api.route( '/reference-transaction-type-group/reference-transaction-types/dataloader', methods=['POST'], ) @doc( summary='Fetch transaction types for many groups by group IDs.', description="Retrieves each group's reference transaction types for the group ids passed in the request body.", ) @marshal_with( ReferenceTransactionTypeGroupDataloaderSchema(many=True), code=HTTPStatus.OK, description='One ordered entry per requested group id (its transaction types or null).', ) @marshal_with( None, code=HTTPStatus.BAD_REQUEST, description='Invalid request body payload or validation error.', ) @marshal_with( None, code=HTTPStatus.FORBIDDEN, description=HTTPStatus.FORBIDDEN.phrase, ) def get_transaction_types_by_group_dataloader(): """GET reference transaction types for the given group ids.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: authorized = authorization.pdp_authorize_resource( # Id of 0 means "all reference transaction types" # This resource type is all or nothing, permissions-wise. resource_id=0, resource_type='reference_transaction_type', ) if not authorized: return flaskify( response.create_error_response( code=error.ERROR_CODE_FORBIDDEN, message=error.ERROR_MESSAGE_FORBIDDEN_USER, status=403, ) ) try: group_ids = get_optional_numeric_list_from_params() except (ValueError, TypeError): return flaskify( validation_error( error.ERROR_INVALID_IDS.format(object='ReferenceTransactionTypeGroup') ) ) if len(group_ids) > Config.OWS_BATCH_LIMIT: return flaskify( validation_error( error.ERROR_TOO_MANY_DATALOADER_IDS.format(limit=Config.OWS_BATCH_LIMIT) ) ) return flaskify(logic.get_transaction_types_by_group_ids(group_ids)) @reference_transaction_type_group_api.route( '/reference-transaction-type-groups', methods=['GET'] ) @cache_reference_data def get_transaction_type_groups(): """Get a list of all reference transaction type groups.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: authorized = authorization.pdp_authorize_resource( # Id of 0 means "all reference transaction types" # This resource type is all or nothing, permissions-wise. resource_id=0, resource_type='reference_transaction_type', ) if not authorized: return flaskify( response.create_error_response( code=error.ERROR_CODE_FORBIDDEN, message=error.ERROR_MESSAGE_FORBIDDEN_USER, status=403, ) ) return flaskify(logic.get_transaction_type_groups())