96 lines
3.7 KiB
JavaScript
96 lines
3.7 KiB
JavaScript
const sdk = require('node-appwrite');
|
|
require('dotenv').config({ path: '.env.local' });
|
|
|
|
const client = new sdk.Client()
|
|
.setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT)
|
|
.setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID)
|
|
.setKey(process.env.APPWRITE_API_KEY);
|
|
|
|
const databases = new sdk.Databases(client);
|
|
const DB_ID = process.env.APPWRITE_DATABASE_ID || 'almeira';
|
|
const COLLECTION_ID = 'whatsapp_chats';
|
|
|
|
const delay = ms => new Promise(res => setTimeout(res, ms));
|
|
|
|
async function setupWhatsAppChatsCollection() {
|
|
try {
|
|
console.log("Creando o verificando colección 'whatsapp_chats' en Appwrite...");
|
|
|
|
let collectionExists = false;
|
|
try {
|
|
await databases.getCollection(DB_ID, COLLECTION_ID);
|
|
collectionExists = true;
|
|
console.log("✓ Colección 'whatsapp_chats' ya existe.");
|
|
} catch (e) {
|
|
// Create collection
|
|
await databases.createCollection(
|
|
DB_ID,
|
|
COLLECTION_ID,
|
|
'Chats Autorizados WhatsApp',
|
|
[
|
|
sdk.Permission.read(sdk.Role.any()),
|
|
sdk.Permission.write(sdk.Role.any())
|
|
],
|
|
false
|
|
);
|
|
console.log("✓ Colección 'whatsapp_chats' creada exitosamente.");
|
|
}
|
|
|
|
await delay(1000);
|
|
|
|
// Attributes
|
|
const attributes = [
|
|
{ name: 'name', type: 'string', size: 255, required: true },
|
|
{ name: 'jid', type: 'string', size: 255, required: false },
|
|
{ name: 'enabled', type: 'boolean', required: true, default: true },
|
|
{ name: 'notes', type: 'string', size: 1000, required: false }
|
|
];
|
|
|
|
for (const attr of attributes) {
|
|
try {
|
|
if (attr.type === 'string') {
|
|
await databases.createStringAttribute(DB_ID, COLLECTION_ID, attr.name, attr.size, attr.required, undefined, false);
|
|
} else if (attr.type === 'boolean') {
|
|
await databases.createBooleanAttribute(DB_ID, COLLECTION_ID, attr.name, attr.required, attr.default, false);
|
|
}
|
|
console.log(`✓ Atributo '${attr.name}' creado.`);
|
|
await delay(600);
|
|
} catch (err) {
|
|
console.log(`> Atributo '${attr.name}' ya existe.`);
|
|
}
|
|
}
|
|
|
|
// Wait a bit for Appwrite schema to be ready
|
|
console.log("Esperando que Appwrite procese los atributos...");
|
|
await delay(2500);
|
|
|
|
// Seed initial documents if empty
|
|
const existingDocs = await databases.listDocuments(DB_ID, COLLECTION_ID);
|
|
if (existingDocs.total === 0) {
|
|
console.log("Poblando chats iniciales autorizados...");
|
|
await databases.createDocument(DB_ID, COLLECTION_ID, sdk.ID.unique(), {
|
|
name: 'Publicaciones Almeira',
|
|
jid: '120363419329355281@g.us',
|
|
enabled: true,
|
|
notes: 'Grupo oficial de publicaciones de Almeira'
|
|
});
|
|
|
|
await databases.createDocument(DB_ID, COLLECTION_ID, sdk.ID.unique(), {
|
|
name: 'Medios y Prensa Gob Cauca',
|
|
jid: '573134848203-1408546044@g.us',
|
|
enabled: true,
|
|
notes: 'Boletines de prensa y noticias de la Gobernación del Cauca'
|
|
});
|
|
console.log("✓ Documentos iniciales creados.");
|
|
} else {
|
|
console.log(`✓ Colección ya cuenta con ${existingDocs.total} chats registrados.`);
|
|
}
|
|
|
|
console.log("¡Todo listo en Appwrite!");
|
|
} catch (e) {
|
|
console.error("Error en setup:", e.message);
|
|
}
|
|
}
|
|
|
|
setupWhatsAppChatsCollection();
|