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

53 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-08-11 17:49:52 +00:00
import { ITransactionManager } from "@erp/core/api";
import { UniqueID } from "@repo/rdx-ddd";
import { Result } from "@repo/rdx-utils";
import { ICustomerService } from "../../domain";
import { GetCustomerAssembler } from "./assembler";
2025-08-11 17:49:52 +00:00
2025-08-25 17:42:56 +00:00
type GetCustomerUseCaseInput = {
tenantId: string;
id: string;
};
2025-08-11 17:49:52 +00:00
export class GetCustomerUseCase {
constructor(
private readonly service: ICustomerService,
private readonly transactionManager: ITransactionManager,
private readonly assembler: GetCustomerAssembler
2025-08-11 17:49:52 +00:00
) {}
2025-08-25 17:42:56 +00:00
public execute(params: GetCustomerUseCaseInput) {
const { id, tenantId: companyId } = params;
const idOrError = UniqueID.create(id);
2025-08-11 17:49:52 +00:00
if (idOrError.isFailure) {
return Result.fail(idOrError.error);
}
2025-08-25 17:42:56 +00:00
const companyIdOrError = UniqueID.create(companyId);
if (companyIdOrError.isFailure) {
return Result.fail(companyIdOrError.error);
}
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(
companyIdOrError.data,
idOrError.data,
transaction
);
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-08-21 07:44:07 +00:00
const getDTO = this.assembler.toDTO(customerOrError.data);
2025-08-11 17:49:52 +00:00
return Result.ok(getDTO);
} catch (error: unknown) {
return Result.fail(error as Error);
}
});
}
}