feat: Add precedent caching mechanism via txt and supabase
This commit is contained in:
Generated
+16
@@ -16,6 +16,7 @@
|
||||
"express-rate-limit": "^8.6.2",
|
||||
"heic-convert": "^2.1.0",
|
||||
"mammoth": "^1.12.0",
|
||||
"mkdirp": "^3.0.1",
|
||||
"multer": "^2.2.0",
|
||||
"officeparser": "^7.5.1",
|
||||
"openai": "^7.4.0",
|
||||
@@ -2205,6 +2206,21 @@
|
||||
"url": "https://opencollective.com/express"
|
||||
}
|
||||
},
|
||||
"node_modules/mkdirp": {
|
||||
"version": "3.0.1",
|
||||
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-3.0.1.tgz",
|
||||
"integrity": "sha512-+NsyUUAZDmo6YVHzL/stxSu3t9YS1iljliy3BSDrXJ/dkn1KYdmtZODGGjLcc9XLgVVpH4KshHB8XmZgMhaBXg==",
|
||||
"license": "MIT",
|
||||
"bin": {
|
||||
"mkdirp": "dist/cjs/src/bin.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/isaacs"
|
||||
}
|
||||
},
|
||||
"node_modules/ms": {
|
||||
"version": "2.1.3",
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
"express-rate-limit": "^8.6.2",
|
||||
"heic-convert": "^2.1.0",
|
||||
"mammoth": "^1.12.0",
|
||||
"mkdirp": "^3.0.1",
|
||||
"multer": "^2.2.0",
|
||||
"officeparser": "^7.5.1",
|
||||
"openai": "^7.4.0",
|
||||
|
||||
@@ -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' });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { Router } from 'express';
|
||||
import { requireAuth } from '../middleware/auth';
|
||||
import { searchPrecedents, getPrecedentDocument, analyzePrecedents } from '../controllers/precedent.controller';
|
||||
import { searchPrecedents, getPrecedentDocument, analyzePrecedents, cachePrecedentDocument } from '../controllers/precedent.controller';
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post('/search-precedents', searchPrecedents);
|
||||
router.get('/precedent-document/:documentId', getPrecedentDocument);
|
||||
router.post('/precedent-analysis', requireAuth, analyzePrecedents);
|
||||
router.post('/cache-precedent', cachePrecedentDocument);
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user