Uecko_ERP/modules/customer-invoices/src/api/presentation/get-invoice/get-invoice.controller.ts
2025-06-11 17:13:44 +02:00

45 lines
1.5 KiB
TypeScript

import { ExpressController } from "@erp/core/api";
import { UniqueID } from "@repo/rdx-ddd";
import { GetCustomerInvoiceUseCase } from "../../application";
import { IGetCustomerInvoicePresenter } from "./presenter";
export class GetCustomerInvoiceController extends ExpressController {
public constructor(
private readonly getCustomerInvoice: GetCustomerInvoiceUseCase,
private readonly presenter: IGetCustomerInvoicePresenter
) {
super();
}
protected async executeImpl() {
const { customerCustomerInvoiceId } = this.req.params;
// Validar ID
const customerCustomerInvoiceIdOrError = UniqueID.create(customerCustomerInvoiceId);
if (customerCustomerInvoiceIdOrError.isFailure) return this.invalidInputError("CustomerInvoice ID not valid");
const customerCustomerInvoiceOrError = await this.getCustomerInvoice.execute(customerCustomerInvoiceIdOrError.data);
if (customerCustomerInvoiceOrError.isFailure) {
return this.handleError(customerCustomerInvoiceOrError.error);
}
return this.ok(this.presenter.toDTO(customerCustomerInvoiceOrError.data));
}
private handleError(error: Error) {
const message = error.message;
if (
message.includes("Database connection lost") ||
message.includes("Database request timed out")
) {
return this.unavailableError(
"Database service is currently unavailable. Please try again later."
);
}
return this.conflictError(message);
}
}