Spaces:
Sleeping
Sleeping
axeronis commited on
Commit ·
cd862bf
1
Parent(s): 6e9be79
feat: add delete chat functionality with backend API and frontend UI
Browse files- backend/app/api/leads.py +54 -0
- frontend/app/chats/page.tsx +25 -0
- frontend/components/LeadsList.tsx +23 -4
- frontend/lib/api.ts +5 -0
backend/app/api/leads.py
CHANGED
|
@@ -567,3 +567,57 @@ async def update_lead_stage(
|
|
| 567 |
"lead_id": str(lead.id),
|
| 568 |
"current_stage_id": str(lead.current_stage_id) if lead.current_stage_id else None
|
| 569 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 567 |
"lead_id": str(lead.id),
|
| 568 |
"current_stage_id": str(lead.current_stage_id) if lead.current_stage_id else None
|
| 569 |
}
|
| 570 |
+
|
| 571 |
+
|
| 572 |
+
@router.delete("/{lead_id}")
|
| 573 |
+
async def delete_lead(
|
| 574 |
+
lead_id: UUID,
|
| 575 |
+
current_user: User = Depends(get_current_user),
|
| 576 |
+
session = Depends(get_session)
|
| 577 |
+
):
|
| 578 |
+
"""
|
| 579 |
+
Delete a lead and all associated data (conversations, messages).
|
| 580 |
+
Tenant-isolated - users can only delete their own leads.
|
| 581 |
+
"""
|
| 582 |
+
# Use authenticated user's tenant_id (prevents tenant isolation breach)
|
| 583 |
+
tenant_id = current_user.tenant_id
|
| 584 |
+
|
| 585 |
+
# Get lead
|
| 586 |
+
stmt = select(Lead).where(
|
| 587 |
+
Lead.tenant_id == tenant_id,
|
| 588 |
+
Lead.id == lead_id
|
| 589 |
+
)
|
| 590 |
+
result = await session.execute(stmt)
|
| 591 |
+
lead = result.scalar_one_or_none()
|
| 592 |
+
|
| 593 |
+
if not lead:
|
| 594 |
+
raise HTTPException(status_code=404, detail="Lead not found")
|
| 595 |
+
|
| 596 |
+
lead_name = lead.name or "Unnamed"
|
| 597 |
+
|
| 598 |
+
# Delete all messages from conversations first
|
| 599 |
+
conv_stmt = select(Conversation).where(Conversation.lead_id == lead_id)
|
| 600 |
+
conv_result = await session.execute(conv_stmt)
|
| 601 |
+
conversations = conv_result.scalars().all()
|
| 602 |
+
|
| 603 |
+
for conv in conversations:
|
| 604 |
+
# Delete messages for this conversation
|
| 605 |
+
msg_delete_stmt = select(Message).where(Message.conversation_id == conv.id)
|
| 606 |
+
msg_result = await session.execute(msg_delete_stmt)
|
| 607 |
+
messages = msg_result.scalars().all()
|
| 608 |
+
for msg in messages:
|
| 609 |
+
await session.delete(msg)
|
| 610 |
+
|
| 611 |
+
# Delete conversation
|
| 612 |
+
await session.delete(conv)
|
| 613 |
+
|
| 614 |
+
# Delete lead
|
| 615 |
+
await session.delete(lead)
|
| 616 |
+
await session.commit()
|
| 617 |
+
|
| 618 |
+
return {
|
| 619 |
+
"ok": True,
|
| 620 |
+
"message": f"Lead '{lead_name}' and all associated data deleted successfully",
|
| 621 |
+
"lead_id": str(lead_id)
|
| 622 |
+
}
|
| 623 |
+
|
frontend/app/chats/page.tsx
CHANGED
|
@@ -3,6 +3,7 @@
|
|
| 3 |
import { useState, useEffect, useMemo, Suspense } from 'react'
|
| 4 |
import { useRouter, useSearchParams } from 'next/navigation'
|
| 5 |
import { useAuth } from '@/contexts/AuthContext'
|
|
|
|
| 6 |
import { leadsApi, Lead, Message } from '@/lib/api'
|
| 7 |
import { useQuery } from '@tanstack/react-query'
|
| 8 |
import { LeadsList } from '@/components/LeadsList'
|
|
@@ -16,6 +17,7 @@ function ChatsContent() {
|
|
| 16 |
const router = useRouter()
|
| 17 |
const searchParams = useSearchParams()
|
| 18 |
const { user, isLoading: authLoading } = useAuth()
|
|
|
|
| 19 |
const [selectedLead, setSelectedLead] = useState<Lead | undefined>(undefined)
|
| 20 |
const [messages, setMessages] = useState<Message[]>([])
|
| 21 |
const [messagesLoading, setMessagesLoading] = useState(false)
|
|
@@ -96,6 +98,28 @@ function ChatsContent() {
|
|
| 96 |
}
|
| 97 |
}
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
if (authLoading || !user) {
|
| 100 |
return (
|
| 101 |
<div className="flex items-center justify-center h-screen bg-background">
|
|
@@ -122,6 +146,7 @@ function ChatsContent() {
|
|
| 122 |
isLoading={leadsLoading}
|
| 123 |
selectedLeadId={selectedLead?.id}
|
| 124 |
onSelectLead={handleSelectLead}
|
|
|
|
| 125 |
searchQuery={searchQuery}
|
| 126 |
setSearchQuery={setSearchQuery}
|
| 127 |
activeTab={activeTab}
|
|
|
|
| 3 |
import { useState, useEffect, useMemo, Suspense } from 'react'
|
| 4 |
import { useRouter, useSearchParams } from 'next/navigation'
|
| 5 |
import { useAuth } from '@/contexts/AuthContext'
|
| 6 |
+
import { useToast } from '@/contexts/ToastContext'
|
| 7 |
import { leadsApi, Lead, Message } from '@/lib/api'
|
| 8 |
import { useQuery } from '@tanstack/react-query'
|
| 9 |
import { LeadsList } from '@/components/LeadsList'
|
|
|
|
| 17 |
const router = useRouter()
|
| 18 |
const searchParams = useSearchParams()
|
| 19 |
const { user, isLoading: authLoading } = useAuth()
|
| 20 |
+
const toast = useToast()
|
| 21 |
const [selectedLead, setSelectedLead] = useState<Lead | undefined>(undefined)
|
| 22 |
const [messages, setMessages] = useState<Message[]>([])
|
| 23 |
const [messagesLoading, setMessagesLoading] = useState(false)
|
|
|
|
| 98 |
}
|
| 99 |
}
|
| 100 |
|
| 101 |
+
// Delete Lead Handler
|
| 102 |
+
const handleDeleteLead = async (leadId: string) => {
|
| 103 |
+
try {
|
| 104 |
+
await leadsApi.deleteLead(leadId)
|
| 105 |
+
toast.success('Удалено', 'Чат успешно удален')
|
| 106 |
+
|
| 107 |
+
// Clear selection if deleted lead was selected
|
| 108 |
+
if (selectedLead?.id === leadId) {
|
| 109 |
+
setSelectedLead(undefined)
|
| 110 |
+
setMessages([])
|
| 111 |
+
setConversation(null)
|
| 112 |
+
localStorage.removeItem('last_lead_id')
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
// Refresh leads list
|
| 116 |
+
await refetchLeads()
|
| 117 |
+
} catch (error) {
|
| 118 |
+
console.error('Error deleting lead:', error)
|
| 119 |
+
toast.error('Ошибка', 'Не удалось удалить чат')
|
| 120 |
+
}
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
if (authLoading || !user) {
|
| 124 |
return (
|
| 125 |
<div className="flex items-center justify-center h-screen bg-background">
|
|
|
|
| 146 |
isLoading={leadsLoading}
|
| 147 |
selectedLeadId={selectedLead?.id}
|
| 148 |
onSelectLead={handleSelectLead}
|
| 149 |
+
onDeleteLead={handleDeleteLead}
|
| 150 |
searchQuery={searchQuery}
|
| 151 |
setSearchQuery={setSearchQuery}
|
| 152 |
activeTab={activeTab}
|
frontend/components/LeadsList.tsx
CHANGED
|
@@ -2,13 +2,14 @@
|
|
| 2 |
|
| 3 |
import { Lead } from '@/lib/api'
|
| 4 |
import { cn } from '@/lib/utils'
|
| 5 |
-
import { Search, Flame, Inbox, CheckCircle2, MessageCircle, Mail, Linkedin } from 'lucide-react'
|
| 6 |
|
| 7 |
interface LeadsListProps {
|
| 8 |
leads: Lead[] | undefined
|
| 9 |
isLoading: boolean
|
| 10 |
selectedLeadId: string | undefined
|
| 11 |
onSelectLead: (lead: Lead) => void
|
|
|
|
| 12 |
searchQuery: string
|
| 13 |
setSearchQuery: (query: string) => void
|
| 14 |
activeTab: 'all' | 'hot' | 'unread' | 'whatsapp' | 'email' | 'linkedin'
|
|
@@ -20,6 +21,7 @@ export function LeadsList({
|
|
| 20 |
isLoading,
|
| 21 |
selectedLeadId,
|
| 22 |
onSelectLead,
|
|
|
|
| 23 |
searchQuery,
|
| 24 |
setSearchQuery,
|
| 25 |
activeTab,
|
|
@@ -163,9 +165,26 @@ export function LeadsList({
|
|
| 163 |
)}>
|
| 164 |
{lead.name || 'Без имени'}
|
| 165 |
</h3>
|
| 166 |
-
<
|
| 167 |
-
{
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
</div>
|
| 170 |
|
| 171 |
<p className={cn(
|
|
|
|
| 2 |
|
| 3 |
import { Lead } from '@/lib/api'
|
| 4 |
import { cn } from '@/lib/utils'
|
| 5 |
+
import { Search, Flame, Inbox, CheckCircle2, MessageCircle, Mail, Linkedin, Trash2 } from 'lucide-react'
|
| 6 |
|
| 7 |
interface LeadsListProps {
|
| 8 |
leads: Lead[] | undefined
|
| 9 |
isLoading: boolean
|
| 10 |
selectedLeadId: string | undefined
|
| 11 |
onSelectLead: (lead: Lead) => void
|
| 12 |
+
onDeleteLead?: (leadId: string) => void
|
| 13 |
searchQuery: string
|
| 14 |
setSearchQuery: (query: string) => void
|
| 15 |
activeTab: 'all' | 'hot' | 'unread' | 'whatsapp' | 'email' | 'linkedin'
|
|
|
|
| 21 |
isLoading,
|
| 22 |
selectedLeadId,
|
| 23 |
onSelectLead,
|
| 24 |
+
onDeleteLead,
|
| 25 |
searchQuery,
|
| 26 |
setSearchQuery,
|
| 27 |
activeTab,
|
|
|
|
| 165 |
)}>
|
| 166 |
{lead.name || 'Без имени'}
|
| 167 |
</h3>
|
| 168 |
+
<div className="flex items-center gap-1">
|
| 169 |
+
{/* Delete Button - shows on hover */}
|
| 170 |
+
{onDeleteLead && (
|
| 171 |
+
<button
|
| 172 |
+
onClick={(e) => {
|
| 173 |
+
e.stopPropagation()
|
| 174 |
+
if (confirm(`Удалить чат с "${lead.name || 'Без имени'}"? Все сообщения будут удалены.`)) {
|
| 175 |
+
onDeleteLead(lead.id)
|
| 176 |
+
}
|
| 177 |
+
}}
|
| 178 |
+
className="p-1 opacity-0 group-hover:opacity-100 hover:bg-red-500/20 rounded-md transition-all"
|
| 179 |
+
title="Удалить чат"
|
| 180 |
+
>
|
| 181 |
+
<Trash2 className="w-3.5 h-3.5 text-red-400 hover:text-red-300" />
|
| 182 |
+
</button>
|
| 183 |
+
)}
|
| 184 |
+
<span className="text-[10px] text-slate-500 font-medium whitespace-nowrap">
|
| 185 |
+
{new Date(lead.last_message_at).toLocaleTimeString('ru-RU', { hour: '2-digit', minute: '2-digit' })}
|
| 186 |
+
</span>
|
| 187 |
+
</div>
|
| 188 |
</div>
|
| 189 |
|
| 190 |
<p className={cn(
|
frontend/lib/api.ts
CHANGED
|
@@ -124,6 +124,11 @@ export const leadsApi = {
|
|
| 124 |
message: message
|
| 125 |
})
|
| 126 |
return response.data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
}
|
| 128 |
}
|
| 129 |
|
|
|
|
| 124 |
message: message
|
| 125 |
})
|
| 126 |
return response.data
|
| 127 |
+
},
|
| 128 |
+
|
| 129 |
+
deleteLead: async (leadId: string) => {
|
| 130 |
+
const response = await api.delete(`/api/v1/leads/${leadId}`)
|
| 131 |
+
return response.data
|
| 132 |
}
|
| 133 |
}
|
| 134 |
|