UNPKG

skailan-contacts

Version:

Servicio de gestión de contactos para Skailan

45 lines 2.36 kB
import { ContactCommunicationPlatform } from '../../domain/entities/ContactCommunicationPlatform'; import { v4 as uuidv4 } from 'uuid'; export class AddCommunicationPlatform { contactRepository; platformRepository; constructor(contactRepository, platformRepository) { this.contactRepository = contactRepository; this.platformRepository = platformRepository; } async execute(request) { const { contactId, organizationId, platform, platformUserId, platformUsername, isPrimary = false, config } = request; // Validar plataformas soportadas const validPlatforms = ['whatsapp', 'telegram', 'email', 'sms', 'facebook', 'instagram']; if (!validPlatforms.includes(platform)) { throw new Error(`Invalid platform: ${platform}. Valid platforms are: ${validPlatforms.join(', ')}`); } // Buscar el contacto const contact = await this.contactRepository.findById(contactId, organizationId); if (!contact) { throw new Error('Contact not found.'); } // Verificar si ya existe esta plataforma para este contacto const existingPlatforms = await this.platformRepository.findByContactId(contactId, organizationId); const existingPlatform = existingPlatforms.find(p => p.platform === platform); if (existingPlatform) { throw new Error(`Platform ${platform} already exists for this contact.`); } // Crear la nueva plataforma const newPlatform = new ContactCommunicationPlatform(uuidv4(), contactId, organizationId, platform, platformUserId || null, platformUsername || null, true, // isActive isPrimary, config || null, new Date(), new Date()); // Si se está marcando como primaria, desmarcar las demás if (isPrimary) { for (const existingPlatform of existingPlatforms) { existingPlatform.setAsSecondary(); await this.platformRepository.save(existingPlatform); } } // Guardar la nueva plataforma await this.platformRepository.save(newPlatform); // Actualizar el contacto con la nueva plataforma contact.addCommunicationPlatform(newPlatform); return await this.contactRepository.save(contact); } } //# sourceMappingURL=AddCommunicationPlatform.js.map