37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
|
|
import { ITransactionManager } from "@erp/core/api";
|
||
|
|
import { GetCustomerByIdQueryDTO } from "@erp/customers/common/dto";
|
||
|
|
import { UniqueID } from "@repo/rdx-ddd";
|
||
|
|
import { Result } from "@repo/rdx-utils";
|
||
|
|
import { ICustomerService } from "../../domain";
|
||
|
|
import { GetCustomerPresenter } from "./presenter";
|
||
|
|
|
||
|
|
export class GetCustomerUseCase {
|
||
|
|
constructor(
|
||
|
|
private readonly service: ICustomerService,
|
||
|
|
private readonly transactionManager: ITransactionManager,
|
||
|
|
private readonly presenter: GetCustomerPresenter
|
||
|
|
) {}
|
||
|
|
|
||
|
|
public execute(dto: GetCustomerByIdQueryDTO) {
|
||
|
|
const idOrError = UniqueID.create(dto.id);
|
||
|
|
|
||
|
|
if (idOrError.isFailure) {
|
||
|
|
return Result.fail(idOrError.error);
|
||
|
|
}
|
||
|
|
|
||
|
|
return this.transactionManager.complete(async (transaction) => {
|
||
|
|
try {
|
||
|
|
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);
|
||
|
|
} catch (error: unknown) {
|
||
|
|
return Result.fail(error as Error);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|