UNPKG

n8n-nodes-wuzapi

Version:

n8n community nodes for Wuzapi - WhatsApp Multi-Device REST API

598 lines 30.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WuzapiAdmin = void 0; class WuzapiAdmin { constructor() { this.description = { displayName: 'Wuzapi Admin', name: 'wuzapiAdmin', icon: 'file:wuzapi.svg', group: ['transform'], version: 1, subtitle: '={{$parameter["operation"]}}', description: 'Administrative operations for Wuzapi WhatsApp API', defaults: { name: 'Wuzapi Admin', }, inputs: ["main" /* NodeConnectionType.Main */], outputs: ["main" /* NodeConnectionType.Main */], credentials: [ { name: 'wuzapiAdminApi', required: true, }, ], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, options: [ { name: 'Create User', value: 'createUser', description: 'Create a new user', action: 'Create user', }, { name: 'Delete User', value: 'deleteUser', description: 'Delete a user from database', action: 'Delete user', }, { name: 'Delete User Full', value: 'deleteUserFull', description: 'Delete user completely (DB, S3, logout)', action: 'Delete user completely', }, { name: 'List Users', value: 'listUsers', description: 'List all users', action: 'List users', }, // Global System Management { name: 'Get Global Stats', value: 'getGlobalStats', description: 'Get global system statistics', action: 'Get global statistics', }, { name: 'Test Global Systems', value: 'testGlobalSystems', description: 'Test global webhook and RabbitMQ systems', action: 'Test global systems', }, { name: 'Reload Global Config', value: 'reloadGlobalConfig', description: 'Reload global configurations from environment', action: 'Reload global config', }, { name: 'Send Global Test Event', value: 'sendGlobalTestEvent', description: 'Send test event through global systems', action: 'Send global test event', }, { name: 'Get Global Configuration', value: 'getGlobalConfiguration', description: 'Get current global configuration and env vars', action: 'Get global configuration', }, ], default: 'listUsers', }, // Create User { displayName: 'User Name', name: 'userName', type: 'string', default: '', placeholder: 'John Doe', description: 'Name of the user', required: true, displayOptions: { show: { operation: ['createUser'], }, }, }, { displayName: 'User Token', name: 'userToken', type: 'string', typeOptions: { password: true }, default: '', placeholder: 'e.g. Z1234ABCCXD', description: 'Authentication token for the user', required: true, displayOptions: { show: { operation: ['createUser'], }, }, }, { displayName: 'Additional Options', name: 'additionalOptions', type: 'collection', placeholder: 'Add Option', default: {}, displayOptions: { show: { operation: ['createUser'], }, }, options: [ { displayName: 'Webhook URL', name: 'webhook', type: 'string', default: '', placeholder: 'https://webhook.site/1234567890', description: 'Webhook URL to receive events', }, { displayName: 'Events', name: 'events', type: 'string', default: 'All', placeholder: 'e.g. All, Message, Presence', description: 'Events to subscribe to', }, { displayName: 'Proxy Configuration', name: 'proxyConfig', type: 'fixedCollection', default: {}, description: 'Proxy settings for the user', options: [ { name: 'proxy', displayName: 'Proxy', values: [ { displayName: 'Enabled', name: 'enabled', type: 'boolean', default: false, description: 'Whether to enable proxy', }, { displayName: 'Proxy URL', name: 'proxyURL', type: 'string', default: '', placeholder: 'socks5://user:pass@host:port', }, ], }, ], }, { displayName: 'S3 Configuration', name: 's3Config', type: 'fixedCollection', default: {}, description: 'S3 storage settings for the user', options: [ { name: 's3', displayName: 'S3', values: [ { displayName: 'Access Key', name: 'accessKey', type: 'string', default: '', description: 'S3 access key ID', }, { displayName: 'Bucket', name: 'bucket', type: 'string', default: '', description: 'S3 bucket name', }, { displayName: 'Enabled', name: 'enabled', type: 'boolean', default: false, description: 'Whether to enable S3', }, { displayName: 'Endpoint', name: 'endpoint', type: 'string', default: 'https://s3.amazonaws.com', description: 'S3 endpoint URL', }, { displayName: 'Media Delivery', name: 'mediaDelivery', type: 'options', default: 'both', options: [ { name: 'Base64', value: 'base64', }, { name: 'S3', value: 's3', }, { name: 'Both', value: 'both', }, ], description: 'Media delivery method', }, { displayName: 'Path Style', name: 'pathStyle', type: 'boolean', default: false, description: 'Whether to use path style URLs', }, { displayName: 'Public URL', name: 'publicURL', type: 'string', default: '', description: 'Public URL for accessing files', }, { displayName: 'Region', name: 'region', type: 'string', default: 'us-east-1', description: 'S3 region', }, { displayName: 'Retention Days', name: 'retentionDays', type: 'number', default: 30, description: 'Days to retain files', }, { displayName: 'Secret Key', name: 'secretKey', type: 'string', typeOptions: { password: true }, default: '', description: 'S3 secret access key', }, ], }, ], }, { displayName: 'RabbitMQ Configuration', name: 'rabbitmqConfig', type: 'fixedCollection', default: {}, description: 'RabbitMQ configuration for the user', options: [ { name: 'rabbitmq', displayName: 'RabbitMQ', values: [ { displayName: 'Auto Delete', name: 'auto_delete', type: 'boolean', default: false, description: 'Whether to auto-delete when unused', }, { displayName: 'Delivery Mode', name: 'delivery_mode', type: 'options', default: 2, options: [ { name: 'Non-Persistent (1)', value: 1, }, { name: 'Persistent (2)', value: 2, }, ], description: 'Message delivery mode', }, { displayName: 'Durable', name: 'durable', type: 'boolean', default: true, description: 'Whether exchange and queue should be durable', }, { displayName: 'Enabled', name: 'enabled', type: 'boolean', default: false, description: 'Whether to enable RabbitMQ', }, { displayName: 'Events', name: 'events', type: 'string', default: 'All', placeholder: 'e.g. All, Message, Presence', description: 'Events to subscribe to', }, { displayName: 'Exchange', name: 'exchange', type: 'string', default: '', placeholder: 'e.g. wuzapi.events', description: 'RabbitMQ exchange name', }, { displayName: 'Exclusive', name: 'exclusive', type: 'boolean', default: false, description: 'Whether queue should be exclusive', }, { displayName: 'No Wait', name: 'no_wait', type: 'boolean', default: false, description: 'Whether to wait for server confirmation', }, { displayName: 'Queue', name: 'queue', type: 'string', default: '', placeholder: 'e.g. wuzapi.user.{user_id}.events', description: 'RabbitMQ queue name', }, { displayName: 'Routing Key', name: 'routing_key', type: 'string', default: '', placeholder: 'e.g. wuzapi.{user_id}.{event_type}', description: 'RabbitMQ routing key pattern', }, { displayName: 'URL', name: 'url', type: 'string', default: '', placeholder: 'amqp://user:password@localhost:5672/', description: 'RabbitMQ connection URL', }, ], }, ], }, ], }, // Send Global Test Event { displayName: 'Event Type', name: 'eventType', type: 'string', default: 'TestEvent', placeholder: 'e.g. TestEvent, Message, Connected', description: 'Type of event to send', required: true, displayOptions: { show: { operation: ['sendGlobalTestEvent'], }, }, }, { displayName: 'User ID', name: 'testUserId', type: 'string', default: 'admin-test', placeholder: 'e.g. admin-test', description: 'User ID for the test event', required: true, displayOptions: { show: { operation: ['sendGlobalTestEvent'], }, }, }, { displayName: 'Test Data', name: 'testData', type: 'json', default: '{"message": "test event", "timestamp": "2024-01-01T00:00:00Z"}', description: 'Custom data for the test event (JSON format)', displayOptions: { show: { operation: ['sendGlobalTestEvent'], }, }, }, // Delete User { displayName: 'User ID', name: 'userId', type: 'string', default: '', placeholder: 'e.g. 4e4942c7dee1deef99ab8fd9f7350de5', description: 'ID of the user to delete', required: true, displayOptions: { show: { operation: ['deleteUser', 'deleteUserFull'], }, }, }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; const credentials = await this.getCredentials('wuzapiAdminApi'); for (let i = 0; i < items.length; i++) { try { const operation = this.getNodeParameter('operation', i); let responseData; // Admin API request helper const adminApiRequest = async (method, endpoint, body, qs) => { const options = { method: method, url: `${credentials.url}${endpoint}`, headers: { 'Authorization': credentials.adminToken, 'Content-Type': 'application/json', }, json: true, body, qs, }; return await this.helpers.httpRequest(options); }; if (operation === 'listUsers') { responseData = await adminApiRequest('GET', '/admin/users'); } else if (operation === 'createUser') { const name = this.getNodeParameter('userName', i); const token = this.getNodeParameter('userToken', i); const additionalOptions = this.getNodeParameter('additionalOptions', i); const body = { name, token, }; if (additionalOptions.webhook) { body.webhook = additionalOptions.webhook; } if (additionalOptions.events) { body.events = additionalOptions.events; } if (additionalOptions.proxyConfig) { const proxyConfigWrapper = additionalOptions.proxyConfig; const proxyConfig = proxyConfigWrapper.proxy || proxyConfigWrapper; if (proxyConfig) { const config = Array.isArray(proxyConfig) ? proxyConfig[0] : proxyConfig; if (config) { body.proxyConfig = { Enabled: config.enabled || false, ProxyURL: config.proxyURL || '', }; } } } if (additionalOptions.s3Config) { const s3ConfigWrapper = additionalOptions.s3Config; const s3Config = s3ConfigWrapper.s3 || s3ConfigWrapper; if (s3Config) { const config = Array.isArray(s3Config) ? s3Config[0] : s3Config; if (config) { body.s3Config = { Enabled: config.enabled || false, Endpoint: config.endpoint || '', Region: config.region || 'us-east-1', Bucket: config.bucket || '', AccessKey: config.accessKey || '', SecretKey: config.secretKey || '', PathStyle: config.pathStyle || false, PublicURL: config.publicURL || '', MediaDelivery: config.mediaDelivery || 'both', RetentionDays: config.retentionDays || 30, }; } } } if (additionalOptions.rabbitmqConfig) { const rabbitmqConfigWrapper = additionalOptions.rabbitmqConfig; const rabbitmqConfig = rabbitmqConfigWrapper.rabbitmq || rabbitmqConfigWrapper; if (rabbitmqConfig) { const config = Array.isArray(rabbitmqConfig) ? rabbitmqConfig[0] : rabbitmqConfig; if (config) { body.rabbitmqConfig = { Enabled: config.enabled || false, URL: config.url || '', Exchange: config.exchange || '', Queue: config.queue || '', RoutingKey: config.routing_key || '', Events: config.events || 'All', Durable: config.durable !== false, AutoDelete: config.auto_delete === true, Exclusive: config.exclusive === true, NoWait: config.no_wait === true, DeliveryMode: config.delivery_mode || 2, }; } } } responseData = await adminApiRequest('POST', '/admin/users', body); } else if (operation === 'deleteUser') { const userId = this.getNodeParameter('userId', i); responseData = await adminApiRequest('DELETE', `/admin/users/${userId}`); } else if (operation === 'deleteUserFull') { const userId = this.getNodeParameter('userId', i); responseData = await adminApiRequest('DELETE', `/admin/users/${userId}/full`); } // Global System Management operations else if (operation === 'getGlobalStats') { responseData = await adminApiRequest('GET', '/admin/global/stats'); } else if (operation === 'testGlobalSystems') { responseData = await adminApiRequest('POST', '/admin/global/test'); } else if (operation === 'reloadGlobalConfig') { responseData = await adminApiRequest('POST', '/admin/global/config/reload'); } else if (operation === 'sendGlobalTestEvent') { const eventType = this.getNodeParameter('eventType', i); const testUserId = this.getNodeParameter('testUserId', i); const testDataStr = this.getNodeParameter('testData', i); let testData; try { testData = JSON.parse(testDataStr); } catch (error) { throw new Error('Test Data must be valid JSON'); } const body = { event_type: eventType, user_id: testUserId, data: testData, }; responseData = await adminApiRequest('POST', '/admin/global/event/test', body); } else if (operation === 'getGlobalConfiguration') { responseData = await adminApiRequest('GET', '/admin/global/config'); } const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item: i } }); returnData.push(...executionData); } catch (error) { if (this.continueOnFail()) { const executionErrorData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ error: error.message }), { itemData: { item: i } }); returnData.push(...executionErrorData); continue; } throw error; } } return [returnData]; } } exports.WuzapiAdmin = WuzapiAdmin; //# sourceMappingURL=WuzapiAdmin.node.js.map