"""Blueprint for statement_period_payment_entity API.""" from abacus_common_logic.views.item_view import ItemView from flask import Blueprint, request from owsrequest import flask_request from owsresponse import response from owsresponse.adaptors.flask import flaskify from royalties.constants.error import ERROR_CODE_AUTHORIZATION from royalties.logic import statement_period_payment_entity as logic from royalties.models import StatementPeriodPaymentEntity from royalties.schemas.statement_period_payment_entity import ( StatementPeriodPaymentEntitySchema, ) statement_period_payment_entity_api = Blueprint( 'statement_period_payment_entity_api', __name__ ) @statement_period_payment_entity_api.route( '/statement-period//payment-entities', methods=['GET'] ) def get_statement_period_payment_entity_by_statement_period(object_id): """Endpoint to GET statement_period_payment_entity by statement_period_id.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: return flaskify( response.create_error_response( code=ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=401, ) ) return flaskify(logic.get_by_statement_period_id(object_id)) @statement_period_payment_entity_api.route( '/statement-period//payment-entity' '//visible', methods=['PUT'], ) def set_statement_period_payment_entity_is_visible( statement_period_id, payment_entity_id ): """Endpoint to update is_visible_to_customer field to True.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: return flaskify( response.create_error_response( code=ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=401, ) ) return flaskify( logic.set_is_visible_to_customer(statement_period_id, payment_entity_id) ) @statement_period_payment_entity_api.route( '/statement-period//payment-entities/states', methods=['GET'], ) def get_states_by_statement_period(statement_period_id: int): """Get statement_period_payment_entity states by statement_period. Args: statement_period_id (int): ID of parent statement_period """ access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: return flaskify( response.create_error_response( code=ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=401, ) ) return flaskify(logic.get_states_by_statement_period(statement_period_id)) class StatementPeriodPaymentEntityItemView(ItemView): """View to get statement_period_payment_entity by ID.""" model_class = StatementPeriodPaymentEntity object_detail_schema = StatementPeriodPaymentEntitySchema() def get(self, object_id, **kwargs): """Get statement_period_payment_entity by ID.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: return flaskify( response.create_error_response( code=ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=401, ) ) return super().get(object_id, **kwargs) statement_period_payment_entity_api.add_url_rule( '/statement-period-payment-entity/', methods=['GET'], view_func=StatementPeriodPaymentEntityItemView.as_view( 'statement_period_payment_entity' ), )