"""Meta Update Queue Model CRUD operation.""" from oto import response from sqlalchemy import BigInteger from sqlalchemy import Column from sqlalchemy import DateTime from sqlalchemy import ForeignKey from sqlalchemy import func as sql_func from sqlalchemy import Integer from sqlalchemy import String from sqlalchemy.orm import relationship from product_workflow.connectors import mysql from product_workflow.constants import error from product_workflow.models.sql.delivery_history_records \ import GET_PREVIOUSLY_DELIVERED_STORES_SQL class MetaUpdateQueue(mysql.BaseModel): """Table definition for meta_update_queue table.""" __tablename__ = 'meta_update_queue' meta_update_queue_id = Column( 'meta_update_queue_id', Integer, primary_key=True, nullable=False) upc = Column(BigInteger, nullable=False) orchadmin_user_id = Column(BigInteger, nullable=False, default=179) date_added = Column(DateTime, default=sql_func.now()) update_type = Column(String, default='update') description = Column(String, default=None) update = relationship( 'MetaUpdateDmsMaster', back_populates='meta_update_queue', cascade='save-update, merge, delete-orphan, delete' ) def to_dict(self): """Return a dictionary of a meta_update_queue.""" return { 'meta_update_queue_id': self.meta_update_queue_id, 'upc': self.upc, } class MetaUpdateDmsMaster(mysql.BaseModel): """Table definition for meta_update_dms_master table.""" __tablename__ = 'meta_update_dms_master' meta_update_dms_master_id = Column( 'meta_update_dms_master_id', Integer, primary_key=True, nullable=False) meta_update_queue_id = Column( 'meta_update_queue_id', Integer, ForeignKey('meta_update_queue.meta_update_queue_id'), nullable=False) meta_update_queue = relationship( 'MetaUpdateQueue', back_populates='update' ) customer_master_master_id = Column(Integer, nullable=False) @mysql.wrap_db_errors def create_meta_update_queue(upc, data): """Create meta_update_queue record for the UPC. Args: upc (String): UPC for which meta_update_queue record to create. data (Dict): Dict with additional parameters for the meta_update_queue record. Return: response: message containing data upon successful response. """ with mysql.db_session() as session: delivery_history_store_ids = get_delivery_history_stores(upc) if not delivery_history_store_ids: return response.create_not_found_response( message=error.ERROR_MESSAGE_HISTORY_NOT_EXIST.format(upc)) delivery_history_store_ids_set = set(delivery_history_store_ids) selected_store_ids_set = set(data.get('delivery_store_ids', [])) update_store_ids = list( delivery_history_store_ids_set.intersection(selected_store_ids_set)) if not update_store_ids: return response.create_not_found_response( message=error.ERROR_MESSAGE_SELECTED_NOT_IN_HISTORY.format(upc)) meta_update = MetaUpdateQueue( upc=upc, update=list(map( lambda x: MetaUpdateDmsMaster(customer_master_master_id=x), update_store_ids )), description=data.get('description') ) session.add(meta_update) session.commit() return response.Response( status=201, message='Meta Update Queue created successfully' ) @mysql.wrap_db_errors def get_delivery_history_stores(upc): """Get delivery history records for the UPC. Args: upc (String): UPC for which delivery history records has to fetched. Return: response: List of store ids. """ with mysql.db_session() as session: delivery_history_records = session.execute( GET_PREVIOUSLY_DELIVERED_STORES_SQL, {'upc': upc}).fetchall() stores_list = [] store_ids = [dict(row) for row in delivery_history_records] for store in store_ids: stores_list.append(store['customer_master_master_id']) return stores_list