109 lines
5.5 KiB
JavaScript
109 lines
5.5 KiB
JavaScript
const sdk = require('node-appwrite');
|
|
|
|
const client = new sdk.Client();
|
|
const databases = new sdk.Databases(client);
|
|
|
|
// Use explicit credentials for Almeira Web Cloud (Coolify)
|
|
const ENDPOINT = process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT || 'https://appwrite.almeira.com.co/v1';
|
|
const PROJECT_ID = process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID || '69b84df5003bd8957c5c';
|
|
const API_KEY = process.env.APPWRITE_API_KEY || 'standard_ee81a4ca6784c0fa6a23abab0a983dbba9cd0c2d8e211a00c759d9801f5d8e27b43a605bfa373e13106da87acfc7673608f0ffb0eb04e10cae3dae93d2404bfda8676f96e3fcfd6bd642f6e12d69367916b796c54e98e2e4c4bc0b7a27aa42ba5048582fcbd45b1365b51c5014cb939c1853923c418048ff0e39c2237065da3c';
|
|
|
|
client
|
|
.setEndpoint(ENDPOINT)
|
|
.setProject(PROJECT_ID)
|
|
.setKey(API_KEY);
|
|
|
|
const delay = ms => new Promise(res => setTimeout(res, ms));
|
|
const dbId = 'almeira';
|
|
|
|
async function setupPhase3() {
|
|
try {
|
|
console.log('========================================');
|
|
console.log('Almeira Web — Appwrite Phase 3 Engine');
|
|
console.log('========================================\n');
|
|
|
|
// 1. Updating 'posts' collection
|
|
console.log('[1/5] Actualizando colección: posts...');
|
|
try { await databases.createDatetimeAttribute(dbId, 'posts', 'fecha_finalizacion', false); console.log(' ✓ posts.fecha_finalizacion añadido'); } catch(e){}
|
|
await delay(300);
|
|
try { await databases.createUrlAttribute(dbId, 'posts', 'url_destino', false); console.log(' ✓ posts.url_destino añadido'); } catch(e){}
|
|
await delay(500);
|
|
|
|
// 2. Updating 'contracts' collection
|
|
console.log('\n[2/5] Actualizando colección: contracts...');
|
|
try { await databases.createStringAttribute(dbId, 'contracts', 'eslogan', 255, false); console.log(' ✓ contracts.eslogan añadido'); } catch(e){}
|
|
await delay(500);
|
|
|
|
// 3. Creating 'settings' global collection
|
|
console.log('\n[3/5] Creando colección global: settings...');
|
|
try {
|
|
await databases.createCollection(dbId, 'settings', 'Configuración de UI', [], false);
|
|
console.log(' ✓ Colección settings creada.');
|
|
} catch(e){
|
|
console.log(' > settings: ya existe o saltado.');
|
|
}
|
|
await delay(500);
|
|
try { await databases.createIntegerAttribute(dbId, 'settings', 'ticker_count', true, 0, 50, 10); console.log(' ✓ settings.ticker_count'); } catch(e){}
|
|
await delay(300);
|
|
try { await databases.createIntegerAttribute(dbId, 'settings', 'rotation_seconds', true, 1, 60, 5); console.log(' ✓ settings.rotation_seconds'); } catch(e){}
|
|
await delay(1000);
|
|
|
|
// Seed basic settings
|
|
console.log(' Sembrando variables de entorno Settings...');
|
|
try {
|
|
const list = await databases.listDocuments(dbId, 'settings');
|
|
if (list.total === 0) {
|
|
await databases.createDocument(dbId, 'settings', 'global_config', { ticker_count: 10, rotation_seconds: 5 });
|
|
console.log(' ✓ Datos iniciales "global_config" inyectados.');
|
|
} else {
|
|
console.log(' ✓ Configurador ya poblado.');
|
|
}
|
|
} catch(e) {}
|
|
|
|
// 4. Creating 'metrics' tracking collection
|
|
console.log('\n[4/5] Creando colección analítica: metrics...');
|
|
try {
|
|
await databases.createCollection(dbId, 'metrics', 'Métricas de Publicidad', [], false);
|
|
console.log(' ✓ Colección metrics creada.');
|
|
} catch(e){
|
|
console.log(' > metrics: ya existe.');
|
|
}
|
|
await delay(500);
|
|
try { await databases.createStringAttribute(dbId, 'metrics', 'post_id', 255, true); console.log(' ✓ metrics.post_id'); } catch(e){}
|
|
await delay(300);
|
|
try { await databases.createStringAttribute(dbId, 'metrics', 'tipo_metrica', 50, true); console.log(' ✓ metrics.tipo_metrica (impresion/click)'); } catch(e){}
|
|
await delay(300);
|
|
try { await databases.createDatetimeAttribute(dbId, 'metrics', 'timestamp', true); console.log(' ✓ metrics.timestamp'); } catch(e){}
|
|
await delay(800);
|
|
|
|
// 5. Seed Publication Types (Reseeding just in case DB wipe or failure)
|
|
console.log('\n[5/5] Re-sembrando catálogo: publication_types...');
|
|
const tiposPublicacion = [
|
|
{ id: 't_noticia', codigo: 'NOTICIA', descripcion: 'Noticia' },
|
|
{ id: 't_evento', codigo: 'EVENTO', descripcion: 'Evento' },
|
|
{ id: 't_clasif', codigo: 'CLASIFICADO', descripcion: 'Clasificado' },
|
|
{ id: 't_dbanner', codigo: 'DISPLAY_BANNER', descripcion: 'Display Banner' },
|
|
{ id: 't_dslider', codigo: 'DISPLAY_SLIDER', descripcion: 'Display Slider' },
|
|
];
|
|
for (const t of tiposPublicacion) {
|
|
try {
|
|
// Using hardcoded valid IDs (letters/numbers/underscore) for easy queries later
|
|
await databases.createDocument(dbId, 'publication_types', t.id, { codigo: t.codigo, descripcion: t.descripcion });
|
|
console.log(` ✓ ${t.descripcion}`);
|
|
} catch(e){
|
|
// It might exist already, ignore gracefully
|
|
}
|
|
await delay(200);
|
|
}
|
|
|
|
console.log('\n========================================');
|
|
console.log('✅ Fase 3 Arquitectura Completado con Éxito!');
|
|
console.log('========================================');
|
|
|
|
} catch(e) {
|
|
console.error('\n❌ Error Criptico DB:', e.message);
|
|
}
|
|
}
|
|
|
|
setupPhase3();
|