import { ITransactionManager } from "@erp/core/api"; import { UniqueID } from "@repo/rdx-ddd"; import { Result } from "@repo/rdx-utils"; import { ICustomerInvoiceService } from "../../domain"; import { GetCustomerInvoiceAssembler } from "./assembler"; type GetCustomerInvoiceUseCaseInput = { tenantId: string; id: string; }; export class GetCustomerInvoiceUseCase { constructor( private readonly service: ICustomerInvoiceService, private readonly transactionManager: ITransactionManager, private readonly assembler: GetCustomerInvoiceAssembler ) {} public execute(params: GetCustomerInvoiceUseCaseInput) { const { id, tenantId } = params; const idOrError = UniqueID.create(id); if (idOrError.isFailure) { return Result.fail(idOrError.error); } return this.transactionManager.complete(async (transaction) => { try { const invoiceOrError = await this.service.getById(idOrError.data, transaction); if (invoiceOrError.isFailure) { return Result.fail(invoiceOrError.error); } const getDTO = this.assembler.toDTO(invoiceOrError.data); return Result.ok(getDTO); } catch (error: unknown) { return Result.fail(error as Error); } }); } }