feat: Add precedent caching mechanism via txt and supabase

This commit is contained in:
mstfyldz
2026-08-12 02:17:57 +03:00
parent f7daa3d4c7
commit 437f973d6f
4 changed files with 75 additions and 1 deletions
+56
View File
@@ -1,6 +1,9 @@
import { Request, Response } from 'express';
import { AuthenticatedRequest } from '../middleware/auth';
import { callOllama } from '../lib/aiClient';
import { supabase } from '../lib/supabase';
import fs from 'fs';
import path from 'path';
export const searchPrecedents = async (req: Request, res: Response) => {
try {
@@ -133,3 +136,56 @@ export const getPrecedentDocument = async (req: Request, res: Response) => {
res.status(500).json({ error: error.message || 'Internal Server Error' });
}
};
export const cachePrecedentDocument = async (req: Request, res: Response) => {
try {
const { item, content } = req.body;
if (!item || !item.documentId || !content) {
return res.status(400).json({ error: 'Eksik veri (item veya content)' });
}
const documentId = String(item.documentId);
const dir = path.join(process.cwd(), 'uploads', 'precedents');
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
const filePath = path.join(dir, `${documentId}.txt`);
// 1. Txt Olarak Kaydet (CDN / Sunucu depolama)
if (!fs.existsSync(filePath)) {
fs.writeFileSync(filePath, content, 'utf8');
// 2. Supabase DB'ye Metadata Kaydet (Lazy Caching & Meilisearch Indexing)
try {
const { error } = await supabase
.from('cached_precedents')
.upsert({
document_id: documentId,
mahkeme: item.mahkeme || '',
karar_no: item.karar_no || '',
esas_no: item.esas_no || '',
tarih: item.tarih || '',
konu: item.konu || '',
ozet: item.ozet || '',
created_at: new Date().toISOString()
}, { onConflict: 'document_id' });
if (error) {
console.error('[Cache] Supabase kayıt hatası:', error.message);
} else {
console.log(`[Cache] Başarıyla kaydedildi: ${documentId}`);
}
} catch (dbErr) {
console.error('[Cache] Supabase try-catch hatası:', dbErr);
}
}
res.json({ success: true, message: 'Ön belleğe alındı' });
} catch (error: any) {
console.error('Cache Precedent Document Error:', error);
res.status(500).json({ error: error.message || 'Internal Server Error' });
}
};