"""Canned Response History model.""" from owsresponse import status as owsresponse_status from product_review.api import db from product_review.constants.error import ERROR_CODE_NOT_FOUND, ERROR_MESSAGE_NOT_FOUND from product_review.util.exception import raise_exception_for_ows_response class CannedResponseHistory(db.Model): """Canned Response History model.""" __tablename__ = "canned_response_history" canned_response_history_id = db.Column( "id", db.Integer, primary_key=True, autoincrement=True, nullable=False ) review_queue_id = db.Column( db.Integer, db.ForeignKey("review_queue.id"), nullable=False ) track_id = db.Column(db.Integer, default=None) canned_response_id = db.Column(db.Integer, nullable=False) def to_dict(self): """Convert the CannedResponseHistory object to a dictionary representation.""" item = {} for c in self.__table__.columns: column_name = "canned_response_history_id" if c.name == "id" else c.name item[column_name] = getattr(self, column_name) return item def get_canned_response_history(review_queue_id): """Get canned response history records for a review.""" records = ( db.session.query(CannedResponseHistory) .filter(CannedResponseHistory.review_queue_id == review_queue_id) .all() ) if not records: raise_exception_for_ows_response( status=owsresponse_status.NOT_FOUND, code=ERROR_CODE_NOT_FOUND, message=ERROR_MESSAGE_NOT_FOUND, ) return [item.to_dict() for item in records] def create_canned_response_history(review_queue_id, canned_response_id, track_id=None): """Create canned response history item.""" db.session.add( CannedResponseHistory( review_queue_id=review_queue_id, canned_response_id=canned_response_id, track_id=track_id, ) ) def create_bulk(review_queue_id, canned_response_ids): """Create bulk canned response history records.""" records = [] for canned_response_id in canned_response_ids: records.append( CannedResponseHistory( review_queue_id=review_queue_id, canned_response_id=canned_response_id ) ) if records: db.session.bulk_save_objects(records) def delete(review_queue_id): """For integration testing purposes only, delete canned_response_history items.""" CannedResponseHistory.query.filter( CannedResponseHistory.review_queue_id == review_queue_id).delete()