skailan-contacts
Version:
Servicio de gestión de contactos para Skailan
54 lines • 2.21 kB
JavaScript
export class AddTagToContact {
contactTagRepository;
contactRepository;
tagRepository;
constructor(contactTagRepository, contactRepository, tagRepository) {
this.contactTagRepository = contactTagRepository;
this.contactRepository = contactRepository;
this.tagRepository = tagRepository;
}
async execute(request) {
try {
const { organizationId, contactId, tagId } = request;
// Validate required fields
if (!organizationId) {
throw new Error('Organization ID is required');
}
if (!contactId) {
throw new Error('Contact ID is required');
}
if (!tagId) {
throw new Error('Tag ID is required');
}
// Validate UUID format
if (!this.isValidUUID(contactId)) {
throw new Error('Invalid contact ID format');
}
if (!this.isValidUUID(tagId)) {
throw new Error('Invalid tag ID format');
}
const contact = await this.contactRepository.findById(contactId, organizationId);
if (!contact) {
throw new Error('Contact not found.');
}
const tag = await this.tagRepository.findById(tagId, organizationId);
if (!tag) {
throw new Error('Tag not found.');
}
// Check if already tagged
const existingTags = await this.contactTagRepository.findTagsByContact(contactId, organizationId);
if (existingTags.some(ct => ct.tagId === tagId)) {
throw new Error('Contact already has this tag.');
}
return await this.contactTagRepository.addTagToContact(contactId, tagId, organizationId);
}
catch (error) {
throw new Error(`Error adding tag to contact: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
isValidUUID(uuid) {
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
return uuidRegex.test(uuid);
}
}
//# sourceMappingURL=AddTagToContact.js.map