import { ITransactionManager } from "@erp/core/api"; import { UniqueID } from "@repo/rdx-ddd"; import { Result } from "@repo/rdx-utils"; import { CustomerInvoiceService } from "../../domain"; import { GetCustomerInvoiceAssembler } from "./assembler"; type GetCustomerInvoiceUseCaseInput = { companyId: UniqueID; invoice_id: string; }; export class GetCustomerInvoiceUseCase { constructor( private readonly service: CustomerInvoiceService, private readonly transactionManager: ITransactionManager, private readonly assembler: GetCustomerInvoiceAssembler ) {} public execute(params: GetCustomerInvoiceUseCaseInput) { const { invoice_id, companyId } = params; const idOrError = UniqueID.create(invoice_id); if (idOrError.isFailure) { return Result.fail(idOrError.error); } const invoiceId = idOrError.data; return this.transactionManager.complete(async (transaction) => { try { const invoiceOrError = await this.service.getInvoiceByIdInCompany( companyId, invoiceId, 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); } }); } }