skailan-contacts
Version:
Servicio de gestión de contactos para Skailan
174 lines • 6.92 kB
JavaScript
import { ContactCommunicationPlatform } from './ContactCommunicationPlatform';
export class Contact {
id;
organizationId;
externalId;
name;
email;
phone;
profilePictureUrl;
metadata;
createdAt;
updatedAt;
communicationPlatforms;
constructor(id, organizationId, externalId, name, email, phone, profilePictureUrl, metadata, createdAt, updatedAt, communicationPlatforms = []) {
this.id = id;
this.organizationId = organizationId;
this.externalId = externalId;
this.name = name;
this.email = email;
this.phone = phone;
this.profilePictureUrl = profilePictureUrl;
this.metadata = metadata;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
this.communicationPlatforms = communicationPlatforms;
this.validate();
}
validate() {
if (!this.id) {
throw new Error('Contact ID is required');
}
if (!this.organizationId) {
throw new Error('Organization ID is required');
}
if (!this.externalId) {
throw new Error('External ID is required');
}
if (this.email && !this.isValidEmail(this.email)) {
throw new Error('Invalid email format');
}
if (this.phone && !this.isValidPhone(this.phone)) {
throw new Error('Invalid phone format');
}
}
isValidEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
isValidPhone(phone) {
const phoneRegex = /^[\+]?[1-9][\d]{0,15}$/;
return phoneRegex.test(phone.replace(/\s/g, ''));
}
updateInfo(name, email, phone, profilePictureUrl, metadata) {
this.name = name;
this.email = email;
this.phone = phone;
this.profilePictureUrl = profilePictureUrl;
this.metadata = metadata;
this.updatedAt = new Date();
this.validate();
}
// Communication platform methods
addCommunicationPlatform(platform) {
// If this is the first platform, set it as primary
if (this.communicationPlatforms.length === 0) {
platform.setAsPrimary();
}
// If this platform is being set as primary, unset others
if (platform.isPrimary) {
this.communicationPlatforms.forEach(p => p.setAsSecondary());
}
this.communicationPlatforms.push(platform);
this.updatedAt = new Date();
}
removeCommunicationPlatform(platformId) {
const platformIndex = this.communicationPlatforms.findIndex(p => p.id === platformId);
if (platformIndex === -1) {
throw new Error('Platform not found');
}
const removedPlatform = this.communicationPlatforms[platformIndex];
// If removing primary platform, set first remaining as primary
if (removedPlatform?.isPrimary && this.communicationPlatforms.length > 1) {
const remainingPlatforms = this.communicationPlatforms.filter(p => p.id !== platformId);
if (remainingPlatforms.length > 0 && remainingPlatforms[0]) {
remainingPlatforms[0].setAsPrimary();
}
}
this.communicationPlatforms.splice(platformIndex, 1);
this.updatedAt = new Date();
}
getPrimaryPlatform() {
return this.communicationPlatforms.find(p => p.isPrimary) || null;
}
getActivePlatforms() {
return this.communicationPlatforms.filter(p => p.isActive);
}
getPlatformByType(platformType) {
return this.communicationPlatforms.find(p => p.platform === platformType && p.isActive) || null;
}
setPlatformAsPrimary(platformId) {
const platform = this.communicationPlatforms.find(p => p.id === platformId);
if (!platform) {
throw new Error('Platform not found');
}
// Unset current primary
this.communicationPlatforms.forEach(p => p.setAsSecondary());
// Set new primary
platform.setAsPrimary();
this.updatedAt = new Date();
}
// Method to create contact from conversation data
static createFromConversation(id, organizationId, externalId, name, platform, platformUserId, platformUsername) {
const contact = new Contact(id, organizationId, externalId, name, null, // email
null, // phone
null, // profilePictureUrl
{
source: 'conversation',
platform: platform,
platformUserId: platformUserId,
platformUsername: platformUsername
}, new Date(), new Date());
// Add the communication platform
const communicationPlatform = new ContactCommunicationPlatform(id, // Using same ID for simplicity, could be different
contact.id, organizationId, platform, platformUserId, platformUsername, true, // isActive
true, // isPrimary (first platform)
null, // config
new Date(), new Date());
contact.addCommunicationPlatform(communicationPlatform);
return contact;
}
// Method to create contact from lead data
static createFromLead(id, organizationId, externalId, name, email, phone, metadata) {
const contact = new Contact(id, organizationId, externalId, name, email, phone, null, // profilePictureUrl
{
...metadata,
source: 'crm_lead'
}, new Date(), new Date());
// Add email as communication platform if available
if (email) {
const emailPlatform = new ContactCommunicationPlatform(`${id}_email`, contact.id, organizationId, 'email', email, null, // platformUsername
true, // isActive
true, // isPrimary
null, // config
new Date(), new Date());
contact.addCommunicationPlatform(emailPlatform);
}
// Add phone as communication platform if available
if (phone) {
const phonePlatform = new ContactCommunicationPlatform(`${id}_phone`, contact.id, organizationId, 'sms', phone, null, // platformUsername
true, // isActive
!email, // isPrimary only if no email
null, // config
new Date(), new Date());
contact.addCommunicationPlatform(phonePlatform);
}
return contact;
}
toJSON() {
return {
id: this.id,
organizationId: this.organizationId,
externalId: this.externalId,
name: this.name,
email: this.email,
phone: this.phone,
profilePictureUrl: this.profilePictureUrl,
metadata: this.metadata,
communicationPlatforms: this.communicationPlatforms.map(p => p.toJSON()),
createdAt: this.createdAt,
updatedAt: this.updatedAt
};
}
}
//# sourceMappingURL=Contact.js.map