"""Set main release date Utils.""" from typing import Dict, List from bulk_metadata_ingester_common.models.bulk_release import BulkRelease from bulk_metadata_ingester_common.utils.catalog_ingestion import \ save_catalog_ingestion_action from bulk_metadata_ingester_common.utils.error import graphql_execute from config import graphql_gateway from constants import queries from constants.exceptions import ( SetCarveoutsException, SetPricingException) from constants.pricing import ( MUSIC_ALBUM, PRICE_MAPPING) from constants.territories import ( EXCLUDE, INCLUDE, SME_ORCH_COUNTRY_MAPPING, TERRITORY_SEPARATOR ) from ddex_ingester_common.constants.catalog_ingestion import ( UPDATE_ACTION ) from ddex_ingester_common.constants.country_codes import ( ALL_COUNTRY_CODES ) def update_product_release_date( event: Dict, model: BulkRelease, logger: object) -> Dict: """Update product release date and sale start date.""" # Audio product payload = { 'data': { 'productId': model.product_id, 'releaseDate': model.original_release_date, 'saleStartDate': model.sale_start_date, } } result = graphql_execute( graphql_gateway, queries.UPDATE_PRODUCT_RELEASE_DATE, payload, logger ) # Logging save_catalog_ingestion_action( event, model, {}, UPDATE_ACTION ) # Return result return result def update_carveouts( event: Dict, model: BulkRelease, logger: object) -> Dict: """Update product territory carveouts.""" logger.info(f'Got territories: {model.territory_iso_codes}') if model.territory_iso_codes: territories = model.territory_iso_codes.strip() else: territories = '' territories = format_carveout_data( territories, model.only_include) carveout_data = { 'productId': model.product_id, 'upc': str(model.upc), 'countryCodes': territories, } country_codes = map_country_codes(carveout_data['countryCodes'], logger) carveout_data['countryCodes'] = country_codes # call ows-carveouts result = graphql_execute( graphql_gateway, queries.SET_CARVEOUTS, {'data': carveout_data}, logger ) # Logging save_catalog_ingestion_action( event, model, {}, UPDATE_ACTION ) # Return result return result def update_pricing( event: Dict, model: BulkRelease, logger: object) -> Dict: """Update product pricing.""" logger.info(f'Got pricing: {model.album_pricing}') # TODO: This should be pre-flight # Check pricing tier is valid if model.album_pricing not in PRICE_MAPPING[MUSIC_ALBUM]: msg = f'{model.album_pricing} is not a valid product price tier.' raise SetPricingException(msg) # Map to value query expects graph_price = PRICE_MAPPING[MUSIC_ALBUM][model.album_pricing] # Shape payload payload = { 'input': { 'productId': model.product_id, 'orchardPricingTier': graph_price, 'pricingFamily': MUSIC_ALBUM, } } # Execute graph query result = graphql_execute( graphql_gateway, queries.SET_PRODUCT_PRICING_TIER, payload, logger ) # Logging save_catalog_ingestion_action( event, model, {}, UPDATE_ACTION ) # Return result return result def format_carveout_data( territories: List, only_include: str) -> Dict: """Extract territories from product deal terms.""" # TODO: This should be pre-flight if not territories: # Return WW for blank if not only_include: return [] else: msg = f'Incorrect carveout data: "{only_include}" territory list cannot be blank.' # noqa raise SetCarveoutsException(msg) # Split territories territories = territories.split(TERRITORY_SEPARATOR) # Process 'Only Include' / 'Only Exclude' # Only Include - Carveout the subtraction set from WW territory list if territories and only_include.lower() == INCLUDE.lower(): territories = list(set(ALL_COUNTRY_CODES) - set(territories)) # Only Exclude - Carveout the passed set as exclusions elif territories and only_include.lower() == EXCLUDE.lower(): territories = territories else: # Bad formatting or bad data msg = f'Incorrect territory carveouts: {only_include} - {territories}' raise SetCarveoutsException(msg) return territories def map_country_codes(country_codes: List, logger: object) -> List: """Map SME country codes to Orchard.""" mapped_country_codes = set() logger.info(f'Attempting to map country codes: {country_codes}') # Remove blanks due to trailing separator country_codes = [c for c in country_codes if c] # Leaving the below logic. More remaps will happen one day. for country_code in country_codes: if SME_ORCH_COUNTRY_MAPPING.get(country_code): logger.info( f'Mapping {country_code} to' f' {SME_ORCH_COUNTRY_MAPPING.get(country_code)}' ) mapped_country_codes.add( SME_ORCH_COUNTRY_MAPPING.get(country_code)) else: logger.info(f'Adding {country_code} without mapping') mapped_country_codes.add(country_code) return sorted(list(mapped_country_codes))