"""Notification service.""" from typing import List, Optional from sqlalchemy.orm import Session from datetime import datetime from app.models.notification import Notification from app.models.user import User class NotificationService: """Notification service.""" def __init__(self, db: Session): self.db = db def create_notification( self, user_id: int, ticket_id: int, notification_type: str, title: str, message: str, is_read: int = 0, is_delivered: int = 0 ) -> Notification: """Create notification.""" notification = Notification( user_id=user_id, ticket_id=ticket_id, type=notification_type, title=title, message=message, is_read=is_read, is_delivered=is_delivered ) self.db.add(notification) self.db.commit() self.db.refresh(notification) return notification def get_user_notifications( self, user_id: int, is_read: Optional[int] = None ) -> List[Notification]: """Get user notifications.""" query = self.db.query(Notification).filter(Notification.user_id == user_id) if is_read is not None: query = query.filter(Notification.is_read == is_read) return query.order_by(Notification.created_at.desc()).all() def mark_as_read(self, notification_id: int, user_id: int) -> Notification: """Mark notification as read.""" notification = self.db.query(Notification).filter( Notification.id == notification_id, Notification.user_id == user_id ).first() if not notification: raise ValueError("Notification not found") notification.is_read = 1 self.db.commit() self.db.refresh(notification) return notification def mark_all_as_read(self, user_id: int) -> int: """Mark all notifications as read for user.""" updated = self.db.query(Notification).filter( Notification.user_id == user_id, Notification.is_read == 0 ).update({"is_read": 1}) self.db.commit() return updated def delete_notification(self, notification_id: int, user_id: int) -> bool: """Delete notification.""" notification = self.db.query(Notification).filter( Notification.id == notification_id, Notification.user_id == user_id ).first() if notification: self.db.delete(notification) self.db.commit() return True return False