"""Shared validation logic.""" from abacus_common_data.country import Country from abacus_common_data.currency import Currency from flask import abort from marshmallow import ValidationError from owsrequest import flask_request from abacus_account import models from abacus_account.config import ows_client from abacus_account.constants import error from abacus_account.constants.constants import EMPLOYEE_ALL_VENDORS_MARK from abacus_account.constants.constants import LABEL_RESOURCE_NAME from abacus_account.constants.constants import OWS_PERMISSIONS_ACCOUNT_ENDPOINT_URL from abacus_account.constants.constants import OWS_PERMISSIONS_SERVICE_NAME from abacus_account.constants.constants import VENDOR_RESOURCE_NAME from abacus_account.logic import reference_payoneer_program as logic def validate_country_code(country_code): """Validate a country code parameter.""" try: Country(country_code) except KeyError: raise ValidationError( error.ERROR_UNKNOWN_COUNTRY.format(code=country_code) ) def validate_currency_code(currency_code): """Validate a currency code parameter.""" try: Currency(currency_code) except KeyError: raise ValidationError( error.ERROR_UNKNOWN_CURRENCY.format(code=currency_code) ) def validate_payment_type(account_payment_term_obj, params): """Validate a payment type to avoid KNR account update agreement type.""" knr_payment_entities = [ payment_entity.reference_payment_entity_id for payment_entity in models.ReferencePaymentEntity.get_knr_payment_entities() ] if knr_payment_entities and 'payment_entity_id' in params and \ params['payment_entity_id'] in knr_payment_entities and \ account_payment_term_obj.agreement_type_id != \ params.get('agreement_type_id'): raise ValidationError( error.ERROR_KNR_ACCOUNT_CANT_MODIFY_AGREEMENT_TYPE.format( payment_term_id=account_payment_term_obj.account_payment_term_id) ) def validate_record_owner(request, account_id): """ Validate if request was made by the owner of the account record. Args: request (flask.request): request account_id (int): Abacus account id Returns: bool: False if not the owner """ profile_type, profile_id = flask_request.get_profile_headers(request) if profile_type != 'DocumentsProfile': return True if not profile_id: return False ows_permission_response = ows_client.get( OWS_PERMISSIONS_SERVICE_NAME, OWS_PERMISSIONS_ACCOUNT_ENDPOINT_URL.format( profile_type=profile_type, profile_id=profile_id, resource_type=LABEL_RESOURCE_NAME ) ) if ows_permission_response.status_code == 200: response_data = ows_permission_response.json() if response_data['items']: for item in response_data['items']: # We need to check only type = `Vendor` if item.get('type') == VENDOR_RESOURCE_NAME and \ item.get('vendorId') \ in [account_id, EMPLOYEE_ALL_VENDORS_MARK]: return True return False def validate_payoneer_program(payee_object, **params): """Validate a payoneer program id by signing entity, payment type and currency.""" if params.get('payoneer_program_id') is not None: payoneer_program_id = params['payoneer_program_id'] account_payment_term = models.AccountPaymentTerm.query.filter_by( account_id=payee_object.account_id ).first() if not account_payment_term: raise ValidationError( error.ERROR_PAYMENT_TERM_NOT_EXISTS.format( account_id=payee_object.account_id) ) payoneer_program = logic.get_payoneer_program( currency_code=account_payment_term.currency_code, agreement_type_id=account_payment_term.agreement_type_id, payment_entity_id=account_payment_term.payment_entity_id, reference_payment_type_id=params.get('reference_payment_type_id') ) if payoneer_program.status != 200: raise ValidationError( error.ERROR_PAYONEER_PROGRAM_BY_PARAMS_NOT_EXISTS.format( currency=account_payment_term.currency_code, agreement_type=account_payment_term.agreement_type_id, payment_entity=account_payment_term.payment_entity_id ) ) if payoneer_program.message['payoneer_program_id'] != payoneer_program_id: raise ValidationError( error.ERROR_INPUT_PAYONEER_PROGRAM_MISMATCH.format( request_pp_id=payoneer_program_id, payment_term_pp_id=payoneer_program.message['payoneer_program_id'], ) ) def validate_payload(payload, schema): """Validate request payload.""" try: return schema().load(payload) except ValidationError as err: abort( description=err.messages, code=400, )