Spaces:
Sleeping

axeronis commited on
Commit
9c325cc
·
1 Parent(s): 45a98e6

fix: remove duplicate notification endpoints causing settings reset

Browse files
Files changed (1) hide show
  1. backend/app/api/notifications.py +28 -249
backend/app/api/notifications.py CHANGED
@@ -40,14 +40,14 @@ class UnreadCountResponse(BaseModel):
40
  class NotificationSettingsResponse(BaseModel):
41
  telegram_enabled: bool
42
  telegram_chat_id: Optional[str]
43
- telegram_channel_id: Optional[str] = None # Added support for group ID
44
 
45
  dashboard_enabled: bool
46
 
47
  notify_hot_leads: bool
48
  notify_qualification_complete: bool
49
- notify_orders: bool = True # Added
50
- notify_bookings: bool = True # Added
51
 
52
  # Escalation
53
  notify_escalation_high: bool = True
@@ -55,56 +55,18 @@ class NotificationSettingsResponse(BaseModel):
55
  notify_escalation_low: bool = False
56
 
57
 
58
- # ... endpoints ...
59
-
60
- @router.get("/settings", response_model=NotificationSettingsResponse)
61
- async def get_notification_settings(
62
- session: AsyncSession = Depends(get_async_session),
63
- current_user: User = Depends(get_current_user)
64
- ):
65
- """Get notification settings."""
66
- stmt = select(NotificationSettings).where(
67
- NotificationSettings.tenant_id == current_user.tenant_id
68
- )
69
- result = await session.execute(stmt)
70
- settings = result.scalar_one_or_none()
71
-
72
- if not settings:
73
- # Create default settings
74
- settings = NotificationSettings(tenant_id=current_user.tenant_id)
75
- session.add(settings)
76
- await session.commit()
77
- await session.refresh(settings)
78
-
79
- return NotificationSettingsResponse(
80
- telegram_enabled=settings.telegram_enabled,
81
- telegram_chat_id=settings.telegram_chat_id,
82
- telegram_channel_id=settings.telegram_channel_id,
83
- dashboard_enabled=settings.dashboard_enabled,
84
- notify_hot_leads=settings.notify_hot_leads,
85
- notify_qualification_complete=settings.notify_qualification_complete,
86
- notify_orders=settings.notify_orders,
87
- notify_bookings=settings.notify_bookings,
88
-
89
- # Escalation
90
- notify_escalation_high=settings.notify_escalation_high,
91
- notify_escalation_medium=settings.notify_escalation_medium,
92
- notify_escalation_low=settings.notify_escalation_low
93
- )
94
-
95
-
96
  class UpdateNotificationSettingsRequest(BaseModel):
97
  telegram_enabled: Optional[bool] = None
98
  telegram_chat_id: Optional[str] = None
99
- telegram_channel_id: Optional[str] = None # Added support for group ID
100
 
101
  dashboard_enabled: Optional[bool] = None
102
 
103
  # Notification Types
104
  notify_hot_leads: Optional[bool] = None
105
  notify_qualification_complete: Optional[bool] = None
106
- notify_orders: Optional[bool] = None # Added
107
- notify_bookings: Optional[bool] = None # Added
108
 
109
  # Escalation Levels
110
  notify_escalation_high: Optional[bool] = None
@@ -112,151 +74,16 @@ class UpdateNotificationSettingsRequest(BaseModel):
112
  notify_escalation_low: Optional[bool] = None
113
 
114
 
115
- # ... (skipping endpoints) ...
116
-
117
- @router.put("/settings")
118
- async def update_notification_settings(
119
- request: UpdateNotificationSettingsRequest,
120
- session: AsyncSession = Depends(get_async_session),
121
- current_user: User = Depends(get_current_user)
122
- ):
123
- """Update notification settings."""
124
- stmt = select(NotificationSettings).where(
125
- NotificationSettings.tenant_id == current_user.tenant_id
126
- )
127
- result = await session.execute(stmt)
128
- settings = result.scalar_one_or_none()
129
-
130
- if not settings:
131
- settings = NotificationSettings(tenant_id=current_user.tenant_id)
132
- session.add(settings)
133
-
134
- # Update fields
135
- if request.telegram_enabled is not None:
136
- settings.telegram_enabled = request.telegram_enabled
137
- if request.telegram_chat_id is not None:
138
- settings.telegram_chat_id = request.telegram_chat_id
139
- if request.telegram_channel_id is not None:
140
- settings.telegram_channel_id = request.telegram_channel_id
141
- if request.dashboard_enabled is not None:
142
- settings.dashboard_enabled = request.dashboard_enabled
143
- if request.notify_hot_leads is not None:
144
- settings.notify_hot_leads = request.notify_hot_leads
145
- if request.notify_qualification_complete is not None:
146
- settings.notify_qualification_complete = request.notify_qualification_complete
147
- if request.notify_orders is not None:
148
- settings.notify_orders = request.notify_orders
149
- if request.notify_bookings is not None:
150
- settings.notify_bookings = request.notify_bookings
151
-
152
- # Escalation
153
- if request.notify_escalation_high is not None:
154
- settings.notify_escalation_high = request.notify_escalation_high
155
- if request.notify_escalation_medium is not None:
156
- settings.notify_escalation_medium = request.notify_escalation_medium
157
- if request.notify_escalation_low is not None:
158
- settings.notify_escalation_low = request.notify_escalation_low
159
-
160
- settings.updated_at = datetime.utcnow()
161
- await session.commit()
162
-
163
- return {"success": True}
164
-
165
-
166
  # ============================================================================
167
- # Endpoints
168
  # ============================================================================
169
 
170
- @router.get("/unread-count", response_model=UnreadCountResponse)
171
- async def get_unread_count(
172
- session: AsyncSession = Depends(get_async_session),
173
- current_user: User = Depends(get_current_user)
174
- ):
175
- """Get count of unread notifications."""
176
- stmt = select(func.count(Notification.id)).where(
177
- Notification.tenant_id == current_user.tenant_id,
178
- Notification.is_read == False
179
- )
180
- count = await session.scalar(stmt)
181
-
182
- return UnreadCountResponse(unread_count=count or 0)
183
-
184
-
185
- @router.get("/", response_model=List[NotificationResponse])
186
- async def get_notifications(
187
- limit: int = 20,
188
- session: AsyncSession = Depends(get_async_session),
189
- current_user: User = Depends(get_current_user)
190
- ):
191
- """Get list of notifications (last 20)."""
192
- stmt = select(Notification).where(
193
- Notification.tenant_id == current_user.tenant_id
194
- ).order_by(Notification.created_at.desc()).limit(limit)
195
-
196
- result = await session.execute(stmt)
197
- notifications = result.scalars().all()
198
-
199
- return [
200
- NotificationResponse(
201
- id=n.id,
202
- type=n.type,
203
- title=n.title,
204
- message=n.message,
205
- is_read=n.is_read,
206
- created_at=n.created_at.isoformat(),
207
- lead_id=n.lead_id
208
- )
209
- for n in notifications
210
- ]
211
-
212
-
213
- @router.post("/{notification_id}/mark-read")
214
- async def mark_notification_read(
215
- notification_id: UUID,
216
- session: AsyncSession = Depends(get_async_session),
217
- current_user: User = Depends(get_current_user)
218
- ):
219
- """Mark notification as read."""
220
- stmt = select(Notification).where(
221
- Notification.id == notification_id,
222
- Notification.tenant_id == current_user.tenant_id
223
- )
224
- result = await session.execute(stmt)
225
- notification = result.scalar_one_or_none()
226
-
227
- if not notification:
228
- raise HTTPException(status_code=404, detail="Notification not found")
229
-
230
- notification.is_read = True
231
- notification.read_at = datetime.utcnow()
232
- await session.commit()
233
-
234
- return {"success": True}
235
-
236
-
237
- @router.post("/mark-all-read")
238
- async def mark_all_notifications_read(
239
- session: AsyncSession = Depends(get_async_session),
240
- current_user: User = Depends(get_current_user)
241
- ):
242
- """Mark all notifications as read."""
243
- stmt = update(Notification).where(
244
- Notification.tenant_id == current_user.tenant_id,
245
- Notification.is_read == False
246
- ).values(is_read=True, read_at=datetime.utcnow())
247
-
248
- await session.execute(stmt)
249
- await session.commit()
250
-
251
- return {"success": True}
252
-
253
-
254
  @router.get("/settings", response_model=NotificationSettingsResponse)
255
  async def get_notification_settings(
256
  session: AsyncSession = Depends(get_async_session),
257
  current_user: User = Depends(get_current_user)
258
  ):
259
- """Get notification settings."""
260
  stmt = select(NotificationSettings).where(
261
  NotificationSettings.tenant_id == current_user.tenant_id
262
  )
@@ -273,10 +100,15 @@ async def get_notification_settings(
273
  return NotificationSettingsResponse(
274
  telegram_enabled=settings.telegram_enabled,
275
  telegram_chat_id=settings.telegram_chat_id,
276
- telegram_channel_id=settings.telegram_channel_id, # Include group ID
277
  dashboard_enabled=settings.dashboard_enabled,
278
  notify_hot_leads=settings.notify_hot_leads,
279
- notify_qualification_complete=settings.notify_qualification_complete
 
 
 
 
 
280
  )
281
 
282
 
@@ -286,7 +118,7 @@ async def update_notification_settings(
286
  session: AsyncSession = Depends(get_async_session),
287
  current_user: User = Depends(get_current_user)
288
  ):
289
- """Update notification settings."""
290
  stmt = select(NotificationSettings).where(
291
  NotificationSettings.tenant_id == current_user.tenant_id
292
  )
@@ -297,7 +129,7 @@ async def update_notification_settings(
297
  settings = NotificationSettings(tenant_id=current_user.tenant_id)
298
  session.add(settings)
299
 
300
- # Update fields
301
  if request.telegram_enabled is not None:
302
  settings.telegram_enabled = request.telegram_enabled
303
  if request.telegram_chat_id is not None:
@@ -310,6 +142,16 @@ async def update_notification_settings(
310
  settings.notify_hot_leads = request.notify_hot_leads
311
  if request.notify_qualification_complete is not None:
312
  settings.notify_qualification_complete = request.notify_qualification_complete
 
 
 
 
 
 
 
 
 
 
313
 
314
  settings.updated_at = datetime.utcnow()
315
  await session.commit()
@@ -318,7 +160,7 @@ async def update_notification_settings(
318
 
319
 
320
  # ============================================================================
321
- # Endpoints
322
  # ============================================================================
323
 
324
  @router.get("/unread-count", response_model=UnreadCountResponse)
@@ -403,66 +245,3 @@ async def mark_all_notifications_read(
403
  await session.commit()
404
 
405
  return {"success": True}
406
-
407
-
408
- @router.get("/settings", response_model=NotificationSettingsResponse)
409
- async def get_notification_settings(
410
- session: AsyncSession = Depends(get_async_session),
411
- current_user: User = Depends(get_current_user)
412
- ):
413
- """Get notification settings."""
414
- stmt = select(NotificationSettings).where(
415
- NotificationSettings.tenant_id == current_user.tenant_id
416
- )
417
- result = await session.execute(stmt)
418
- settings = result.scalar_one_or_none()
419
-
420
- if not settings:
421
- # Create default settings
422
- settings = NotificationSettings(tenant_id=current_user.tenant_id)
423
- session.add(settings)
424
- await session.commit()
425
- await session.refresh(settings)
426
-
427
- return NotificationSettingsResponse(
428
- telegram_enabled=settings.telegram_enabled,
429
- telegram_chat_id=settings.telegram_chat_id,
430
- dashboard_enabled=settings.dashboard_enabled,
431
- notify_hot_leads=settings.notify_hot_leads,
432
- notify_qualification_complete=settings.notify_qualification_complete
433
- )
434
-
435
-
436
- @router.put("/settings")
437
- async def update_notification_settings(
438
- request: UpdateNotificationSettingsRequest,
439
- session: AsyncSession = Depends(get_async_session),
440
- current_user: User = Depends(get_current_user)
441
- ):
442
- """Update notification settings."""
443
- stmt = select(NotificationSettings).where(
444
- NotificationSettings.tenant_id == current_user.tenant_id
445
- )
446
- result = await session.execute(stmt)
447
- settings = result.scalar_one_or_none()
448
-
449
- if not settings:
450
- settings = NotificationSettings(tenant_id=current_user.tenant_id)
451
- session.add(settings)
452
-
453
- # Update fields
454
- if request.telegram_enabled is not None:
455
- settings.telegram_enabled = request.telegram_enabled
456
- if request.telegram_chat_id is not None:
457
- settings.telegram_chat_id = request.telegram_chat_id
458
- if request.dashboard_enabled is not None:
459
- settings.dashboard_enabled = request.dashboard_enabled
460
- if request.notify_hot_leads is not None:
461
- settings.notify_hot_leads = request.notify_hot_leads
462
- if request.notify_qualification_complete is not None:
463
- settings.notify_qualification_complete = request.notify_qualification_complete
464
-
465
- settings.updated_at = datetime.utcnow()
466
- await session.commit()
467
-
468
- return {"success": True}
 
40
  class NotificationSettingsResponse(BaseModel):
41
  telegram_enabled: bool
42
  telegram_chat_id: Optional[str]
43
+ telegram_channel_id: Optional[str] = None
44
 
45
  dashboard_enabled: bool
46
 
47
  notify_hot_leads: bool
48
  notify_qualification_complete: bool
49
+ notify_orders: bool = True
50
+ notify_bookings: bool = True
51
 
52
  # Escalation
53
  notify_escalation_high: bool = True
 
55
  notify_escalation_low: bool = False
56
 
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  class UpdateNotificationSettingsRequest(BaseModel):
59
  telegram_enabled: Optional[bool] = None
60
  telegram_chat_id: Optional[str] = None
61
+ telegram_channel_id: Optional[str] = None
62
 
63
  dashboard_enabled: Optional[bool] = None
64
 
65
  # Notification Types
66
  notify_hot_leads: Optional[bool] = None
67
  notify_qualification_complete: Optional[bool] = None
68
+ notify_orders: Optional[bool] = None
69
+ notify_bookings: Optional[bool] = None
70
 
71
  # Escalation Levels
72
  notify_escalation_high: Optional[bool] = None
 
74
  notify_escalation_low: Optional[bool] = None
75
 
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  # ============================================================================
78
+ # Settings Endpoints
79
  # ============================================================================
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  @router.get("/settings", response_model=NotificationSettingsResponse)
82
  async def get_notification_settings(
83
  session: AsyncSession = Depends(get_async_session),
84
  current_user: User = Depends(get_current_user)
85
  ):
86
+ """Get notification settings for current user's tenant."""
87
  stmt = select(NotificationSettings).where(
88
  NotificationSettings.tenant_id == current_user.tenant_id
89
  )
 
100
  return NotificationSettingsResponse(
101
  telegram_enabled=settings.telegram_enabled,
102
  telegram_chat_id=settings.telegram_chat_id,
103
+ telegram_channel_id=settings.telegram_channel_id,
104
  dashboard_enabled=settings.dashboard_enabled,
105
  notify_hot_leads=settings.notify_hot_leads,
106
+ notify_qualification_complete=settings.notify_qualification_complete,
107
+ notify_orders=settings.notify_orders,
108
+ notify_bookings=settings.notify_bookings,
109
+ notify_escalation_high=settings.notify_escalation_high,
110
+ notify_escalation_medium=settings.notify_escalation_medium,
111
+ notify_escalation_low=settings.notify_escalation_low
112
  )
113
 
114
 
 
118
  session: AsyncSession = Depends(get_async_session),
119
  current_user: User = Depends(get_current_user)
120
  ):
121
+ """Update notification settings for current user's tenant."""
122
  stmt = select(NotificationSettings).where(
123
  NotificationSettings.tenant_id == current_user.tenant_id
124
  )
 
129
  settings = NotificationSettings(tenant_id=current_user.tenant_id)
130
  session.add(settings)
131
 
132
+ # Update ALL fields
133
  if request.telegram_enabled is not None:
134
  settings.telegram_enabled = request.telegram_enabled
135
  if request.telegram_chat_id is not None:
 
142
  settings.notify_hot_leads = request.notify_hot_leads
143
  if request.notify_qualification_complete is not None:
144
  settings.notify_qualification_complete = request.notify_qualification_complete
145
+ if request.notify_orders is not None:
146
+ settings.notify_orders = request.notify_orders
147
+ if request.notify_bookings is not None:
148
+ settings.notify_bookings = request.notify_bookings
149
+ if request.notify_escalation_high is not None:
150
+ settings.notify_escalation_high = request.notify_escalation_high
151
+ if request.notify_escalation_medium is not None:
152
+ settings.notify_escalation_medium = request.notify_escalation_medium
153
+ if request.notify_escalation_low is not None:
154
+ settings.notify_escalation_low = request.notify_escalation_low
155
 
156
  settings.updated_at = datetime.utcnow()
157
  await session.commit()
 
160
 
161
 
162
  # ============================================================================
163
+ # Notification List Endpoints
164
  # ============================================================================
165
 
166
  @router.get("/unread-count", response_model=UnreadCountResponse)
 
245
  await session.commit()
246
 
247
  return {"success": True}