"""Model for queue_move_history table.""" from flask import g from sqlalchemy.orm import relationship from product_review.api import db from product_review.models import move_target as move_target_model class QueueMoveHistory(db.Model): """Model for queue_move_history table.""" __tablename__ = "queue_move_history" queue_move_history_id = db.Column( "id", db.Integer, primary_key=True, autoincrement=True, key="queue_move_history_id", comment="Unique id of the queue_move_history record", ) review_queue_id = db.Column( db.Integer, db.ForeignKey("review_queue.id"), nullable=False, comment="Id of the review queue item that was moved to queue_name", ) queue_name = db.Column( db.String(80), nullable=False, comment="Name of the queue review_queue_id was moved to", ) previous_queue_name = db.Column( db.String(80), nullable=False, comment="Name of previous queue that product was located in", ) moved_to_queue_by_user_id = db.Column( db.String(36), nullable=False, comment="Identity id of the user who moved review_queue_id to queue_name", ) moved_at_datetime = db.Column( db.DateTime, nullable=False, server_default=db.func.current_timestamp(), comment="Date and time the review_queue_id was moved to queue_name by user_id", ) moved_to_target_id = db.Column( db.Integer, db.ForeignKey("move_target.id"), default=None, comment="ID of move target to notify.", ) moved_to_target_name = db.Column( db.String(80), default=None, comment="Name of move target to notify.", ) moved_to_target_email = db.Column( db.String(255), default=None, comment="orchadmin_user email of move target to notify.", ) moved_to_orchadmin_user_id = db.Column( db.Integer, default=None, comment="orchadmin_user ID of move target to notify." ) move_note = db.Column( db.String, default=None, comment="Note explaining why the product was moved to queue.", ) move_target = relationship( "MoveTarget", foreign_keys=[move_target_model.MoveTarget.moved_to_target_id], primaryjoin="MoveTarget.moved_to_target_id==QueueMoveHistory.moved_to_target_id", # noqa: E501 uselist=False, lazy="select" ) def to_dict(self): """Convert the QueueMoveHistory object to a dictionary representation.""" item = {} for c in self.__table__.columns: column_name = "queue_move_history_id" if c.name == "id" else c.name item[column_name] = getattr(self, column_name) escalation_type = ( self.move_target.escalation_type if self.move_target else None ) item["escalation_type"] = escalation_type return item def add_queue_move( review_queue_id, queue_name, previous_queue_name, move_note, moved_to_target_id, moved_to_target_name, moved_to_target_email, moved_to_orchadmin_user_id, ): """Add a record to the queue_move_history table.""" db.session.add( QueueMoveHistory( review_queue_id=review_queue_id, queue_name=queue_name, previous_queue_name=previous_queue_name, move_note=move_note, moved_to_target_id=moved_to_target_id, moved_to_target_name=moved_to_target_name, moved_to_target_email=moved_to_target_email, moved_to_orchadmin_user_id=moved_to_orchadmin_user_id, moved_to_queue_by_user_id=g.request_context.identity_id, ) ) def get_queue_move_history(review_queue_id): """Get the queue move history for a review queue item.""" return [ queue_move_history_item.to_dict() for queue_move_history_item in QueueMoveHistory.query.filter_by( review_queue_id=review_queue_id ) .join(QueueMoveHistory.move_target, isouter=True) .order_by(QueueMoveHistory.queue_move_history_id.asc()) .all() ] def delete(review_queue_id): """For integration testing purposes only, delete queue move history.""" QueueMoveHistory.query.filter_by(review_queue_id=review_queue_id).delete()