"""Blueprint for ContractTermSchedule API.""" from flask import Blueprint, request from owsrequest import flask_request from owsresponse import response from owsresponse.adaptors.flask import flaskify from abacus_contract.constants import error from abacus_contract.logic import ( contract_term as contract_term_logic, contract_term_schedule as logic, ) from abacus_contract.utils import authorization from abacus_contract.utils.dataloader import account_scoped_dataloader contract_term_schedule_api = Blueprint('contract_term_schedule_api', __name__) @contract_term_schedule_api.route( '/contract-term//contract-term-schedules', methods=['GET'] ) def get_contract_term_schedules_by_term_id(contract_term_id: int): """GET a list of contract_term_schedule's by contract_term_id. Arg: contract_term_id(int): id of the related contract_term """ access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: account_id = contract_term_logic.get_account_id_by_contract_term_id( contract_term_id ) if not account_id: # Can't verify that user has access to something we can't find return flaskify( response.create_error_response( code=error.ERROR_CODE_FORBIDDEN, message=error.ERROR_MESSAGE_FORBIDDEN_USER, status=403, ) ) authorized = authorization.pdp_authorize_many_accounts([account_id]) if not authorized: return flaskify( response.create_error_response( code=error.ERROR_CODE_FORBIDDEN, message=error.ERROR_MESSAGE_FORBIDDEN_USER, status=403, ) ) return flaskify(logic.get_contract_term_schedules_by_term_id(contract_term_id)) @contract_term_schedule_api.route( '/contract-term-schedules/dataloader', methods=['POST'] ) def get_contract_term_schedules_by_term_ids_dataloader(): """Batch-fetch contract term schedules for the given contract_term_ids.""" return account_scoped_dataloader( entity_name='ContractTerm', resolve_accounts=contract_term_logic.get_account_ids_by_contract_term_ids, fetch_records=logic.get_contract_term_schedule_records_by_term_ids, key_field='contract_term_id', as_list=True, )