Spaces:
Running

axeronis commited on
Commit
7b8ea55
·
1 Parent(s): 99e0d4e

fix: PATCH /me endpoint, manager redirect to chats, notification_settings

Browse files
backend/app/api/tenants.py CHANGED
@@ -50,9 +50,10 @@ class UpdateTenantRequest(BaseModel):
50
  sales_methodology: Optional[str] = None
51
  custom_instructions: Optional[str] = None
52
  banned_words: Optional[list] = None
53
-
54
  booking_settings: Optional[Dict[str, Any]] = None
55
  manager_monthly_salary: Optional[int] = None
 
 
56
 
57
 
58
 
@@ -139,6 +140,109 @@ async def get_my_tenant(
139
  )
140
 
141
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  @router.get("/me/usage")
143
  async def get_my_usage_stats(
144
  session: AsyncSession = Depends(get_async_session),
 
50
  sales_methodology: Optional[str] = None
51
  custom_instructions: Optional[str] = None
52
  banned_words: Optional[list] = None
 
53
  booking_settings: Optional[Dict[str, Any]] = None
54
  manager_monthly_salary: Optional[int] = None
55
+ company_name: Optional[str] = None
56
+ notification_settings: Optional[Dict[str, Any]] = None
57
 
58
 
59
 
 
140
  )
141
 
142
 
143
+ @router.patch("/me", response_model=TenantResponse)
144
+ async def update_my_tenant(
145
+ request: UpdateTenantRequest,
146
+ session: AsyncSession = Depends(get_async_session),
147
+ current_user: User = Depends(get_current_user)
148
+ ):
149
+ """
150
+ Update current user's tenant settings.
151
+ Shortcut for PATCH /tenants/{tenant_id} using current user.
152
+ """
153
+ # PERMISSION CHECK: Only OWNER can update settings
154
+ if current_user.role != UserRole.OWNER:
155
+ raise HTTPException(
156
+ status_code=status.HTTP_403_FORBIDDEN,
157
+ detail="Only OWNER can update settings. Managers have read-only access."
158
+ )
159
+
160
+ tenant_id = current_user.tenant_id
161
+
162
+ # Get tenant
163
+ result = await session.execute(
164
+ select(Tenant).where(Tenant.id == tenant_id)
165
+ )
166
+ tenant = result.scalar_one_or_none()
167
+
168
+ if not tenant:
169
+ raise HTTPException(
170
+ status_code=status.HTTP_404_NOT_FOUND,
171
+ detail="Tenant not found"
172
+ )
173
+
174
+ # Update fields
175
+ if request.ai_settings is not None:
176
+ current_settings = tenant.ai_settings or {}
177
+ current_settings.update(request.ai_settings)
178
+ tenant.ai_settings = current_settings
179
+
180
+ if request.ai_identity is not None:
181
+ current_settings = tenant.ai_settings or {}
182
+ current_settings["ai_identity"] = request.ai_identity
183
+ tenant.ai_settings = current_settings
184
+
185
+ if request.business_name is not None:
186
+ tenant.business_name = request.business_name
187
+
188
+ if request.company_name is not None:
189
+ tenant.company_name = request.company_name
190
+
191
+ if request.business_type is not None:
192
+ tenant.business_type = request.business_type
193
+
194
+ if request.tone_of_voice is not None:
195
+ current_settings = tenant.ai_settings or {}
196
+ current_settings["tone_of_voice"] = request.tone_of_voice
197
+ tenant.ai_settings = current_settings
198
+
199
+ if request.sales_methodology is not None:
200
+ current_settings = tenant.ai_settings or {}
201
+ current_settings["sales_methodology"] = request.sales_methodology
202
+ tenant.ai_settings = current_settings
203
+
204
+ if request.custom_instructions is not None:
205
+ current_settings = tenant.ai_settings or {}
206
+ current_settings["custom_instructions"] = request.custom_instructions
207
+ tenant.ai_settings = current_settings
208
+
209
+ if request.banned_words is not None:
210
+ current_settings = tenant.ai_settings or {}
211
+ current_settings["banned_words"] = request.banned_words
212
+ tenant.ai_settings = current_settings
213
+
214
+ if request.booking_settings is not None:
215
+ tenant.booking_settings = request.booking_settings
216
+
217
+ if request.manager_monthly_salary is not None:
218
+ tenant.manager_monthly_salary = request.manager_monthly_salary
219
+
220
+ if request.notification_settings is not None:
221
+ tenant.notification_settings = request.notification_settings
222
+
223
+ session.add(tenant)
224
+ await session.commit()
225
+ await session.refresh(tenant)
226
+
227
+ return TenantResponse(
228
+ id=tenant.id,
229
+ name=tenant.name,
230
+ business_name=tenant.business_name,
231
+ business_type=tenant.business_type,
232
+ ai_settings=tenant.ai_settings,
233
+ ai_identity=(tenant.ai_settings or {}).get("ai_identity"),
234
+ tone_of_voice=(tenant.ai_settings or {}).get("tone_of_voice"),
235
+ sales_methodology=(tenant.ai_settings or {}).get("sales_methodology"),
236
+ custom_instructions=(tenant.ai_settings or {}).get("custom_instructions"),
237
+ banned_words=(tenant.ai_settings or {}).get("banned_words"),
238
+ booking_settings=tenant.booking_settings,
239
+ tokens_balance=tenant.tokens_balance,
240
+ is_active=tenant.is_active,
241
+ manager_monthly_salary=tenant.manager_monthly_salary,
242
+ referred_by=None
243
+ )
244
+
245
+
246
  @router.get("/me/usage")
247
  async def get_my_usage_stats(
248
  session: AsyncSession = Depends(get_async_session),
frontend/app/dashboard/page.tsx CHANGED
@@ -1,6 +1,7 @@
1
  'use client'
2
 
3
  import { useEffect, useState } from 'react'
 
4
  import { useAuth } from '@/contexts/AuthContext'
5
  import { leadsApi, Lead, api } from '@/lib/api'
6
  import { useQuery } from '@tanstack/react-query'
@@ -80,6 +81,14 @@ const COLORS = ['#6366f1', '#8b5cf6', '#ec4899', '#f43f5e', '#10b981', '#06b6d4'
80
 
81
  export default function DashboardPage() {
82
  const { user, isLoading: authLoading } = useAuth()
 
 
 
 
 
 
 
 
83
 
84
  // Date Range State
85
  const [dateRange, setDateRange] = useState<'7d' | '30d' | '90d' | 'all'>('30d')
 
1
  'use client'
2
 
3
  import { useEffect, useState } from 'react'
4
+ import { useRouter } from 'next/navigation'
5
  import { useAuth } from '@/contexts/AuthContext'
6
  import { leadsApi, Lead, api } from '@/lib/api'
7
  import { useQuery } from '@tanstack/react-query'
 
81
 
82
  export default function DashboardPage() {
83
  const { user, isLoading: authLoading } = useAuth()
84
+ const router = useRouter()
85
+
86
+ // Redirect managers to chats (they don't have access to analytics)
87
+ useEffect(() => {
88
+ if (!authLoading && user && user.role !== 'OWNER' && user.role !== 'admin' && !user.is_superuser) {
89
+ router.replace('/chats')
90
+ }
91
+ }, [user, authLoading, router])
92
 
93
  // Date Range State
94
  const [dateRange, setDateRange] = useState<'7d' | '30d' | '90d' | 'all'>('30d')