File size: 6,127 Bytes
22087d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from fastapi import APIRouter, Depends, HTTPException, Request
from typing import Dict, List, Optional, Any
from pydantic import BaseModel
from core.auth import get_current_user
from core.utils.logger import logger
from core.utils.config import config, EnvMode
from .notification_service import notification_service
from .models import NotificationChannel, NotificationEvent, NotificationPriority

router = APIRouter(tags=["notifications"], prefix="/notifications")


def check_notifications_enabled():
    if config.ENV_MODE != EnvMode.STAGING:
        raise HTTPException(
            status_code=403, 
            detail=f"Notifications are only available in staging mode (current mode: {config.ENV_MODE.value})"
        )


class NotificationSettingsUpdate(BaseModel):
    email_enabled: Optional[bool] = None
    push_enabled: Optional[bool] = None
    in_app_enabled: Optional[bool] = None


class DeviceTokenRequest(BaseModel):
    device_token: str
    device_type: str = "mobile"
    provider: str = "expo"


class TestNotificationRequest(BaseModel):
    event_type: NotificationEvent = NotificationEvent.SYSTEM_ALERT
    channels: Optional[List[NotificationChannel]] = None
    title: str = "Test Notification"
    message: str = "This is a test notification from Kortix"


class SendNotificationRequest(BaseModel):
    account_id: str
    event_type: NotificationEvent
    data: Dict[str, Any]
    channels: Optional[List[NotificationChannel]] = None
    priority: NotificationPriority = NotificationPriority.MEDIUM


@router.get("/settings")
async def get_notification_settings(current_user: dict = Depends(get_current_user)):
    check_notifications_enabled()
    try:
        account_id = current_user.get('user_id')
        settings = await notification_service.get_account_notification_settings(account_id)
        
        if not settings:
            settings = await notification_service.create_default_settings(account_id)
        
        return {"success": True, "settings": settings.dict()}
        
    except Exception as e:
        logger.error(f"Error getting notification settings: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))


@router.put("/settings")
async def update_notification_settings(
    settings_update: NotificationSettingsUpdate,
    current_user: dict = Depends(get_current_user)
):
    check_notifications_enabled()
    try:
        account_id = current_user.get('user_id')
        
        update_data = {k: v for k, v in settings_update.dict().items() if v is not None}
        
        success = await notification_service.update_account_notification_settings(
            account_id=account_id,
            settings=update_data
        )
        
        if not success:
            raise HTTPException(status_code=500, detail="Failed to update settings")
        
        updated_settings = await notification_service.get_account_notification_settings(account_id)
        
        return {
            "success": True,
            "message": "Notification settings updated successfully",
            "settings": updated_settings.dict() if updated_settings else None
        }
        
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Error updating notification settings: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/device-token")
async def register_device_token(
    token_request: DeviceTokenRequest,
    current_user: dict = Depends(get_current_user)
):
    check_notifications_enabled()
    try:
        account_id = current_user.get('user_id')
        
        success = await notification_service.register_device_token(
            account_id=account_id,
            device_token=token_request.device_token,
            device_type=token_request.device_type,
            provider=token_request.provider
        )
        
        if not success:
            raise HTTPException(status_code=500, detail="Failed to register device token")
        
        return {
            "success": True,
            "message": "Device token registered successfully"
        }
        
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Error registering device token: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))


@router.delete("/device-token/{device_token}")
async def unregister_device_token(
    device_token: str,
    provider: str = "expo",
    current_user: dict = Depends(get_current_user)
):
    check_notifications_enabled()
    try:
        account_id = current_user.get('user_id')
        
        success = await notification_service.unregister_device_token(
            account_id=account_id,
            device_token=device_token,
            provider=provider
        )
        
        if not success:
            raise HTTPException(status_code=500, detail="Failed to unregister device token")
        
        return {
            "success": True,
            "message": "Device token unregistered successfully"
        }
        
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Error unregistering device token: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/webhooks/novu")
async def handle_novu_webhook(request: Request):
    try:
        payload = await request.json()
        
        logger.info(f"Received Novu webhook: {payload.get('type', 'unknown')}")
        
        event_type = payload.get('type')
        
        if event_type == 'notification.sent':
            logger.info(f"Notification sent: {payload.get('transactionId')}")
        elif event_type == 'notification.failed':
            logger.warning(f"Notification failed: {payload.get('transactionId')}")
        elif event_type == 'subscriber.created':
            logger.info(f"Subscriber created: {payload.get('subscriberId')}")
        
        return {"status": "ok"}
        
    except Exception as e:
        logger.error(f"Error handling Novu webhook: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))