almeira-web/setup_sneakers_schema.js

211 lines
12 KiB
JavaScript

const { Client, Databases, Permission, Role } = require('./node_modules/node-appwrite');
const client = new Client()
.setEndpoint('https://appwrite.almeira.com.co/v1')
.setProject('69b84df5003bd8957c5c')
.setKey('standard_cce3cdb866ea6433888d40cedd24dc45987372308fdc3dd0697de08648c0e5c7e04b02c3bcee271403fd656b00a5820f04c08b51631284a6e8a3582d8aed2aa33234f3c29cc777e9b162eb54810b789251b0d006a9a3b081ebc2da53b9baa94614b124b3508ad849fd9a4187cc6094f506d4b57f409b69d9562a74df594f3ec3');
const dbs = new Databases(client);
const DB_ID = 'sneakers_multistore_db';
async function createAttributeSafely(colId, type, key, sizeOrParams = {}) {
try {
if (type === 'string') {
const size = sizeOrParams.size || 1000;
const req = sizeOrParams.required || false;
const def = sizeOrParams.default || null;
const isArray = sizeOrParams.array || false;
await dbs.createStringAttribute(DB_ID, colId, key, size, req, def, isArray);
} else if (type === 'integer') {
const req = sizeOrParams.required || false;
const min = sizeOrParams.min ?? -999999999;
const max = sizeOrParams.max ?? 999999999;
const def = sizeOrParams.default ?? null;
const isArray = sizeOrParams.array || false;
await dbs.createIntegerAttribute(DB_ID, colId, key, req, min, max, def, isArray);
} else if (type === 'boolean') {
const req = sizeOrParams.required || false;
const def = sizeOrParams.default ?? null;
const isArray = sizeOrParams.array || false;
await dbs.createBooleanAttribute(DB_ID, colId, key, req, def, isArray);
}
console.log(` + Created attr ${key} on ${colId}`);
} catch (e) {
if (e.message.includes('already exists')) {
console.log(` . Attr ${key} already exists on ${colId}`);
} else {
console.warn(` ! Error creating attr ${key} on ${colId}:`, e.message);
}
}
}
async function main() {
console.log('--- Setting up collections in ' + DB_ID + ' ---');
const collections = [
{ id: 'cities', name: 'Ciudades y Zonas' },
{ id: 'payment_methods', name: 'Métodos de Pago' },
{ id: 'orders', name: 'Pedidos' },
{ id: 'products', name: 'Productos' },
{ id: 'suppliers', name: 'Proveedores' },
{ id: 'store_settings', name: 'Configuración Tienda' },
{ id: 'monitored_groups', name: 'Grupos WhatsApp' },
{ id: 'supplier_posts', name: 'Feed WhatsApp' },
{ id: 'users', name: 'Usuarios' },
{ id: 'message_templates', name: 'Plantillas Mensajes' },
];
for (const c of collections) {
try {
await dbs.getCollection(DB_ID, c.id);
console.log(`Collection ${c.id} exists.`);
} catch {
console.log(`Creating collection ${c.id}...`);
await dbs.createCollection(DB_ID, c.id, c.name, [
Permission.read(Role.any()),
Permission.create(Role.any()),
Permission.update(Role.any()),
Permission.delete(Role.any()),
]);
}
}
// 1. Cities
console.log('\nAttributes for cities:');
await createAttributeSafely('cities', 'string', 'name', { size: 255 });
await createAttributeSafely('cities', 'string', 'department', { size: 255 });
await createAttributeSafely('cities', 'integer', 'shippingCost', {});
await createAttributeSafely('cities', 'string', 'deliveryTime', { size: 255 });
await createAttributeSafely('cities', 'boolean', 'allowsContraentrega', {});
await createAttributeSafely('cities', 'boolean', 'firstExchangeFree', {});
await createAttributeSafely('cities', 'string', 'exchangePolicyNote', { size: 1000 });
await createAttributeSafely('cities', 'boolean', 'isActive', {});
// 2. Payment Methods
console.log('\nAttributes for payment_methods:');
await createAttributeSafely('payment_methods', 'string', 'name', { size: 255 });
await createAttributeSafely('payment_methods', 'string', 'type', { size: 100 });
await createAttributeSafely('payment_methods', 'string', 'description', { size: 2000 });
await createAttributeSafely('payment_methods', 'boolean', 'requiresDeposit', {});
await createAttributeSafely('payment_methods', 'integer', 'depositAmount', {});
await createAttributeSafely('payment_methods', 'string', 'accountInfo', { size: 1000 });
await createAttributeSafely('payment_methods', 'string', 'boldPaymentLink', { size: 1000 });
await createAttributeSafely('payment_methods', 'string', 'boldQrUrl', { size: 1000 });
await createAttributeSafely('payment_methods', 'boolean', 'isActive', {});
// 3. Orders
console.log('\nAttributes for orders:');
await createAttributeSafely('orders', 'string', 'customerName', { size: 255 });
await createAttributeSafely('orders', 'string', 'customerPhone', { size: 100 });
await createAttributeSafely('orders', 'string', 'address', { size: 1000 });
await createAttributeSafely('orders', 'string', 'cityId', { size: 100 });
await createAttributeSafely('orders', 'string', 'cityName', { size: 255 });
await createAttributeSafely('orders', 'string', 'items', { size: 65535 }); // JSON array of items
await createAttributeSafely('orders', 'integer', 'subtotal', {});
await createAttributeSafely('orders', 'integer', 'shippingCost', {});
await createAttributeSafely('orders', 'integer', 'totalAmount', {});
await createAttributeSafely('orders', 'integer', 'depositAmount', {});
await createAttributeSafely('orders', 'integer', 'balanceDue', {});
await createAttributeSafely('orders', 'string', 'paymentMethodId', { size: 100 });
await createAttributeSafely('orders', 'string', 'paymentMethodName', { size: 255 });
await createAttributeSafely('orders', 'string', 'status', { size: 100 });
await createAttributeSafely('orders', 'string', 'notes', { size: 2000 });
await createAttributeSafely('orders', 'string', 'createdAt', { size: 100 });
// 4. Products
console.log('\nAttributes for products:');
await createAttributeSafely('products', 'string', 'name', { size: 255 });
await createAttributeSafely('products', 'string', 'modelName', { size: 255 });
await createAttributeSafely('products', 'string', 'brand', { size: 100 });
await createAttributeSafely('products', 'string', 'colorName', { size: 100 });
await createAttributeSafely('products', 'integer', 'supplierPrice', {});
await createAttributeSafely('products', 'integer', 'retailPrice', {});
await createAttributeSafely('products', 'integer', 'originalPrice', {});
await createAttributeSafely('products', 'string', 'sizes', { size: 65535 }); // JSON or string
await createAttributeSafely('products', 'string', 'images', { size: 65535 }); // JSON
await createAttributeSafely('products', 'string', 'colors', { size: 65535 }); // JSON
await createAttributeSafely('products', 'string', 'description', { size: 10000 });
await createAttributeSafely('products', 'string', 'category', { size: 100 });
await createAttributeSafely('products', 'boolean', 'inStock', {});
await createAttributeSafely('products', 'boolean', 'isActive', {});
await createAttributeSafely('products', 'string', 'supplierName', { size: 255 });
await createAttributeSafely('products', 'string', 'supplierPhone', { size: 100 });
// 5. Suppliers
console.log('\nAttributes for suppliers:');
await createAttributeSafely('suppliers', 'string', 'name', { size: 255 });
await createAttributeSafely('suppliers', 'string', 'phone', { size: 100 });
await createAttributeSafely('suppliers', 'string', 'contactPerson', { size: 255 });
await createAttributeSafely('suppliers', 'string', 'address', { size: 500 });
await createAttributeSafely('suppliers', 'string', 'notes', { size: 2000 });
await createAttributeSafely('suppliers', 'boolean', 'isActive', {});
// 6. Store Settings
console.log('\nAttributes for store_settings:');
await createAttributeSafely('store_settings', 'string', 'storeName', { size: 255 });
await createAttributeSafely('store_settings', 'string', 'slogan', { size: 255 });
await createAttributeSafely('store_settings', 'string', 'email', { size: 255 });
await createAttributeSafely('store_settings', 'string', 'phone', { size: 100 });
await createAttributeSafely('store_settings', 'string', 'whatsappNumber', { size: 100 });
await createAttributeSafely('store_settings', 'string', 'instagramHandle', { size: 100 });
await createAttributeSafely('store_settings', 'string', 'address', { size: 500 });
await createAttributeSafely('store_settings', 'string', 'city', { size: 255 });
await createAttributeSafely('store_settings', 'string', 'nequiAccount', { size: 100 });
await createAttributeSafely('store_settings', 'string', 'daviplataAccount', { size: 100 });
await createAttributeSafely('store_settings', 'string', 'accountHolder', { size: 255 });
await createAttributeSafely('store_settings', 'string', 'boldPaymentLink', { size: 1000 });
await createAttributeSafely('store_settings', 'string', 'boldApiKey', { size: 1000 });
await createAttributeSafely('store_settings', 'string', 'boldMerchantId', { size: 255 });
await createAttributeSafely('store_settings', 'string', 'boldQrUrl', { size: 1000 });
await createAttributeSafely('store_settings', 'boolean', 'boldEnabled', {});
await createAttributeSafely('store_settings', 'integer', 'defaultProfitMargin', {});
await createAttributeSafely('store_settings', 'integer', 'defaultDepositAmount', {});
// 7. Monitored Groups
console.log('\nAttributes for monitored_groups:');
await createAttributeSafely('monitored_groups', 'string', 'jid', { size: 255 });
await createAttributeSafely('monitored_groups', 'string', 'name', { size: 255 });
await createAttributeSafely('monitored_groups', 'boolean', 'isMonitored', {});
await createAttributeSafely('monitored_groups', 'boolean', 'autoPublish', {});
await createAttributeSafely('monitored_groups', 'string', 'notes', { size: 1000 });
// 8. Supplier Posts (Feed WhatsApp)
console.log('\nAttributes for supplier_posts:');
await createAttributeSafely('supplier_posts', 'string', 'channelName', { size: 255 });
await createAttributeSafely('supplier_posts', 'string', 'channelJid', { size: 255 });
await createAttributeSafely('supplier_posts', 'string', 'supplierName', { size: 255 });
await createAttributeSafely('supplier_posts', 'string', 'supplierPhone', { size: 100 });
await createAttributeSafely('supplier_posts', 'string', 'rawMessage', { size: 10000 });
await createAttributeSafely('supplier_posts', 'string', 'rawImages', { size: 65535 }); // JSON array
await createAttributeSafely('supplier_posts', 'string', 'extractedProduct', { size: 65535 }); // JSON
await createAttributeSafely('supplier_posts', 'string', 'status', { size: 50 });
await createAttributeSafely('supplier_posts', 'string', 'receivedAt', { size: 100 });
// 9. Users
console.log('\nAttributes for users:');
await createAttributeSafely('users', 'string', 'name', { size: 255 });
await createAttributeSafely('users', 'string', 'email', { size: 255 });
await createAttributeSafely('users', 'string', 'phone', { size: 100 });
await createAttributeSafely('users', 'string', 'city', { size: 255 });
await createAttributeSafely('users', 'string', 'address', { size: 500 });
await createAttributeSafely('users', 'string', 'role', { size: 50 });
await createAttributeSafely('users', 'boolean', 'isActive', {});
await createAttributeSafely('users', 'string', 'createdAt', { size: 100 });
// 10. Message Templates
console.log('\nAttributes for message_templates:');
await createAttributeSafely('message_templates', 'string', 'title', { size: 255 });
await createAttributeSafely('message_templates', 'string', 'description', { size: 1000 });
await createAttributeSafely('message_templates', 'string', 'category', { size: 100 });
await createAttributeSafely('message_templates', 'string', 'channel', { size: 50 });
await createAttributeSafely('message_templates', 'string', 'content', { size: 10000 });
await createAttributeSafely('message_templates', 'string', 'defaultContent', { size: 10000 });
await createAttributeSafely('message_templates', 'string', 'availableVariables', { size: 65535 }); // JSON
await createAttributeSafely('message_templates', 'boolean', 'isActive', {});
await createAttributeSafely('message_templates', 'string', 'updatedAt', { size: 100 });
console.log('\n✅ All collections and attributes processed!');
}
main().catch(console.error);