"""Blueprint for payment hold API.""" from abacus_common_logic.views.create_view import CreateView from flask import Blueprint from flask import request from owsrequest import flask_request from owsresponse import response from owsresponse.adaptors.flask import flaskify from abacus_account.constants.error import ERROR_CODE_AUTHORIZATION from abacus_account.logic import payment_hold as logic from abacus_account.schemas import PaymentHoldSchema from abacus_account.schemas.payment_hold import PaymentHoldListInputSchema from abacus_account.utils.validations import validate_payload payment_hold_api = Blueprint('payment_hold_api', __name__) class PaymentHoldCreate(CreateView): """POST payment hold for specified account.""" post_schema = PaymentHoldSchema(exclude=('payment_hold_id', 'account_id')) def post(self, **kwargs): """POST payment hold for specified account.""" 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 or update existing payment hold.""" return logic.create_or_update_payment_hold(**params) @payment_hold_api.route('/account//payment-hold', methods=['GET']) def payment_hold_status(object_id): """Endpoint to GET payment hold status info for a specified account.""" 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_payment_hold(object_id)) @payment_hold_api.route('/payment-holds', methods=['POST']) def list_payment_holds(): """List payment holds with optional filters.""" 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, ) ) payload = {**request.args, 'account_ids': request.get_json(silent=True)} params = validate_payload(payload, PaymentHoldListInputSchema) return flaskify(logic.get_payment_holds(**params)) payment_hold_api.add_url_rule( '/account//payment-hold', methods=['POST'], view_func=PaymentHoldCreate.as_view('create_payment_hold') )