import { IContactRepository } from '../../../Domain/IRepository/IContactRepository'; import { UpdateContactCommand } from './UpdateContactCommand'; import { DetailContactDto } from '../../DTOs/ContactDto'; import { ContactMapper } from '../../DTOs/Mappers/ContactMapper'; import { ContactState } from '../../../Domain/Contact/ContactAggregate'; export class UpdateContactCommandHandler { constructor(private readonly contactRepo: IContactRepository) {} async execute(cmd: UpdateContactCommand): Promise { try { const existingContact = await this.contactRepo.findById(cmd.id); if (!existingContact) { throw new Error('Contact not found'); } const updateData: any = {}; if (cmd.adminResponse !== undefined) { updateData.adminResponse = cmd.adminResponse; updateData.responseDate = new Date(); } if (cmd.state !== undefined) { updateData.state = cmd.state; } if (cmd.respondedBy !== undefined) { updateData.respondedBy = cmd.respondedBy; } const updated = await this.contactRepo.update(cmd.id, updateData); if (!updated) { throw new Error('Failed to update contact'); } return ContactMapper.toDetailDto(updated); } catch (error) { if (error instanceof Error && error.message === 'Contact not found') { throw error; } throw new Error('Failed to update contact'); } } }