UNPKG

skailan-conversations

Version:

Servicio de conversaciones y mensajería para Skailan

112 lines 4.14 kB
export class UpdateChannel { channelRepository; constructor(channelRepository) { this.channelRepository = channelRepository; } async execute(request) { try { // Validar que el ID del canal existe if (!request.id) { return { success: false, error: "Channel ID is required", }; } // Validar que la organización existe if (!request.organizationId) { return { success: false, error: "Organization ID is required", }; } // Buscar el canal existente const existingChannel = await this.channelRepository.findById(request.id); if (!existingChannel) { return { success: false, error: "Channel not found", }; } // Verificar que el canal pertenece a la organización if (existingChannel.organizationId !== request.organizationId) { return { success: false, error: "Channel does not belong to the specified organization", }; } // Aplicar las actualizaciones if (request.config !== undefined) { existingChannel.updateConfig(request.config); } if (request.status !== undefined) { // Aplicar transiciones de estado según la lógica de negocio this.applyStatusTransition(existingChannel, request.status); } // Guardar los cambios const updatedChannel = await this.channelRepository.update(existingChannel); return { success: true, channel: updatedChannel, }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : "Unknown error occurred", }; } } /** * Aplica transiciones de estado con validaciones de negocio */ applyStatusTransition(channel, newStatus) { const currentStatus = channel.status; // Validar transiciones permitidas switch (currentStatus) { case "pending_setup": if (newStatus === "active" && !channel.isConfigured()) { throw new Error("Cannot activate channel that is not properly configured"); } break; case "active": if (newStatus === "pending_setup") { throw new Error("Cannot revert active channel to pending setup"); } break; case "error": if (newStatus === "active" && !channel.isConfigured()) { throw new Error("Cannot activate channel with error status without proper configuration"); } break; case "maintenance": // Desde mantenimiento se puede ir a cualquier estado break; case "inactive": // Desde inactivo se puede ir a cualquier estado break; } // Aplicar el nuevo estado switch (newStatus) { case "active": channel.activate(); break; case "inactive": channel.deactivate(); break; case "error": channel.markAsError(); break; case "maintenance": channel.setMaintenance(); break; case "pending_setup": // Para pending_setup, solo permitir si no está configurado if (channel.isConfigured()) { throw new Error("Cannot set configured channel to pending setup"); } channel.status = "pending_setup"; break; } } } //# sourceMappingURL=UpdateChannel.js.map