| 1 | from helpers.api import ApiHandler |
| 2 | from flask import Request, Response |
| 3 | from helpers.notification import NotificationManager, NotificationPriority, NotificationType |
| 4 | |
| 5 | |
| 6 | class NotificationCreate(ApiHandler): |
| 7 | @classmethod |
| 8 | def requires_auth(cls) -> bool: |
| 9 | return True |
| 10 | |
| 11 | async def process(self, input: dict, request: Request) -> dict | Response: |
| 12 | # Extract notification data |
| 13 | notification_type = input.get("type", NotificationType.INFO.value) |
| 14 | priority = input.get("priority", NotificationPriority.NORMAL.value) |
| 15 | message = input.get("message", "") |
| 16 | title = input.get("title", "") |
| 17 | detail = input.get("detail", "") |
| 18 | display_time = input.get("display_time", 3) # Default to 3 seconds |
| 19 | group = input.get("group", "") # Group parameter for notification grouping |
| 20 | notification_id = input.get("id", "") |
| 21 | |
| 22 | # Validate required fields |
| 23 | if not message: |
| 24 | return {"success": False, "error": "Message is required"} |
| 25 | |
| 26 | # Validate display_time |
| 27 | try: |
| 28 | display_time = int(display_time) |
| 29 | if display_time < 0: |
| 30 | display_time = 3 # Reset to default if negative |
| 31 | except (ValueError, TypeError): |
| 32 | display_time = 3 # Reset to default if not numeric |
| 33 | |
| 34 | # Validate notification type |
| 35 | try: |
| 36 | if isinstance(notification_type, str): |
| 37 | notification_type = NotificationType(notification_type.lower()) |
| 38 | except ValueError: |
| 39 | return { |
| 40 | "success": False, |
| 41 | "error": f"Invalid notification type: {notification_type}", |
| 42 | } |
| 43 | |
| 44 | # Create notification using the appropriate helper method |
| 45 | try: |
| 46 | notification = NotificationManager.send_notification( |
| 47 | notification_type, |
| 48 | priority, |
| 49 | message, |
| 50 | title, |
| 51 | detail, |
| 52 | display_time, |
| 53 | group, |
| 54 | notification_id, |
| 55 | ) |
| 56 | |
| 57 | return { |
| 58 | "success": True, |
| 59 | "notification_id": notification.id, |
| 60 | "notification": notification.output(), |
| 61 | "message": "Notification created successfully", |
| 62 | } |
| 63 | |
| 64 | except Exception as e: |
| 65 | return { |
| 66 | "success": False, |
| 67 | "error": f"Failed to create notification: {str(e)}", |
| 68 | } |