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

49 lines
1.3 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;
id: string;
};
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) {
const { companyId, id } = params;
const idOrError = UniqueID.create(id);
2025-08-11 17:49:52 +00:00
if (idOrError.isFailure) {
return Result.fail(idOrError.error);
}
2025-09-01 14:07:59 +00:00
const validId = idOrError.data;
2025-08-11 17:49:52 +00:00
return this.transactionManager.complete(async (transaction) => {
try {
2025-09-01 14:07:59 +00:00
const existsCheck = await this.service.existsByIdInCompany(companyId, validId, 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-01 14:07:59 +00:00
return Result.fail(new EntityNotFoundError("Customer", "id", validId.toString()));
2025-08-11 17:49:52 +00:00
}
2025-09-01 16:38:00 +00:00
return await this.service.deleteCustomerByIdInCompany(validId, companyId, transaction);
2025-08-11 17:49:52 +00:00
} catch (error: unknown) {
return Result.fail(error as Error);
}
});
}
}