Files
lagos-back/src/controllers/case.controller.ts
T
mstfyldz 0e40aaef96 feat: add case creation endpoint and auto-title from first message
POST /api/cases lets the frontend actually create a new dava dosyası
(no working "Yeni Dosya" flow existed before). chatMessage now
generates a short title from a case's first message (like Claude/
ChatGPT auto-titling) and renames the case, returned as `newTitle`.
2026-08-09 10:34:55 +03:00

42 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Response } from 'express';
import { supabase } from '../lib/supabase';
import { AuthenticatedRequest } from '../middleware/auth';
export const createCase = async (req: AuthenticatedRequest, res: Response) => {
try {
const userId = req.user?.id;
const title = String(req.body?.title || 'Yeni Sohbet').slice(0, 200);
// requireAuth doğrudan kullanıcının kendi id'sini set ediyor; başkasının hesabına
// dosya oluşturulamaz. Yazma frontend'in anon client'ı yerine burada (service_role
// ile) yapılıyor, cases için de RLS engeli yaşanmasın diye.
const { data, error } = await supabase
.from('cases')
.insert([{ title, user_id: userId }])
.select('id, title')
.single();
if (error) throw error;
res.json({ case: data });
} catch (error: any) {
console.error('Create Case Error:', error);
res.status(500).json({ error: error.message || 'Internal Server Error' });
}
};
export const deleteCase = async (req: AuthenticatedRequest, res: Response) => {
try {
const { caseId } = req.params;
if (!caseId) {
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ı.
// documents/analyses/chat_messages tabloları case_id üzerinden ON DELETE CASCADE
// ile tanımlı (bkz. sql/1.sql), yani cases satırını silmek hepsini birlikte siliyor.
const { error } = await supabase.from('cases').delete().eq('id', caseId);
if (error) throw error;
res.json({ ok: true });
} catch (error: any) {
console.error('Delete Case Error:', error);
res.status(500).json({ error: error.message || 'Internal Server Error' });
}
};