"""Interface for the ows-account microservice.""" from oto import response from oto import status from owsrequest import request from owsrequest.constants import headers from salessheets.constants import ows_services def get_subaccount_by_id(subaccount_id): """Fetch subaccount details by subaccount_id. Args: subaccount_id (int): Id of the subaccount to fetch. Returns: Response: Response containing the subaccount details. """ subaccount_response = request.get( ows_services.OWS_ACCOUNT_SERVICE_NAME, path='/subaccount/{}'.format(subaccount_id)) if subaccount_response.status_code != 200: return response.create_error_response( code=ows_services.CODE_FORBIDDEN_PRODUCT, message=ows_services.MESSAGE_COULD_NOT_GET_SUBACCOUNT.format( subaccount_id), status=subaccount_response.status_code) return response.Response(message=subaccount_response.json()) def check_vendor_rights(vendor_id, account_type, account_id): """Check grass headers to match vendor id. Args: vendor_id (int): Vendor Id. account_type (str or None): grass account type account_id (str or None): grass account id Returns: Response: Response containing the subaccount details. """ if not account_id or not account_type: return response.Response() vendor_id = int(vendor_id) account_id = int(account_id) if account_type == headers.GRASS_ACCOUNT_TYPE_VENDOR: if vendor_id != account_id: return response.create_error_response( code=ows_services.CODE_FORBIDDEN_VENDOR, message=ows_services.MESSAGE_DIFFERENT_VENDOR, status=status.FORBIDDEN) return response.Response() subaccount = get_subaccount_by_id(account_id) if not subaccount: return subaccount account_vendor_id = int(subaccount.message['vendor_id']) if account_vendor_id != vendor_id: return response.create_error_response( code=ows_services.CODE_FORBIDDEN_VENDOR, message=ows_services.MESSAGE_DIFFERENT_VENDOR, status=status.FORBIDDEN) return response.Response()