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

42 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-06-26 18:05:33 +00:00
import { ITransactionManager } from "@erp/core/api";
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 { GetCustomerInvoiceAssembler } from "./assembler";
type GetCustomerInvoiceUseCaseInput = {
tenantId: string;
id: string;
};
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 assembler: GetCustomerInvoiceAssembler
2025-03-18 08:05:00 +00:00
) {}
public execute(params: GetCustomerInvoiceUseCaseInput) {
const { id, tenantId } = params;
const idOrError = UniqueID.create(id);
2025-06-26 18:05:33 +00:00
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.assembler.toDTO(invoiceOrError.data);
2025-06-26 18:05:33 +00:00
return Result.ok(getDTO);
2025-03-18 08:05:00 +00:00
} catch (error: unknown) {
return Result.fail(error as Error);
}
});
}
}