"""Logic for Product.""" import json from owsresponse import response from product_store_mapping.models import productstoremapping def get_product_with_tracks(product_id, dms_id): """Retrieving a product with tracks from the store mapping. Args: product_id (int): The product_id for the product store mapping dms_id (int): The dms_id for the product store mapping Returns: Response: ProductStoreMapping """ result = productstoremapping.get_product_with_tracks(product_id, dms_id) if result.status == 404: return result product_tracks = {"product": None, "tracks": []} for product in result.message: mapping_data = json.loads(product.mapping_data) provider = None if "provider" in mapping_data: provider = mapping_data["provider"] if product.track_unique_id: product_tracks["tracks"].append( { product.track_unique_id: { "store_unique_id": product.store_unique_id, "custom_id": mapping_data.get("custom_id"), } } ) else: product_tracks["product"] = { "store_unique_id": product.store_unique_id, "custom_id": mapping_data.get("custom_id"), "provider": provider, } product_tracks["product"].update(mapping_data) return response.Response(product_tracks) def get_product(product_id, store_id): """Logic handlers. Args: product_id (int): The product_id for the product store mapping store_id (int): The store_id for the product store mapping Returns: Response: dictionary of results """ result = productstoremapping.get_product(product_id, store_id) if result.status != 200: return result mapping_data = json.loads(result.message.mapping_data) mapping_data.setdefault("provider", None) response_dict = {"store_unique_id": result.message.store_unique_id} response_dict.update(mapping_data) return response.Response(response_dict) def delete_product(product_id, store_id): """DELETE product with product_id, store_id. Args: product_id (int): The product_id of the product we want to delete. store_id (int): The store_id of the product we want to delete. Returns: Response: message containing data upon successful query. error Response message otherwise. """ return productstoremapping.delete_product(product_id, store_id) def save_product_data( product_id, store_id, provider, video_order_id, store_unique_id=0 ): """Logic handlers save product data. Args: product_id (int): The product_id for the product store mapping. store_id (int): The store_id for the product store mapping. provider (string): The provider for the product store mapping. video_order_id (string): The video_order_id for the product store mapping. store_unique_id (int): The store_unique_id for the product store mapping. Returns: Response: message containing data upon successful query. error Response message otherwise. """ mandatory_field_list = [product_id, store_id, provider, video_order_id] if not all(mandatory_field_list): return response.create_error_response( "InvalidInput", "product_id, store_id, provider & video_order_id all are required.", ) data = { "provider": provider, "video_order_id": video_order_id, "store_unique_id": store_unique_id, } return productstoremapping.save_product(product_id, store_id, data)