import { EntityNotFoundError, ITransactionManager } from "@erp/core/api"; import { DeleteCustomerByIdQueryDTO } from "@erp/customers/common/dto"; import { UniqueID } from "@repo/rdx-ddd"; import { Result } from "@repo/rdx-utils"; import { ICustomerService } from "../../domain"; export class DeleteCustomerUseCase { constructor( private readonly service: ICustomerService, private readonly transactionManager: ITransactionManager ) {} public execute(dto: DeleteCustomerByIdQueryDTO) { const idOrError = UniqueID.create(dto.id); if (idOrError.isFailure) { return Result.fail(idOrError.error); } const id = idOrError.data; return this.transactionManager.complete(async (transaction) => { try { const existsCheck = await this.service.existsById(id, transaction); if (existsCheck.isFailure) { return Result.fail(existsCheck.error); } if (!existsCheck.data) { return Result.fail(new EntityNotFoundError("Customer", id.toString())); } return await this.service.deleteById(id, transaction); } catch (error: unknown) { return Result.fail(error as Error); } }); } }