UNPKG

skailan-conversations

Version:

Servicio de conversaciones y mensajería para Skailan

178 lines 5.16 kB
export class Channel { id; organizationId; type; config; status; createdAt; updatedAt; constructor(id, organizationId, type, config, status, createdAt, updatedAt) { this.id = id; this.organizationId = organizationId; this.type = type; this.config = config; this.status = status; this.createdAt = createdAt; this.updatedAt = updatedAt; this.validate(); } /** * Valida que la configuración del canal sea correcta según su tipo */ validate() { if (!this.id) { throw new Error('Channel ID is required'); } if (!this.organizationId) { throw new Error('Organization ID is required'); } if (!this.type) { throw new Error('Channel type is required'); } this.validateConfig(); } /** * Valida la configuración específica según el tipo de canal */ validateConfig() { switch (this.type) { case 'telegram': this.validateTelegramConfig(); break; case 'whatsapp': this.validateWhatsAppConfig(); break; case 'facebook': this.validateFacebookConfig(); break; default: // Para otros tipos, solo validar que config existe if (!this.config) { throw new Error(`Configuration is required for channel type: ${this.type}`); } } } validateTelegramConfig() { const telegramConfig = this.config; if (!telegramConfig.botToken) { throw new Error('Telegram bot token is required'); } } validateWhatsAppConfig() { const whatsappConfig = this.config; if (!whatsappConfig.accessToken) { throw new Error('WhatsApp access token is required'); } if (!whatsappConfig.phoneNumberId) { throw new Error('WhatsApp phone number ID is required'); } if (!whatsappConfig.webhookVerifyToken) { throw new Error('WhatsApp webhook verify token is required'); } } validateFacebookConfig() { const facebookConfig = this.config; if (!facebookConfig.pageAccessToken) { throw new Error('Facebook page access token is required'); } if (!facebookConfig.pageId) { throw new Error('Facebook page ID is required'); } if (!facebookConfig.webhookVerifyToken) { throw new Error('Facebook webhook verify token is required'); } } /** * Activa el canal */ activate() { if (this.status === 'error') { throw new Error('Cannot activate channel with error status'); } this.status = 'active'; } /** * Desactiva el canal */ deactivate() { this.status = 'inactive'; } /** * Marca el canal como en error */ markAsError() { this.status = 'error'; } /** * Marca el canal como en mantenimiento */ setMaintenance() { this.status = 'maintenance'; } /** * Verifica si el canal está activo */ isActive() { return this.status === 'active'; } /** * Verifica si el canal está configurado correctamente */ isConfigured() { return this.status !== 'pending_setup'; } /** * Actualiza la configuración del canal */ updateConfig(newConfig) { this.config = newConfig; this.validateConfig(); this.updatedAt = new Date(); } /** * Obtiene la configuración específica según el tipo */ getTelegramConfig() { if (this.type !== 'telegram') { throw new Error('Channel is not a Telegram channel'); } return this.config; } getWhatsAppConfig() { if (this.type !== 'whatsapp') { throw new Error('Channel is not a WhatsApp channel'); } return this.config; } getFacebookConfig() { if (this.type !== 'facebook') { throw new Error('Channel is not a Facebook channel'); } return this.config; } /** * Verifica si el canal puede enviar mensajes */ canSendMessages() { return this.isActive() && this.isConfigured(); } /** * Verifica si el canal puede recibir mensajes */ canReceiveMessages() { return this.isActive() && this.isConfigured(); } /** * Crea una nueva instancia de Channel desde datos primitivos */ static create(id, organizationId, type, config, status = 'pending_setup') { return new Channel(id, organizationId, type, config, status, new Date(), new Date()); } /** * Reconstruye una instancia de Channel desde datos de la base de datos */ static fromData(data) { return new Channel(data.id, data.organizationId, data.type, data.config, data.status, data.createdAt, data.updatedAt); } } //# sourceMappingURL=Channel.js.map