Files
Filamenteka/import-data.js
DaX 82c476430f Fix API connectivity and import filament data from PDF
- Update all environment files to use new PostgreSQL API endpoint
- Fix CORS configuration in API server
- Import 35 filaments and 29 colors from PDF data
- Fix TypeScript type error in dashboard
- Add back emoji icons for dark mode toggle
- Remove debugging code and test buttons
- Clean up error handling
2025-06-20 15:40:40 +02:00

141 lines
3.8 KiB
JavaScript

#!/usr/bin/env node
const fs = require('fs');
const https = require('https');
// Read the data file
const data = JSON.parse(fs.readFileSync('./data.json', 'utf8'));
const uniqueColors = JSON.parse(fs.readFileSync('./unique_colors.json', 'utf8'));
const API_URL = 'https://api.filamenteka.rs/api';
// First, get auth token
async function getAuthToken() {
return new Promise((resolve, reject) => {
const postData = JSON.stringify({
username: process.env.ADMIN_USERNAME || 'admin',
password: process.env.ADMIN_PASSWORD || 'admin'
});
const options = {
hostname: 'api.filamenteka.rs',
path: '/api/login',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': postData.length
}
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => data += chunk);
res.on('end', () => {
try {
const response = JSON.parse(data);
resolve(response.token);
} catch (err) {
reject(err);
}
});
});
req.on('error', reject);
req.write(postData);
req.end();
});
}
// Make authenticated request
async function makeRequest(method, path, data, token) {
return new Promise((resolve, reject) => {
const postData = data ? JSON.stringify(data) : '';
const options = {
hostname: 'api.filamenteka.rs',
path: `/api${path}`,
method: method,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
}
};
if (postData) {
options.headers['Content-Length'] = postData.length;
}
const req = https.request(options, (res) => {
let responseData = '';
res.on('data', (chunk) => responseData += chunk);
res.on('end', () => {
if (res.statusCode >= 200 && res.statusCode < 300) {
try {
resolve(responseData ? JSON.parse(responseData) : null);
} catch {
resolve(responseData);
}
} else {
reject(new Error(`HTTP ${res.statusCode}: ${responseData}`));
}
});
});
req.on('error', reject);
if (postData) req.write(postData);
req.end();
});
}
async function importData() {
try {
console.log('Getting auth token...');
const token = await getAuthToken();
console.log('Authentication successful!');
// Import colors first
console.log('\nImporting colors...');
for (const color of uniqueColors) {
try {
await makeRequest('POST', '/colors', {
name: color.name,
hex: color.hex
}, token);
console.log(`✓ Added color: ${color.name}`);
} catch (err) {
if (err.message.includes('already exists')) {
console.log(`⚠ Color already exists: ${color.name}`);
} else {
console.error(`✗ Failed to add color ${color.name}:`, err.message);
}
}
}
// Import filaments
console.log('\nImporting filaments...');
for (const filament of data) {
try {
await makeRequest('POST', '/filaments', {
brand: filament.brand,
tip: filament.tip,
finish: filament.finish,
boja: filament.boja,
bojaHex: filament.boja_hex,
refill: filament.refill,
vakum: filament.vakum,
otvoreno: filament.otvoreno,
kolicina: filament.kolicina.toString(),
cena: filament.cena.toString()
}, token);
console.log(`✓ Added filament: ${filament.brand} ${filament.tip} ${filament.finish} - ${filament.boja}`);
} catch (err) {
console.error(`✗ Failed to add filament:`, err.message);
}
}
console.log('\nData import completed!');
} catch (err) {
console.error('Import failed:', err);
}
}
importData();