"""Model ProductStoreMapping.""" from flask import json from owsresponse import response from product_store_mapping.connectors import mysql QUERY_DELETE_PRODUCT = """DELETE FROM product_store_mapping WHERE product_id = %s AND dms_id = %s""" QUERY_PRODUCT_TRACKS = """SELECT product_id, dms_id, store_unique_id, track_unique_id, mapping_data FROM product_store_mapping WHERE product_id = %s AND dms_id = %s""" QUERY_PRODUCT = """SELECT product_id, dms_id, store_unique_id, track_unique_id, mapping_data FROM product_store_mapping WHERE product_id = %s AND dms_id = %s AND track_unique_id = 0""" QUERY_TRACK = """SELECT product_id, dms_id, store_unique_id, track_unique_id, mapping_data FROM product_store_mapping WHERE track_unique_id = %s AND dms_id = %s""" QUERY_INSERT_PRODUCT = """INSERT INTO product_store_mapping (product_id, dms_id, store_unique_id, track_unique_id, mapping_data) VALUES (%s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE store_unique_id = values(store_unique_id), mapping_data = values(mapping_data)""" class ProductStoreMapping: """Model for ows_product_store_mapping.""" def __init__( self, product_id, dms_id, store_unique_id, track_unique_id, mapping_data ): """Initialize product store mapping data. Args: product_id (int): art_relations.releases.release_id dms_id (int): art_relations.customer_master_master.customer_master_master_id store_unique_id (int): Unique ID from retailer, e.g. Apple ID track_unique_id (int): art_relations.track.id mapping_data (str): Contains Orchard custom ID for given product or track and optional provider info (iTunes). """ self.product_id = product_id self.dms_id = dms_id self.store_unique_id = store_unique_id self.track_unique_id = track_unique_id self.mapping_data = mapping_data def __hash__(self): """Hashcode calculation. Returns: int: Hashcode value representing contents. """ return hash( ( self.product_id, self.dms_id, self.store_unique_id, self.track_unique_id, self.mapping_data, ) ) def __eq__(self, other): """Equality implementation. Returns: bool: Whether self is equivalent to other. """ return self.__dict__ == other.__dict__ def delete_product(product_id, store_id): """DELETE product with store_id, product_id. Args: product_id (int): The product_id of the product we want. store_id (int): The store_id is the store id to filter by. Returns: Response: message containing data upon successful query. error Response message otherwise. """ result = mysql.execute_query(QUERY_DELETE_PRODUCT, (product_id, store_id)) if isinstance(result, response.Response): return result success_msg = "mapping deleted succssfully" return response.Response(message={"success": success_msg}) def get_product(product_id, store_id): """Query product with product_id and store_id. Args: product_id (int): The product_id of the product we want store_id (int): The store_id is the store id to filter by Returns: Response: message containing data upon successful query. error Response message otherwise """ result = mysql.execute_query(QUERY_PRODUCT, (product_id, store_id)) if not result or isinstance(result, response.Response): return response.create_not_found_response() if len(result) > 1: return response.create_fatal_response( "more than 1 records found for {} and {}".format(product_id, store_id) ) row = result[0] product = ProductStoreMapping(row[0], row[1], row[2], row[3], row[4]) return response.Response(product) def get_product_with_tracks(product_id, dms_id): """Function to retrieve all data for the given product and store. Args: product_id (int): art_relations.releases.release_id dms_id (int): art_relations.customer_master_master.customer_master_master_id """ result = mysql.execute_query(QUERY_PRODUCT_TRACKS, (product_id, dms_id)) if result and not isinstance(result, response.Response): products = [] existing_tracks = set() for row in result: if row[3] in existing_tracks: return response.create_fatal_response( "More than 1 records for product {} found for store {}".format( product_id, dms_id ) ) existing_tracks.add(row[3]) products.append(ProductStoreMapping(row[0], row[1], row[2], row[3], row[4])) return response.Response(products) else: return response.create_not_found_response() def get_track(track_unique_id, dms_id): """Function to retrieve all data for the given track and store. Args: track_unique_id (int): art_relations.track.id dms_id (int): art_relations.customer_master_master.customer_master_master_id """ result = mysql.execute_query(QUERY_TRACK, (track_unique_id, dms_id)) if not result or isinstance(result, response.Response): return response.create_not_found_response() if len(result) > 1: return response.create_fatal_response( "more than 1 records found for {} and {}".format(track_unique_id, dms_id) ) row = result[0] track = ProductStoreMapping(row[0], row[1], row[2], row[3], row[4]) return response.Response(track) def save_product(product_id, store_id, data): """Function to save mapping data to product store mapping. Args: product_id (int): The product_id for the product store mapping. store_id (int): The store_id for the product store mapping. data (dict): provider, video_order_id,store_unique_id values. Returns: Response: message containing data upon successful query. error Response message otherwise. """ mapping_data = { "provider": data["provider"], "video_order_id": data["video_order_id"], } result = mysql.execute_query( QUERY_INSERT_PRODUCT, ( product_id, store_id, data.get("store_unique_id", 0), data.get("track_unique_id", 0), json.dumps(mapping_data), ), ) # since this is a insert query execution, it will always returns a empty # if error while executing query result is response object. if isinstance(result, response.Response): return result response_dict = {"product_id": product_id, "store_id": store_id} response_dict.update(data) return response.Response(response_dict)