production-evyos-systems-an.../ServicesFrontEnd/frontend/src/lib/init-sync.ts

59 lines
2.0 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 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<T>(obj: Record<string, T>, chunkSize: number): Record<string, T>[] {
const keys = Object.keys(obj);
const chunks: Record<string, T>[] = [];
for (let i = 0; i < keys.length; i += chunkSize) {
const chunk: Record<string, T> = {};
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.');
}