From 0831ebb25b44b645327fe1e3a2831a33ff666336 Mon Sep 17 00:00:00 2001 From: mstfyldz Date: Sun, 9 Aug 2026 10:09:15 +0300 Subject: [PATCH] feat: add DELETE /api/chat/:caseId to clear a case's chat history Frontend's chat history trash icon was calling this endpoint but it didn't exist yet, causing a JSON-parse error on the default 404 page. Also renames the LegalOS health-check message to AyrisLegal. --- src/controllers/chat.controller.ts | 18 ++++++++++++++++++ src/routes/chat.routes.ts | 3 ++- src/routes/index.ts | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/controllers/chat.controller.ts b/src/controllers/chat.controller.ts index 319ad74..46d7a5a 100644 --- a/src/controllers/chat.controller.ts +++ b/src/controllers/chat.controller.ts @@ -2,6 +2,24 @@ import { Response } from 'express'; import { supabase } from '../lib/supabase'; import { AuthenticatedRequest } from '../middleware/auth'; +export const deleteChatHistory = async (req: AuthenticatedRequest, res: Response) => { + try { + const { caseId } = req.params; + if (!caseId || caseId === 'general') { + return res.status(400).json({ error: 'Geçersiz caseId' }); + } + // requireAuth zaten bu caseId'nin isteği yapan kullanıcıya ait olduğunu doğruladı. + // Silme işlemi burada (backend'de, service_role ile) yapılıyor — frontend'in anon + // client'ı chat_messages'a RLS nedeniyle yazamıyor/silemiyor olabilir. + const { error } = await supabase.from('chat_messages').delete().eq('case_id', caseId); + if (error) throw error; + res.json({ ok: true }); + } catch (error: any) { + console.error('Delete Chat History Error:', error); + res.status(500).json({ error: error.message || 'Internal Server Error' }); + } +}; + export const chatMessage = async (req: AuthenticatedRequest, res: Response) => { try { const { caseId } = req.params; diff --git a/src/routes/chat.routes.ts b/src/routes/chat.routes.ts index 778f62f..15b02b6 100644 --- a/src/routes/chat.routes.ts +++ b/src/routes/chat.routes.ts @@ -1,9 +1,10 @@ import { Router } from 'express'; import { requireAuth } from '../middleware/auth'; -import { chatMessage } from '../controllers/chat.controller'; +import { chatMessage, deleteChatHistory } from '../controllers/chat.controller'; const router = Router(); router.post('/:caseId/message', requireAuth, chatMessage); +router.delete('/:caseId', requireAuth, deleteChatHistory); export default router; diff --git a/src/routes/index.ts b/src/routes/index.ts index 507cde8..f231cb9 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -6,7 +6,7 @@ import precedentRoutes from './precedent.routes'; const router = Router(); router.get('/health', (req, res) => { - res.json({ status: 'ok', message: 'LegalOS Backend is running' }); + res.json({ status: 'ok', message: 'AyrisLegal Backend is running' }); }); router.use('/documents', documentRoutes);