43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import { IContactRepository } from '../../../Domain/IRepository/IContactRepository';
|
|
import { DeleteContactCommand } from './DeleteContactCommand';
|
|
import { AdminAuditService } from '../../Services/AdminBypassService';
|
|
import { logRequest } from '../../Services/Logger';
|
|
|
|
export class DeleteContactCommandHandler {
|
|
constructor(private readonly contactRepo: IContactRepository) {}
|
|
|
|
async execute(cmd: DeleteContactCommand): Promise<boolean> {
|
|
try {
|
|
const existingContact = await this.contactRepo.findById(cmd.id);
|
|
if (!existingContact) {
|
|
throw new Error('Contact not found');
|
|
}
|
|
|
|
if (cmd.hard) {
|
|
// Permanent delete
|
|
await this.contactRepo.delete(cmd.id);
|
|
logRequest('Contact hard deleted', undefined, undefined, {
|
|
contactId: cmd.id,
|
|
contactEmail: existingContact.email,
|
|
deleteType: 'hard'
|
|
});
|
|
} else {
|
|
// Soft delete (default)
|
|
await this.contactRepo.softDelete(cmd.id);
|
|
logRequest('Contact soft deleted', undefined, undefined, {
|
|
contactId: cmd.id,
|
|
contactEmail: existingContact.email,
|
|
deleteType: 'soft'
|
|
});
|
|
}
|
|
|
|
return true;
|
|
} catch (error) {
|
|
if (error instanceof Error && error.message === 'Contact not found') {
|
|
throw error;
|
|
}
|
|
throw new Error('Failed to delete contact');
|
|
}
|
|
}
|
|
}
|