Uecko_ERP/modules/customer-invoices/src/api/application/get-customer-invoice/get-customer-invoice.use-case.ts

37 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-06-26 18:05:33 +00:00
import { ITransactionManager } from "@erp/core/api";
import { GetCustomerInvoiceByIdQueryDTO } from "@erp/customer-invoices/common/dto";
import { UniqueID } from "@repo/rdx-ddd";
2025-05-09 10:45:32 +00:00
import { Result } from "@repo/rdx-utils";
2025-06-26 18:05:33 +00:00
import { ICustomerInvoiceService } from "../../domain";
import { GetCustomerInvoicePresenter } from "./presenter";
2025-03-18 08:05:00 +00:00
2025-06-11 15:13:44 +00:00
export class GetCustomerInvoiceUseCase {
2025-03-18 08:05:00 +00:00
constructor(
2025-06-26 18:05:33 +00:00
private readonly service: ICustomerInvoiceService,
private readonly transactionManager: ITransactionManager,
private readonly presenter: GetCustomerInvoicePresenter
2025-03-18 08:05:00 +00:00
) {}
2025-06-26 18:05:33 +00:00
public execute(dto: GetCustomerInvoiceByIdQueryDTO) {
const idOrError = UniqueID.create(dto.id);
if (idOrError.isFailure) {
return Result.fail(idOrError.error);
}
2025-03-18 08:05:00 +00:00
return this.transactionManager.complete(async (transaction) => {
try {
2025-06-26 18:05:33 +00:00
const invoiceOrError = await this.service.getById(idOrError.data, transaction);
if (invoiceOrError.isFailure) {
return Result.fail(invoiceOrError.error);
}
const getDTO = this.presenter.toDTO(invoiceOrError.data);
return Result.ok(getDTO);
2025-03-18 08:05:00 +00:00
} catch (error: unknown) {
return Result.fail(error as Error);
}
});
}
}