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

53 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-08-11 17:49:52 +00:00
import { EntityNotFoundError, ITransactionManager } from "@erp/core/api";
import { UniqueID } from "@repo/rdx-ddd";
import { Result } from "@repo/rdx-utils";
2025-09-01 14:07:59 +00:00
import { CustomerService } from "../../domain";
type DeleteCustomerUseCaseInput = {
companyId: UniqueID;
2025-09-02 08:57:41 +00:00
customer_id: string;
2025-09-01 14:07:59 +00:00
};
2025-08-11 17:49:52 +00:00
export class DeleteCustomerUseCase {
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-01 14:07:59 +00:00
public execute(params: DeleteCustomerUseCaseInput) {
2025-09-02 08:57:41 +00:00
const { companyId, customer_id } = params;
2025-09-01 14:07:59 +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-08-11 17:49:52 +00:00
return this.transactionManager.complete(async (transaction) => {
try {
2025-09-02 08:57:41 +00:00
const existsCheck = await this.service.existsByIdInCompany(
companyId,
customerId,
transaction
);
2025-08-11 17:49:52 +00:00
if (existsCheck.isFailure) {
return Result.fail(existsCheck.error);
}
2025-09-01 16:38:00 +00:00
const customerExists = existsCheck.data;
if (!customerExists) {
2025-09-02 08:57:41 +00:00
return Result.fail(new EntityNotFoundError("Customer", "id", customerId.toString()));
2025-08-11 17:49:52 +00:00
}
2025-09-02 08:57:41 +00:00
return await this.service.deleteCustomerByIdInCompany(customerId, companyId, transaction);
2025-08-11 17:49:52 +00:00
} catch (error: unknown) {
return Result.fail(error as Error);
}
});
}
}