2025-08-11 17:49:52 +00:00
|
|
|
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";
|
2025-08-14 14:58:13 +00:00
|
|
|
import { GetCustomerAssembler } from "./assembler";
|
2025-08-11 17:49:52 +00:00
|
|
|
|
|
|
|
|
export class GetCustomerUseCase {
|
|
|
|
|
constructor(
|
|
|
|
|
private readonly service: ICustomerService,
|
|
|
|
|
private readonly transactionManager: ITransactionManager,
|
2025-08-14 14:58:13 +00:00
|
|
|
private readonly assembler: GetCustomerAssembler
|
2025-08-11 17:49:52 +00:00
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-14 14:58:13 +00:00
|
|
|
const getDTO = this.assembler.toDTO(invoiceOrError.data);
|
2025-08-11 17:49:52 +00:00
|
|
|
return Result.ok(getDTO);
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
return Result.fail(error as Error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|