import fs from 'fs'; import path from 'path'; import { employeeMapper } from '@/pages/office/mapper'; import { occupantMapper } from '@/pages/venue/mapper'; import { Page } from '@/pages/types/page'; function chunkifyObject(obj: Record, chunkSize: number): Record[] { const keys = Object.keys(obj); const chunks: Record[] = []; for (let i = 0; i < keys.length; i += chunkSize) { const chunk: Record = {}; keys.slice(i, i + chunkSize).forEach((key) => { chunk[key] = obj[key]; }); chunks.push(chunk); } return chunks; } export async function sendChunksToNest() { const sendObject: Page = { ...employeeMapper, ...occupantMapper }; const lockPath = path.join(__dirname, 'sync.lock'); if (fs.existsSync(lockPath)) { console.log('🔁 Zaten sync edilmiş, işlem atlandı.'); return; } const chunks = chunkifyObject(sendObject, 50); const totalChunks = chunks.length; for (let i = 0; i < totalChunks; i++) { const chunk = chunks[i]; // 👇 Bu şekilde index bilgisiyle beraber nested body oluşturuyoruz const payload = { chunkIndex: i + 1, chunkCount: totalChunks, data: chunk, }; try { const response = await fetch('http://localhost:8001/navigator/page/configure', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload), }); const result = await response.json(); console.log(`✅ [${payload.chunkIndex}/${payload.chunkCount}] Chunk gönderildi. Kayıt sayısı: ${result.count}`); } catch (err) { console.error(`❌ [${i + 1}/${totalChunks}] Chunk gönderimi sırasında hata:`, err); } } fs.writeFileSync(lockPath, 'done'); console.log('🎉 Tüm chunklar gönderildi ve lock dosyası oluşturuldu.'); }