107 lines
4.0 KiB
JavaScript
107 lines
4.0 KiB
JavaScript
const fs = require('fs');
|
|
if (fs.existsSync('.env.local')) {
|
|
const envConfig = fs.readFileSync('.env.local').toString();
|
|
envConfig.split('\n').forEach(line => {
|
|
const match = line.match(/^([^=]+)=(.*)$/);
|
|
if (match) process.env[match[1]] = match[2];
|
|
});
|
|
}
|
|
const sdk = require('node-appwrite');
|
|
|
|
const client = new sdk.Client();
|
|
const databases = new sdk.Databases(client);
|
|
|
|
const ENDPOINT = process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT || 'https://appwrite.almeira.com.co/v1';
|
|
const PROJECT_ID = process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID || '69bedced002971103d13';
|
|
const API_KEY = process.env.APPWRITE_API_KEY;
|
|
|
|
if (!API_KEY) {
|
|
console.error("APPWRITE_API_KEY is missing from .env.local");
|
|
process.exit(1);
|
|
}
|
|
|
|
client.setEndpoint(ENDPOINT).setProject(PROJECT_ID).setKey(API_KEY);
|
|
|
|
const dbId = 'almeira';
|
|
const colId = 'entidad';
|
|
|
|
async function setup() {
|
|
try {
|
|
console.log(`Buscando/Creando colección ${colId}...`);
|
|
try {
|
|
await databases.getCollection(dbId, colId);
|
|
console.log("La colección ya existe.");
|
|
} catch (e) {
|
|
if (e.code === 404) {
|
|
await databases.createCollection(dbId, colId, 'Configuración de Entidad', [
|
|
sdk.Permission.read(sdk.Role.any()),
|
|
sdk.Permission.create(sdk.Role.users()),
|
|
sdk.Permission.update(sdk.Role.users()),
|
|
sdk.Permission.delete(sdk.Role.users())
|
|
]);
|
|
console.log("Colección creada.");
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
const stringAttrs = [
|
|
{ key: 'logotipo', size: 2048, req: false },
|
|
{ key: 'razon_social', size: 255, req: true },
|
|
{ key: 'direccion', size: 255, req: false },
|
|
{ key: 'numero_contacto', size: 50, req: false },
|
|
{ key: 'numero_whatsapp', size: 50, req: false },
|
|
{ key: 'ciudad', size: 100, req: false },
|
|
{ key: 'sitio_web', size: 2048, req: false },
|
|
{ key: 'email', size: 255, req: false },
|
|
{ key: 'facebook_url', size: 2048, req: false },
|
|
{ key: 'youtube_url', size: 2048, req: false },
|
|
{ key: 'instagram_url', size: 2048, req: false },
|
|
{ key: 'x_url', size: 2048, req: false },
|
|
{ key: 'telegram_url', size: 2048, req: false },
|
|
];
|
|
|
|
console.log("Creando atributos String...");
|
|
for (let attr of stringAttrs) {
|
|
try {
|
|
await databases.createStringAttribute(dbId, colId, attr.key, attr.size, attr.req);
|
|
console.log(`- ${attr.key} creado`);
|
|
} catch (e) {
|
|
if(e.code !== 409) console.error(`Error en ${attr.key}:`, e.message);
|
|
}
|
|
}
|
|
|
|
try {
|
|
await databases.createBooleanAttribute(dbId, colId, 'mostrar_texto_logo', false, false);
|
|
console.log("- mostrar_texto_logo creado");
|
|
} catch (e) {
|
|
if (e.code !== 409) console.error(e.message);
|
|
}
|
|
|
|
console.log("Atributos creados exitosamente. Espera unos segundos a que Appwrite los indexe.");
|
|
|
|
// Crear documento por defecto si no existe
|
|
setTimeout(async () => {
|
|
try {
|
|
const docs = await databases.listDocuments(dbId, colId);
|
|
if (docs.total === 0) {
|
|
await databases.createDocument(dbId, colId, 'default', {
|
|
razon_social: 'Almeira Producciones',
|
|
mostrar_texto_logo: true
|
|
});
|
|
console.log("Documento por defecto creado.");
|
|
} else {
|
|
console.log("Ya existe un documento de configuración.");
|
|
}
|
|
} catch(e) {
|
|
console.log("No se pudo crear el documento por defecto (A veces la BD demora en activarse):", e.message);
|
|
}
|
|
}, 5000);
|
|
|
|
} catch (error) {
|
|
console.error("Error general:", error.message);
|
|
}
|
|
}
|
|
|
|
setup();
|