"""Report payment API.""" from http import HTTPStatus from common_apispec import doc, marshal_with, use_kwargs from flask import Blueprint from payment.logic import report_payment as logic from payment.schemas.report_payment import ( ReportPaymentDetailSchema, ReportPaymentDownloadSchema, ReportPaymentPutSchema, ) from payment.utils.authorization import check_access report_payment_api = Blueprint('report_payment_api', __name__) @report_payment_api.route('/reports///', methods=['PUT']) @doc( tags=['reports'], description='Create payment report entity.', ) @check_access @use_kwargs(ReportPaymentPutSchema, location='json', required=True) @marshal_with( ReportPaymentDetailSchema, code=HTTPStatus.OK, description=HTTPStatus.OK.phrase, ) def create_or_update_report_payment( target_type: str, target_id: int, report_type: str, report_export_url: str ) -> tuple: target_type = target_type.replace('-', '_') report_payment = logic.create_or_update_report_payment( target_type=target_type, target_id=target_id, report_type=report_type, report_export_url=report_export_url, ) return report_payment, HTTPStatus.OK @report_payment_api.route('/reports///', methods=['GET']) @doc( tags=['reports'], description='Get reports by target.', ) @check_access @marshal_with( ReportPaymentDetailSchema(many=True), code=HTTPStatus.OK, description=HTTPStatus.OK.phrase, ) def get_reports_by_target(target_type: str, target_id: int) -> tuple: target_type = target_type.replace('-', '_') reports = logic.get_reports_by_target( target_type=target_type, target_id=target_id, ) return reports, HTTPStatus.OK @report_payment_api.route('/report-payment//', methods=['GET']) @doc( tags=['reports'], description='Get report by id.', ) @check_access @marshal_with( ReportPaymentDetailSchema, code=HTTPStatus.OK, description=HTTPStatus.OK.phrase, ) @marshal_with( None, code=HTTPStatus.BAD_REQUEST, description=HTTPStatus.BAD_REQUEST.phrase, ) def get_report_payment(report_payment_id: int) -> tuple: report_payment = logic.get_report_payment( report_payment_id=report_payment_id, ) return report_payment, HTTPStatus.OK @report_payment_api.route( '/report-payment//download/', methods=['GET'] ) @doc( tags=['reports'], description='Download report by id.', ) @check_access @marshal_with( ReportPaymentDownloadSchema, code=HTTPStatus.OK, description=HTTPStatus.OK.phrase, ) @marshal_with( None, code=HTTPStatus.BAD_REQUEST, description=HTTPStatus.BAD_REQUEST.phrase, ) def get_download(report_payment_id: int) -> tuple: download = logic.get_download( report_payment_id=report_payment_id, ) return download, HTTPStatus.OK