2025-09-14 10:04:57 +00:00
|
|
|
import { IPresenterRegistry, ITransactionManager } from "@erp/core/api";
|
2025-08-11 17:49:52 +00:00
|
|
|
import { UniqueID } from "@repo/rdx-ddd";
|
|
|
|
|
import { Result } from "@repo/rdx-utils";
|
2025-09-01 14:07:59 +00:00
|
|
|
import { CustomerService } from "../../domain";
|
2025-09-14 10:04:57 +00:00
|
|
|
import { CustomerFullPresenter } from "../presenters";
|
2025-08-11 17:49:52 +00:00
|
|
|
|
2025-08-25 17:42:56 +00:00
|
|
|
type GetCustomerUseCaseInput = {
|
2025-09-01 14:07:59 +00:00
|
|
|
companyId: UniqueID;
|
2025-09-02 08:57:41 +00:00
|
|
|
customer_id: string;
|
2025-08-25 17:42:56 +00:00
|
|
|
};
|
|
|
|
|
|
2025-08-11 17:49:52 +00:00
|
|
|
export class GetCustomerUseCase {
|
|
|
|
|
constructor(
|
2025-09-01 14:07:59 +00:00
|
|
|
private readonly service: CustomerService,
|
2025-08-11 17:49:52 +00:00
|
|
|
private readonly transactionManager: ITransactionManager,
|
2025-09-14 10:04:57 +00:00
|
|
|
private readonly presenterRegistry: IPresenterRegistry
|
2025-08-11 17:49:52 +00:00
|
|
|
) {}
|
|
|
|
|
|
2025-08-25 17:42:56 +00:00
|
|
|
public execute(params: GetCustomerUseCaseInput) {
|
2025-09-02 08:57:41 +00:00
|
|
|
const { customer_id, companyId } = params;
|
2025-08-25 17:42:56 +00:00
|
|
|
|
2025-09-02 08:57:41 +00:00
|
|
|
const idOrError = UniqueID.create(customer_id);
|
2025-08-11 17:49:52 +00:00
|
|
|
if (idOrError.isFailure) {
|
|
|
|
|
return Result.fail(idOrError.error);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-02 08:57:41 +00:00
|
|
|
const customerId = idOrError.data;
|
2025-09-14 10:04:57 +00:00
|
|
|
const presenter = this.presenterRegistry.getPresenter({
|
|
|
|
|
resource: "customer",
|
|
|
|
|
projection: "FULL",
|
|
|
|
|
}) as CustomerFullPresenter;
|
2025-09-02 08:57:41 +00:00
|
|
|
|
2025-08-11 17:49:52 +00:00
|
|
|
return this.transactionManager.complete(async (transaction) => {
|
|
|
|
|
try {
|
2025-08-25 17:42:56 +00:00
|
|
|
const customerOrError = await this.service.getCustomerByIdInCompany(
|
2025-09-01 14:07:59 +00:00
|
|
|
companyId,
|
2025-09-02 08:57:41 +00:00
|
|
|
customerId,
|
2025-08-25 17:42:56 +00:00
|
|
|
transaction
|
|
|
|
|
);
|
2025-09-01 14:07:59 +00:00
|
|
|
|
2025-08-21 07:44:07 +00:00
|
|
|
if (customerOrError.isFailure) {
|
|
|
|
|
return Result.fail(customerOrError.error);
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-14 10:04:57 +00:00
|
|
|
const customer = customerOrError.data;
|
|
|
|
|
const dto = presenter.toOutput(customer);
|
|
|
|
|
|
|
|
|
|
return Result.ok(dto);
|
2025-08-11 17:49:52 +00:00
|
|
|
} catch (error: unknown) {
|
|
|
|
|
return Result.fail(error as Error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|