""" Notification Helpers ==================== Functions for creating and managing user notifications. """ import logging from database import SessionLocal, UserNotification, User logger = logging.getLogger(__name__) def create_notification(user_id, title, message, notification_type='info', related_type=None, related_id=None, action_url=None): """ Create a notification for a user. Args: user_id: ID of the user to notify title: Notification title message: Notification message/body notification_type: Type of notification (news, system, message, event, alert) related_type: Type of related entity (company_news, event, message, etc.) related_id: ID of the related entity action_url: URL to navigate when notification is clicked Returns: UserNotification object or None on error """ db = SessionLocal() try: notification = UserNotification( user_id=user_id, title=title, message=message, notification_type=notification_type, related_type=related_type, related_id=related_id, action_url=action_url ) db.add(notification) db.commit() db.refresh(notification) logger.info(f"Created notification for user {user_id}: {title}") return notification except Exception as e: logger.error(f"Error creating notification: {e}") db.rollback() return None finally: db.close() def create_news_notification(company_id, news_id, news_title): """ Create notification for company owner when their news is approved. Args: company_id: ID of the company news_id: ID of the approved news news_title: Title of the news """ db = SessionLocal() try: # Find users associated with this company users = db.query(User).filter( User.company_id == company_id, User.is_active == True ).all() for user in users: create_notification( user_id=user.id, title="Nowa aktualnosc o Twojej firmie", message=f"Aktualnosc '{news_title}' zostala zatwierdzona i jest widoczna na profilu firmy.", notification_type='news', related_type='company_news', related_id=news_id, action_url=f"/company/{company_id}" ) finally: db.close() def create_message_notification(user_id, sender_name, message_id): """ Create notification when user receives a private message. Args: user_id: ID of the recipient sender_name: Name of the sender message_id: ID of the message """ create_notification( user_id=user_id, title="Nowa wiadomość prywatna", message=f"Otrzymałeś nową wiadomość od {sender_name}.", notification_type='message', related_type='private_message', related_id=message_id, action_url=f"/wiadomosci/{message_id}" ) def create_event_notification(user_id, event_title, event_id): """ Create notification for upcoming event reminder. Args: user_id: ID of the user to notify event_title: Title of the event event_id: ID of the event """ create_notification( user_id=user_id, title="Przypomnienie o wydarzeniu", message=f"Zbliża się wydarzenie: {event_title}", notification_type='event', related_type='norda_event', related_id=event_id, action_url=f"/kalendarz/{event_id}" )