"""Vendor Contract logic tier.""" from ows_accounting import response from ows_accounting.constants import error from ows_accounting.models import currency from ows_accounting.models import ows_contracts def get_currency_by_vendor_id(account): """Get currency for a vendor id. Args: account (GrassAccount): GrassAccount named tuple with params type and id. Returns: Response object: Response object with the vendor's currency """ vendor_contracts = ows_contracts.get_booked_contracts( account_type=account.type, account_id=account.id, sort_order='desc') if not vendor_contracts: return response.create_not_found_response( error.ERROR_MESSAGE_CONTRACTS_NOT_FOUND) latest_contract = vendor_contracts.message['items'][0] vendor_currency_response = currency.get_currency_by_id( latest_contract['currency_id']) if not vendor_currency_response: return response.create_not_found_response( error.ERROR_MESSAGE_CURRENCY_NOT_FOUND) vendor_currency = vendor_currency_response.message.copy() vendor_currency.update({ 'vendor_id': latest_contract['vendor_id'], 'currency_id': vendor_currency['id']}) del vendor_currency['id'] return response.Response(vendor_currency)