"""Interface to the ows-pricing microservice.""" from oto import response from owsrequest import request from product_digital.constants import services from product_digital.models import ows_track def get_track_overrides(product_id): """Get all pricing overrides for a product at the track level. Args: product_id (int): the product to which the track overrides belong Returns: response.Response: response object containing track overrides """ ows_pricing_response = request.get( services.OWS_PRICING, '/product/{product_id}/track-overrides'.format(product_id=product_id), ) response_body = ows_pricing_response.json() if ows_pricing_response.status_code != 200: return response.create_error_response( status=ows_pricing_response.status_code, code=response_body.get('code'), message=response_body.get('message')) track_overrides = _remove_territory_overrides(response_body.get('items')) active_overrides = _filter_active_overrides(track_overrides) updated_overrides_response = _add_track_name_to_overrides( active_overrides) if not updated_overrides_response: return updated_overrides_response updated_overrides = updated_overrides_response.message return response.Response(message={'items': updated_overrides}) def get_active_product_overrides(product_id): """Get all pricing overrides for a product at the product level. Args: product_id (int): the product to which the overrides belong Returns: response.Response: response object containing product overrides """ ows_pricing_response = request.get( services.OWS_PRICING, '/product/{product_id}/override'.format(product_id=product_id), ) response_body = ows_pricing_response.json() if ows_pricing_response.status_code != 200: return response.create_error_response( status=ows_pricing_response.status_code, code=response_body.get('code'), message=response_body.get('message')) product_overrides = _remove_territory_overrides(response_body.get('items')) active_overrides = _filter_active_overrides(product_overrides) return response.Response(message={'items': active_overrides}) def get_pricing_tier_by_product(product_id, pricing_family_id): """Get the pricing tier for a product or track, if empty return default. Args: product_id (int): the product or track relating to the pricing tier pricing_family_id (int): 2 for album, 3 for track Returns: response.Response: response object containing pricing tier """ ows_pricing_response = request.get( services.OWS_PRICING, '/product/{}/pricing-family/{}/orchard_pricing_tier'.format( product_id, pricing_family_id), ) pricing_tiers_response = get_pricing_tiers( pricing_family_id) if not pricing_tiers_response: return pricing_tiers_response if not ows_pricing_response.text: return response.Response(_extract_default_tier( pricing_tiers_response.message.get('items'))) response_body = ows_pricing_response.json() if ows_pricing_response.status_code != 200: return response.create_error_response( status=ows_pricing_response.status_code, code=response_body.get('code'), message=response_body.get('message')) pricing_tiers = pricing_tiers_response.message.get('items') tier_detail = ([item for item in pricing_tiers if item['orchard_pricing_tier_id'] == response_body['orchard_pricing_tier_id'] ][0]) response_body.update({'name': tier_detail['name']}) return response.Response(message=response_body) def get_pricing_tiers(pricing_family_id): """Get a list of pricing tiers for a family (i.e track, album). Args: pricing_family_id (int): 2 for album, 3 for track Returns: response.Response: response object pricing tiers """ ows_pricing_response = request.get( services.OWS_PRICING, '/pricing-family/{}/orchard-pricing-tier'.format(pricing_family_id), ) response_body = ows_pricing_response.json() if ows_pricing_response.status_code != 200: return response.create_error_response( status=ows_pricing_response.status_code, code=response_body.get('code'), message=response_body.get('message')) return response.Response(message=response_body) def get_product_pricing_validations(product_id): """Validate the pricing for a product. Args: product_id (int): the product to validate Returns: response.Response: response object containing validation result """ ows_pricing_response = request.get( services.OWS_PRICING, '/product/{}/validate'.format(product_id), ) response_body = ows_pricing_response.json() if ows_pricing_response.status_code != 200: return response.create_error_response( status=ows_pricing_response.status_code, code=response_body.get('code'), message=response_body.get('message')) return response.Response(message=response_body) def _extract_default_tier(pricing_tiers): """Return the default tier for a set of pricing tiers. Args: pricing_tiers (list): a list of dictionaries of pricing tier data Returns: dict: a single pricing tier that is the default """ return [item for item in pricing_tiers if item['is_default'] is True][0] def _remove_territory_overrides(pricing_overrides): """Return a filtered list of pricing overrides.""" return ([item for item in pricing_overrides if len(item['territories']) == 0 and len(item['stores']) == 0]) def _filter_active_overrides(pricing_overrides): """Return a list of active overrides. Args: pricing_overrides (list): a list of dictionaries of pricing overrides Returns: list: a list of overrides that are active """ return [item for item in pricing_overrides if item['activated'] is True] def _add_track_name_to_overrides(track_overrides): """Fetch the track name for each override and add to override. Args: track_overrides (list): a list of track pricing overrides Returns: response.Response: response object with updated track overrides """ for override in track_overrides: track_response = ows_track.get_track_by_track_id( override['track_id']) if not track_response: return track_response override['track_name'] = track_response.message.get('track_name') return response.Response(message=track_overrides)