skailan-crm
Version:
Servicio de CRM y gestión de ventas para Skailan
39 lines • 1.88 kB
JavaScript
import { Lead } from '../../domain/entities/Lead';
import { v4 as uuidv4 } from 'uuid';
export class CreateLead {
leadRepository;
contactIntegrationService;
constructor(leadRepository, contactIntegrationService) {
this.leadRepository = leadRepository;
this.contactIntegrationService = contactIntegrationService;
}
async execute(request) {
const { organizationId, name, email, phone, source, notes, company, title, industry } = request;
// Domain validation (e.g., check for existing lead with same email/phone if desired)
if (email) {
const existingLead = await this.leadRepository.findByEmail(email, organizationId);
if (existingLead) {
throw new Error('Lead with this email already exists.');
}
}
const newLead = new Lead(uuidv4(), organizationId, name, email || null, phone || null, 'New', // Default status
source || null, notes || null, null, // Initial score
company || null, title || null, industry || null, new Date(), new Date());
newLead.calculateScore(); // Calculate initial score
// Guardar el lead
const savedLead = await this.leadRepository.save(newLead);
// Sincronizar con el módulo de contactos si el servicio está disponible
if (this.contactIntegrationService && savedLead.email) {
try {
await this.contactIntegrationService.syncLeadWithContact(savedLead);
console.log(`Lead ${savedLead.id} synchronized with contacts module`);
}
catch (error) {
console.error('Failed to sync lead with contacts module:', error);
// No fallamos la creación del lead si falla la sincronización
}
}
return savedLead;
}
}
//# sourceMappingURL=CreateLead.js.map