fix: separate private company documents from shared tender item imports

This commit is contained in:
2026-09-06 09:30:37 +03:00
parent 5e9f17969c
commit ece623d86c
8 changed files with 52 additions and 14 deletions
+8 -2
View File
@@ -746,6 +746,9 @@ const server = http.createServer(async (req, res) => {
try {
const body = await readBody(req, 4 * 1024 * 1024);
if (!body.ikn) return json(res, 400, { success: false, error: 'ikn gerekli' });
const sourceDocument = body.documentId && store.getDocument(user.email,String(body.documentId));
if (!sourceDocument || sourceDocument.tur !== 'ihale' || sourceDocument.ikn !== String(body.ikn))
return json(res,403,{success:false,error:'Kalem aktarımı için bu ihaleye bağlı, size ait ihale dokümanı gerekli. Firma belgeleri ortak havuza aktarılamaz.'});
const { kalemler, yontem } = kalemleriCikar(body.metin || '');
if (!kalemler.length) {
return json(res, 200, { success: true, kalemler: [], yontem, not: 'Metinde teklif cetveli tablosu bulunamadı.' });
@@ -954,6 +957,8 @@ const server = http.createServer(async (req, res) => {
if (!body.ad || !body.icerikBase64) {
return json(res, 400, { success: false, error: 'ad ve icerikBase64 gerekli' });
}
const documentType = body.documentType ?? 'firma';
if (!['firma','ihale'].includes(documentType)) throw new Error('Geçersiz belge türü.');
const metadata = documentMetadataPatch(body);
const buf = decodeDocumentUpload(body.icerikBase64);
const kullanimDurumu = store.documentUsage(user.email);
@@ -967,20 +972,21 @@ const server = http.createServer(async (req, res) => {
let ofisMetni = null;
if (sunucuOkuyabilir(body.ad)) {
try { ofisMetni = arsivMetni(String(body.ad), buf); } catch { /* metin çıkmazsa devam */ }
if (!ikn && body.otoIkn && ofisMetni) ikn = iknCikar(ofisMetni);
if (documentType === 'ihale' && !ikn && body.otoIkn && ofisMetni) ikn = iknCikar(ofisMetni);
}
const id = store.saveDocumentFile(user.email, {
ad: String(body.ad).slice(0, 200),
kategori: metadata.kategori || null,
mime: body.mime || null,
sonKullanma: metadata.sonKullanma || null,
tur: documentType,
ikn, // ihaleye bağlı belge
}, buf);
// Yükleme anında otomatik kalem çıkarımı — AI kullanmaz, kullanıcıdan
// tıklama beklemez. PDF'ler tarayıcıda çözüldüğü için istemci
// metni /api/kalem/cikar'a gönderir.
let kalemBilgi = null;
if (ikn && ofisMetni) {
if (documentType === 'ihale' && ikn && ofisMetni) {
try {
const { kalemler, yontem } = kalemleriCikar(ofisMetni);
if (kalemler.length) {
+8 -4
View File
@@ -97,10 +97,13 @@ export function openDb() {
for (const column of ['ikn', 'son_kullanma']) {
if (!columns.has(column)) db.exec(`ALTER TABLE belgeler ADD COLUMN ${column} TEXT`);
}
if (!columns.has('tur')) db.exec("ALTER TABLE belgeler ADD COLUMN tur TEXT NOT NULL DEFAULT 'firma'");
db.exec('CREATE INDEX IF NOT EXISTS idx_belge_ikn ON belgeler(email, ikn)');
db.exec(`CREATE TABLE IF NOT EXISTS schema_migrations (version TEXT PRIMARY KEY, applied_at TEXT NOT NULL)`);
db.prepare('INSERT OR IGNORE INTO schema_migrations VALUES (?,?)')
.run('20260906_document_columns', new Date().toISOString());
db.prepare('INSERT OR IGNORE INTO schema_migrations VALUES (?,?)')
.run('20260906_document_type', new Date().toISOString());
db.exec('COMMIT');
} catch (error) {
try { db.exec('ROLLBACK'); } catch { /* original error is authoritative */ }
@@ -241,11 +244,12 @@ export function verifyApiKey(ham) {
// --- Belgeler (gerçek dosya, gerçek boyut) ---------------------------------
export function addDocument(email, { ad, kategori, boyut, mime, dosya, sonKullanma, ikn }) {
export function addDocument(email, { ad, kategori, boyut, mime, dosya, sonKullanma, ikn, tur = 'firma' }) {
if (!['firma','ihale'].includes(tur)) throw new Error('Geçersiz belge türü.');
const id = crypto.randomUUID();
withDb((db) => db.prepare(
'INSERT INTO belgeler (id,email,ad,kategori,boyut,mime,dosya,son_kullanma,ikn,olusturma) VALUES (?,?,?,?,?,?,?,?,?,?)'
).run(id, email, ad, kategori || null, boyut, mime || null, dosya, sonKullanma || null, ikn || null, now()));
'INSERT INTO belgeler (id,email,ad,kategori,boyut,mime,dosya,son_kullanma,ikn,olusturma,tur) VALUES (?,?,?,?,?,?,?,?,?,?,?)'
).run(id, email, ad, kategori || null, boyut, mime || null, dosya, sonKullanma || null, ikn || null, now(),tur));
return id;
}
@@ -265,7 +269,7 @@ export function saveDocumentFile(email, metadata, bytes) {
}
export function listDocuments(email, { ikn } = {}) {
const alanlar = 'id, ad, kategori, boyut, mime, son_kullanma, ikn, olusturma, dosya';
const alanlar = 'id, ad, kategori, boyut, mime, son_kullanma, ikn, olusturma, dosya, tur';
return withDb((db) => (ikn
? db.prepare(`SELECT ${alanlar} FROM belgeler WHERE email=? AND ikn=? ORDER BY olusturma DESC`).all(email, ikn)
: db.prepare(`SELECT ${alanlar} FROM belgeler WHERE email=? ORDER BY olusturma DESC`).all(email)