"""Function Logic.""" from content_utils.utils.product_metadata import get_queue_fields from kafka_utils.consumer.message.product import ProductEventMessage from kafka_utils.exceptions import IneligibleEventException from src import connectors def build_initial_update(product_msg): """Build an initial set of data for patching the product.""" update_data = {} message_payload = product_msg.message.get('payload') queue_name = message_payload.get('queue_name') if queue_name: update_data['queue_name'] = queue_name user_id = message_payload.get('user_id') if user_id: update_data['moved_to_queue_by_user_id'] = user_id if not update_data: return None update_data['product_id'] = product_msg.product_id update_data['moved_to_queue_by_user_name'] = None return update_data def processing_logic(topic, value, deserializer=None): """Single event handling logic.""" product_msg = ProductEventMessage( message=value, topic=topic, value_deserializer=deserializer ) if product_msg.operation_context != 'queue_move': raise IneligibleEventException(f'skip operation: {product_msg.operation_context}') # To quickly update the review queue listing, update # the indexed queue_name immediately if possible initial_update_data = build_initial_update(product_msg) if initial_update_data: connectors.get_os_connector().patch_product(initial_update_data) queue_fields = get_queue_fields( connectors.get_graphql_connector().fetch_review_queue_item_data(product_msg.review_queue_id) ) if queue_fields.get('moved_to_queue_by_user_name'): # After fetching the name of the user who moved the product, update the index again connectors.get_os_connector().patch_product({ 'product_id': product_msg.product_id, 'moved_to_queue_by_user_name': queue_fields.get('moved_to_queue_by_user_name'), 'moved_to_target_email': queue_fields.get('moved_to_target_email'), 'moved_to_target_id': queue_fields.get('moved_to_target_id'), 'moved_to_target_name': queue_fields.get('moved_to_target_name'), 'moved_to_group_name': queue_fields.get('moved_to_group_name'), 'escalation_type': queue_fields.get('escalation_type') })