"""Account Note model.""" from datetime import datetime, timezone from product_review.api import db class AccountNote(db.Model): """Account Note model.""" __tablename__ = "account_note" vendor_id = db.Column(db.Integer, primary_key=True, nullable=False) note = db.Column(db.String(1000), nullable=False) updated_timestamp = db.Column( db.DateTime, nullable=False, server_default=db.func.current_timestamp(), ) updated_by = db.Column(db.String, nullable=False) def to_dict(self): """Convert the AccountNote object to a dictionary representation.""" item = {} for c in self.__table__.columns: column_name = c.name item[column_name] = getattr(self, column_name) return item def get_items(): """Get all account_note entries.""" return [item.to_dict() for item in AccountNote.query.all()] def get_account_note(vendor_id): """Get a record from acccount_note table.""" entry = ( db.session.query(AccountNote).filter(AccountNote.vendor_id == vendor_id).first() ) if not entry: return None return entry.to_dict() def get_account_notes_by_vendor_ids(vendor_ids): """Get account notes by vendor ids.""" return ( db.session.query(AccountNote) .filter(AccountNote.vendor_id.in_(vendor_ids)) .all() ) def create_account_note(vendor_id, note, updated_by): """Add a record to account_note table.""" new_entry = AccountNote( vendor_id=vendor_id, note=note, updated_by=updated_by, updated_timestamp=datetime.now(), ) db.session.add(new_entry) def update_account_note(vendor_id, note, updated_by): """Update a record in account_note table.""" entry = ( db.session.query(AccountNote).filter(AccountNote.vendor_id == vendor_id).first() ) entry.note = note entry.updated_by = updated_by entry.updated_timestamp = datetime.now(timezone.utc)