"""Blueprint for ProjectTransferTerm API.""" from abacus_common_logic.views.validations import validated_request_body from flask import Blueprint, request from owsrequest import flask_request from owsresponse import response from owsresponse.adaptors.flask import flaskify from abacus_contract.schemas.contract_term_transfer import ( CreateTransferTermRequestSchema, ) from royalties.constants.error import ERROR_CODE_AUTHORIZATION from royalties.logic import project_transfer_term as logic from royalties.schemas.project_transfer_term import ProjectTransferTermPatchSchema project_transfer_term_api = Blueprint('project_transfer_term_api', __name__) @project_transfer_term_api.route('/transfer-job//terms', methods=['GET']) def get_transfer_job_terms(job_id): """Return all transfer terms (with conditions) for a project transfer job.""" 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_terms_for_job(job_id)) @project_transfer_term_api.route('/transfer-job//terms', methods=['POST']) def create_transfer_job_terms(job_id): """Create transfer terms (and their conditions) for a project transfer job. Request body: a JSON array of term objects, each shaped per `ProjectTransferTermInputSchema`. Returns the created rows in the same shape as the GET response. """ 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, ) ) body = request.get_json(force=True, silent=True) if not isinstance(body, list): return flaskify( response.create_error_response( code='validation_error', message='Request body must be a JSON array of terms.', status=400, ) ) return flaskify(logic.create_terms_for_job(job_id, body)) @project_transfer_term_api.route( '/transfer-job//terms/', methods=['PATCH'] ) def patch_transfer_job_term(job_id, term_id): """Set destination_contract_term_id on a staged transfer term.""" 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, ) ) body = validated_request_body(ProjectTransferTermPatchSchema()) return flaskify( logic.set_destination_term_id( term_id=term_id, destination_contract_term_id=body['destination_contract_term_id'], ) ) @project_transfer_term_api.route( '/contract//active-term/label', methods=['GET'] ) def get_active_label_term(contract_id): """Return the active label contract_term for a contract. Used by the AccountingTransfer lambda to resolve the current label term at execution time. Auth: rules-based only (M2M transfer operator). """ 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_active_label_term_for_contract(contract_id)) @project_transfer_term_api.route( '/account//contract-terms/transfer-create', methods=['POST'], ) def create_transfer_term(account_id): """Create a single new contract_term for a staged transfer term. Unlike bulk-add, this always creates a new term rather than merging into an existing one. Idempotent on project_transfer_term_id: a retry returns the already-created term (status 200) instead of duplicating it. Used by the AccountingTransfer lambda so each staged product/track term lands as its own contract_term exactly once. """ 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, ) ) body = validated_request_body(CreateTransferTermRequestSchema()) return flaskify( logic.create_destination_term( project_transfer_term_id=body['project_transfer_term_id'], contract_id=body['contract_id'], term_type=body['term_type'], attachments=body['attachments'], conditions=body.get('conditions', []), attachment_relations=body.get('attachment_relations'), name=body.get('name'), ) )