UNPKG

skailan-conversations

Version:

Servicio de conversaciones y mensajería para Skailan

453 lines 15.3 kB
export class ChannelController { channelRepository; createChannelUseCase; updateChannelUseCase; configureTelegramChannelUseCase; constructor(channelRepository, createChannelUseCase, updateChannelUseCase, configureTelegramChannelUseCase) { this.channelRepository = channelRepository; this.createChannelUseCase = createChannelUseCase; this.updateChannelUseCase = updateChannelUseCase; this.configureTelegramChannelUseCase = configureTelegramChannelUseCase; } /** * Crear un nuevo canal */ async createChannel(req, res) { try { const { organizationId } = req.params; const { type, config, status } = req.body; // Validar parámetros requeridos if (!organizationId) { res.status(400).json({ success: false, error: "Organization ID is required", }); return; } if (!type) { res.status(400).json({ success: false, error: "Channel type is required", }); return; } if (!config) { res.status(400).json({ success: false, error: "Channel configuration is required", }); return; } // Validar que el organizationId del path coincida con el del body if (req.body.organizationId && req.body.organizationId !== organizationId) { res.status(400).json({ success: false, error: "Organization ID in path must match organization ID in body", }); return; } const result = await this.createChannelUseCase.execute({ organizationId, type, config, status, }); if (result.success) { res.status(201).json({ success: true, data: result.channel, }); } else { res.status(400).json({ success: false, error: result.error, }); } } catch (error) { res.status(500).json({ success: false, error: error instanceof Error ? error.message : "Internal server error", }); } } /** * Obtener todos los canales de una organización */ async getChannels(req, res) { try { const { organizationId } = req.params; if (!organizationId) { res.status(400).json({ success: false, error: "Organization ID is required", }); return; } const channels = await this.channelRepository.findAll(organizationId); res.status(200).json({ success: true, data: channels, }); } catch (error) { res.status(500).json({ success: false, error: error instanceof Error ? error.message : "Internal server error", }); } } /** * Obtener un canal específico */ async getChannel(req, res) { try { const { id } = req.params; const { organizationId } = req.params; if (!id) { res.status(400).json({ success: false, error: "Channel ID is required", }); return; } if (!organizationId) { res.status(400).json({ success: false, error: "Organization ID is required", }); return; } const channel = await this.channelRepository.findById(id); if (!channel) { res.status(404).json({ success: false, error: "Channel not found", }); return; } // Verificar que el canal pertenece a la organización if (channel.organizationId !== organizationId) { res.status(403).json({ success: false, error: "Channel does not belong to the specified organization", }); return; } res.status(200).json({ success: true, data: channel, }); } catch (error) { res.status(500).json({ success: false, error: error instanceof Error ? error.message : "Internal server error", }); } } /** * Actualizar un canal */ async updateChannel(req, res) { try { const { id } = req.params; const { organizationId } = req.params; const { config, status } = req.body; if (!id) { res.status(400).json({ success: false, error: "Channel ID is required", }); return; } if (!organizationId) { res.status(400).json({ success: false, error: "Organization ID is required", }); return; } const result = await this.updateChannelUseCase.execute({ id, organizationId, config, status, }); if (result.success) { res.status(200).json({ success: true, data: result.channel, }); } else { res.status(400).json({ success: false, error: result.error, }); } } catch (error) { res.status(500).json({ success: false, error: error instanceof Error ? error.message : "Internal server error", }); } } /** * Eliminar un canal */ async deleteChannel(req, res) { try { const { id } = req.params; const { organizationId } = req.params; if (!id) { res.status(400).json({ success: false, error: "Channel ID is required", }); return; } if (!organizationId) { res.status(400).json({ success: false, error: "Organization ID is required", }); return; } // Verificar que el canal existe y pertenece a la organización const channel = await this.channelRepository.findById(id); if (!channel) { res.status(404).json({ success: false, error: "Channel not found", }); return; } if (channel.organizationId !== organizationId) { res.status(403).json({ success: false, error: "Channel does not belong to the specified organization", }); return; } await this.channelRepository.delete(id, organizationId); res.status(200).json({ success: true, message: "Channel deleted successfully", }); } catch (error) { res.status(500).json({ success: false, error: error instanceof Error ? error.message : "Internal server error", }); } } /** * Configurar un canal de Telegram específicamente */ async configureTelegramChannel(req, res) { try { const { id } = req.params; const { organizationId } = req.params; const { botToken, webhookUrl, allowedUpdates } = req.body; if (!id) { res.status(400).json({ success: false, error: "Channel ID is required", }); return; } if (!organizationId) { res.status(400).json({ success: false, error: "Organization ID is required", }); return; } const result = await this.configureTelegramChannelUseCase.execute({ channelId: id, organizationId, botToken, webhookUrl, allowedUpdates, }); if (result.success) { res.status(200).json({ success: true, data: result.channel, }); } else { res.status(400).json({ success: false, error: result.error, }); } } catch (error) { res.status(500).json({ success: false, error: error instanceof Error ? error.message : "Internal server error", }); } } /** * Obtener canales por tipo */ async getChannelsByType(req, res) { try { const { organizationId } = req.params; const { type } = req.query; if (!organizationId) { res.status(400).json({ success: false, error: "Organization ID is required", }); return; } if (!type || typeof type !== "string") { res.status(400).json({ success: false, error: "Channel type is required", }); return; } const channel = await this.channelRepository.findByType(type, organizationId); if (!channel) { res.status(404).json({ success: false, error: `No ${type} channel found for this organization`, }); return; } res.status(200).json({ success: true, data: channel, }); } catch (error) { res.status(500).json({ success: false, error: error instanceof Error ? error.message : "Internal server error", }); } } /** * Activar un canal */ async activateChannel(req, res) { try { const { id } = req.params; const { organizationId } = req.params; if (!id) { res.status(400).json({ success: false, error: "Channel ID is required", }); return; } if (!organizationId) { res.status(400).json({ success: false, error: "Organization ID is required", }); return; } const result = await this.updateChannelUseCase.execute({ id, organizationId, status: "active", }); if (result.success) { res.status(200).json({ success: true, data: result.channel, }); } else { res.status(400).json({ success: false, error: result.error, }); } } catch (error) { res.status(500).json({ success: false, error: error instanceof Error ? error.message : "Internal server error", }); } } /** * Desactivar un canal */ async deactivateChannel(req, res) { try { const { id } = req.params; const { organizationId } = req.params; if (!id) { res.status(400).json({ success: false, error: "Channel ID is required", }); return; } if (!organizationId) { res.status(400).json({ success: false, error: "Organization ID is required", }); return; } const result = await this.updateChannelUseCase.execute({ id, organizationId, status: "inactive", }); if (result.success) { res.status(200).json({ success: true, data: result.channel, }); } else { res.status(400).json({ success: false, error: result.error, }); } } catch (error) { res.status(500).json({ success: false, error: error instanceof Error ? error.message : "Internal server error", }); } } } // Exportar funciones para uso en rutas export const createChannel = async (req, res) => { const controller = new ChannelController(req.app.locals.channelRepository, req.app.locals.createChannelUseCase, req.app.locals.updateChannelUseCase, req.app.locals.configureTelegramChannelUseCase); await controller.createChannel(req, res); }; export const getChannelById = async (req, res) => { const controller = new ChannelController(req.app.locals.channelRepository, req.app.locals.createChannelUseCase, req.app.locals.updateChannelUseCase, req.app.locals.configureTelegramChannelUseCase); await controller.getChannel(req, res); }; export const listChannels = async (req, res) => { const controller = new ChannelController(req.app.locals.channelRepository, req.app.locals.createChannelUseCase, req.app.locals.updateChannelUseCase, req.app.locals.configureTelegramChannelUseCase); await controller.getChannels(req, res); }; export const updateChannel = async (req, res) => { const controller = new ChannelController(req.app.locals.channelRepository, req.app.locals.createChannelUseCase, req.app.locals.updateChannelUseCase, req.app.locals.configureTelegramChannelUseCase); await controller.updateChannel(req, res); }; export const deleteChannel = async (req, res) => { const controller = new ChannelController(req.app.locals.channelRepository, req.app.locals.createChannelUseCase, req.app.locals.updateChannelUseCase, req.app.locals.configureTelegramChannelUseCase); await controller.deleteChannel(req, res); }; //# sourceMappingURL=channelController.js.map