"""Blueprint for StatementPeriodAdjustmentBatchCriteria API.""" from http import HTTPStatus from abacus_common_logic.views.create_view import CreateView from abacus_common_logic.views.validations import validated_request_body from common_apispec import doc, marshal_with, use_kwargs 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_adjustment_batch_criteria as logic from royalties.schemas import ( StatementPeriodAdjustmentBatchCriteriaDetailSchema, StatementPeriodAdjustmentBatchCriteriaPostSchema, StatementPeriodAdjustmentFileAndBatchCriteriaPostSchema, StatementPeriodAdjustmentFileDetailSchema, ) statement_period_adjustment_batch_criteria_api = Blueprint( 'statement_period_adjustment_batch_criteria_api', __name__ ) class StatementPeriodAdjustmentBatchCriteriaCreateView(CreateView): """Handles statement period adjustment batch criteria creation.""" post_schema = StatementPeriodAdjustmentBatchCriteriaPostSchema() def post(self, **kwargs): """Create statement_period_adjustment_batch_criteria.""" 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().post(**kwargs) def create_handler(self, **params): """Create statement_period_adjustment_batch_criteria.""" return logic.create_statement_period_adjustment_batch_criteria(**params) adjustment_batch_criteria_create_view = ( StatementPeriodAdjustmentBatchCriteriaCreateView.as_view( 'statement_period_adjustment_batch_criteria_create' ) ) adjustment_batch_criteria_create_view = doc( summary='Create StatementPeriodAdjustmentBatchCriteria', )(adjustment_batch_criteria_create_view) adjustment_batch_criteria_create_view = use_kwargs( StatementPeriodAdjustmentBatchCriteriaPostSchema, location='json', required=True, apply=False, )(adjustment_batch_criteria_create_view) adjustment_batch_criteria_create_view = marshal_with( StatementPeriodAdjustmentBatchCriteriaDetailSchema, code=HTTPStatus.CREATED, description='StatementPeriodAdjustmentBatchCriteria successfully created', )(adjustment_batch_criteria_create_view) adjustment_batch_criteria_create_view = marshal_with( None, code=HTTPStatus.BAD_REQUEST, description='Invalid request body or validation error', )(adjustment_batch_criteria_create_view) adjustment_batch_criteria_create_view = marshal_with( None, code=HTTPStatus.UNAUTHORIZED, description=HTTPStatus.UNAUTHORIZED.phrase, )(adjustment_batch_criteria_create_view) adjustment_batch_criteria_create_view = marshal_with( None, code=HTTPStatus.INTERNAL_SERVER_ERROR, description='Internal server error', )(adjustment_batch_criteria_create_view) statement_period_adjustment_batch_criteria_api.add_url_rule( '/statement-period-adjustment-batch-criteria', view_func=adjustment_batch_criteria_create_view, methods=['POST'], ) @statement_period_adjustment_batch_criteria_api.route( '/statement-period-adjustment-file//batch-criteria/', methods=['GET'], ) @doc( summary='Get the statement_period_adjustment_batch_criteria by file id', params={ 'statement_period_adjustment_file_id': { 'description': 'ID of the statement_period_adjustment_file', }, }, ) @marshal_with( StatementPeriodAdjustmentBatchCriteriaDetailSchema, code=HTTPStatus.OK, description='Returns batch record', ) @marshal_with( None, code=HTTPStatus.UNAUTHORIZED, description=HTTPStatus.UNAUTHORIZED.phrase, ) @marshal_with( None, code=HTTPStatus.NOT_FOUND, description='Batch not found', ) @marshal_with( None, code=HTTPStatus.INTERNAL_SERVER_ERROR, description='Internal server error', ) def get_statement_period_adjustment_batch_criteria( statement_period_adjustment_file_id: int, ) -> response.Response: """Get the statement_period_adjustment_batch_criteria by file id. Args: statement_period_adjustment_file_id (int): Id of the statement_period_adjustment_file Returns: statement period adjustment batch criteria record """ access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: return flaskify( response.create_error_response( code='authorization_error', message='Unauthorized', status=401, ) ) return flaskify( logic.get_statement_period_adjustment_batch_criteria( statement_period_adjustment_file_id ) ) @statement_period_adjustment_batch_criteria_api.route( '/statement-period//adjustment-file/batch-criteria', methods=['POST'], ) @doc( summary='Create the adjustment file and the corresponding batch criteria.', ) @use_kwargs( StatementPeriodAdjustmentFileAndBatchCriteriaPostSchema, location='json', required=True, apply=False, ) @marshal_with( StatementPeriodAdjustmentFileDetailSchema, code=HTTPStatus.CREATED, description='StatementPeriodAdjustmentFile and StatementPeriodAdjustmentBatchCriteria successfully created', ) @marshal_with( None, code=HTTPStatus.BAD_REQUEST, description='Invalid request body or validation error', ) @marshal_with( None, code=HTTPStatus.UNAUTHORIZED, description=HTTPStatus.UNAUTHORIZED.phrase, ) @marshal_with( None, code=HTTPStatus.INTERNAL_SERVER_ERROR, description='Internal server error', ) def create_adjustment_file_and_batch_criteria(statement_period_id: int): """Create the adjustment file and the corresponding batch criteria. This function creates the adjustment file record and saves the batch criteria required to generate the adjustment file. Args: statement_period_id (int): id of the 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, ) ) request_body = validated_request_body( StatementPeriodAdjustmentFileAndBatchCriteriaPostSchema() ) return flaskify( logic.create_adjustment_file_and_batch_criteria( statement_period_id, **request_body ) )