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

49 lines
1.4 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-09-03 18:04:09 +00:00
import { CustomerInvoiceService } from "../../domain";
2025-09-04 10:02:24 +00:00
import { GetCustomerInvoiceAssembler } from "./assembler";
type GetCustomerInvoiceUseCaseInput = {
2025-09-03 18:04:09 +00:00
companyId: UniqueID;
invoice_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-09-03 18:04:09 +00:00
private readonly service: CustomerInvoiceService,
2025-06-26 18:05:33 +00:00
private readonly transactionManager: ITransactionManager,
2025-09-04 10:02:24 +00:00
private readonly assembler: GetCustomerInvoiceAssembler
2025-03-18 08:05:00 +00:00
) {}
public execute(params: GetCustomerInvoiceUseCaseInput) {
2025-09-03 18:04:09 +00:00
const { invoice_id, companyId } = params;
const idOrError = UniqueID.create(invoice_id);
2025-06-26 18:05:33 +00:00
if (idOrError.isFailure) {
return Result.fail(idOrError.error);
}
2025-09-03 18:04:09 +00:00
const invoiceId = idOrError.data;
2025-03-18 08:05:00 +00:00
return this.transactionManager.complete(async (transaction) => {
try {
2025-09-03 18:04:09 +00:00
const invoiceOrError = await this.service.getInvoiceByIdInCompany(
companyId,
invoiceId,
transaction
);
2025-06-26 18:05:33 +00:00
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);
}
});
}
}