skailan-conversations
Version:
Servicio de conversaciones y mensajería para Skailan
56 lines • 2.17 kB
JavaScript
import { Channel, } from "../../domain/entities/Channel";
export class CreateChannel {
channelRepository;
constructor(channelRepository) {
this.channelRepository = channelRepository;
}
async execute(request) {
try {
// Validar que la organización existe (esto debería validarse contra el servicio core)
if (!request.organizationId) {
return {
success: false,
error: "Organization ID is required",
};
}
// Validar que el tipo de canal es válido
if (!request.type) {
return {
success: false,
error: "Channel type is required",
};
}
// Validar que la configuración existe
if (!request.config) {
return {
success: false,
error: "Channel configuration is required",
};
}
// Verificar si ya existe un canal del mismo tipo para esta organización
const existingChannel = await this.channelRepository.findByTypeAndOrganization(request.type, request.organizationId);
if (existingChannel) {
return {
success: false,
error: `A ${request.type} channel already exists for this organization`,
};
}
// Crear la entidad Channel
const channel = Channel.create("", // El ID será generado por la base de datos
request.organizationId, request.type, request.config, request.status || "pending_setup");
// Guardar en el repositorio
const savedChannel = await this.channelRepository.create(channel);
return {
success: true,
channel: savedChannel,
};
}
catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : "Unknown error occurred",
};
}
}
}
//# sourceMappingURL=CreateChannel.js.map