2025-08-11 17:49:52 +00:00
|
|
|
import { Criteria } from "@repo/rdx-criteria/server";
|
|
|
|
|
import { UniqueID } from "@repo/rdx-ddd";
|
|
|
|
|
import { Collection, Result } from "@repo/rdx-utils";
|
2025-09-01 14:07:59 +00:00
|
|
|
import { Customer, CustomerPatchProps, CustomerProps } from "../aggregates";
|
2025-08-11 17:49:52 +00:00
|
|
|
import { ICustomerRepository } from "../repositories";
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
export class CustomerService {
|
2025-08-11 17:49:52 +00:00
|
|
|
constructor(private readonly repository: ICustomerRepository) {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construye un nuevo agregado Customer a partir de props validadas.
|
|
|
|
|
*
|
2025-08-25 17:42:56 +00:00
|
|
|
* @param companyId - Identificador de la empresa a la que pertenece el cliente.
|
|
|
|
|
* @param props - Las propiedades ya validadas para crear el cliente.
|
|
|
|
|
* @param customerId - Identificador UUID del cliente (opcional).
|
2025-08-11 17:49:52 +00:00
|
|
|
* @returns Result<Customer, Error> - El agregado construido o un error si falla la creación.
|
|
|
|
|
*/
|
2025-08-25 17:42:56 +00:00
|
|
|
buildCustomerInCompany(
|
|
|
|
|
companyId: UniqueID,
|
|
|
|
|
props: Omit<CustomerProps, "companyId">,
|
|
|
|
|
customerId?: UniqueID
|
|
|
|
|
): Result<Customer, Error> {
|
|
|
|
|
return Customer.create({ ...props, companyId }, customerId);
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Guarda una instancia de Customer en persistencia.
|
|
|
|
|
*
|
2025-08-26 18:55:59 +00:00
|
|
|
* @param customer - El agregado a guardar.
|
2025-08-11 17:49:52 +00:00
|
|
|
* @param transaction - Transacción activa para la operación.
|
|
|
|
|
* @returns Result<Customer, Error> - El agregado guardado o un error si falla la operación.
|
|
|
|
|
*/
|
2025-08-26 18:55:59 +00:00
|
|
|
async saveCustomer(customer: Customer, transaction: any): Promise<Result<Customer, Error>> {
|
2025-08-25 17:42:56 +00:00
|
|
|
return this.repository.save(customer, transaction);
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
2025-08-25 17:42:56 +00:00
|
|
|
* Comprueba si existe o no en persistencia un cliente con el ID proporcionado
|
2025-08-11 17:49:52 +00:00
|
|
|
*
|
2025-08-25 17:42:56 +00:00
|
|
|
* @param companyId - Identificador UUID de la empresa a la que pertenece el cliente.
|
|
|
|
|
* @param customerId - Identificador UUID del cliente
|
2025-08-11 17:49:52 +00:00
|
|
|
* @param transaction - Transacción activa para la operación.
|
2025-08-25 17:42:56 +00:00
|
|
|
* @returns Result<Boolean, Error> - Existe el cliente o no.
|
2025-08-11 17:49:52 +00:00
|
|
|
*/
|
|
|
|
|
|
2025-08-25 17:42:56 +00:00
|
|
|
existsByIdInCompany(
|
|
|
|
|
companyId: UniqueID,
|
|
|
|
|
customerId: UniqueID,
|
|
|
|
|
transaction?: any
|
|
|
|
|
): Promise<Result<boolean, Error>> {
|
|
|
|
|
return this.repository.existsByIdInCompany(companyId, customerId, transaction);
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-08-25 17:42:56 +00:00
|
|
|
* Obtiene una colección de clientes que cumplen con los filtros definidos en un objeto Criteria.
|
2025-08-11 17:49:52 +00:00
|
|
|
*
|
2025-08-25 17:42:56 +00:00
|
|
|
* @param companyId - Identificador UUID de la empresa a la que pertenece el cliente.
|
2025-08-11 17:49:52 +00:00
|
|
|
* @param criteria - Objeto con condiciones de filtro, paginación y orden.
|
|
|
|
|
* @param transaction - Transacción activa para la operación.
|
2025-08-25 17:42:56 +00:00
|
|
|
* @returns Result<Collection<Customer>, Error> - Colección de clientes o error.
|
2025-08-11 17:49:52 +00:00
|
|
|
*/
|
2025-08-26 18:55:59 +00:00
|
|
|
async findCustomerByCriteriaInCompany(
|
2025-08-25 17:42:56 +00:00
|
|
|
companyId: UniqueID,
|
2025-08-11 17:49:52 +00:00
|
|
|
criteria: Criteria,
|
2025-08-25 17:42:56 +00:00
|
|
|
transaction?: any
|
2025-08-26 18:55:59 +00:00
|
|
|
): Promise<Result<Collection<Customer>, Error>> {
|
2025-08-25 17:42:56 +00:00
|
|
|
return this.repository.findByCriteriaInCompany(companyId, criteria, transaction);
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-08-25 17:42:56 +00:00
|
|
|
* Recupera un cliente por su identificador único.
|
2025-08-11 17:49:52 +00:00
|
|
|
*
|
2025-08-25 17:42:56 +00:00
|
|
|
* @param companyId - Identificador UUID de la empresa a la que pertenece el cliente.
|
|
|
|
|
* @param customerId - Identificador UUID del cliente.
|
2025-08-11 17:49:52 +00:00
|
|
|
* @param transaction - Transacción activa para la operación.
|
2025-08-25 17:42:56 +00:00
|
|
|
* @returns Result<Customer, Error> - Cliente encontradoF o error.
|
2025-08-11 17:49:52 +00:00
|
|
|
*/
|
2025-08-25 17:42:56 +00:00
|
|
|
async getCustomerByIdInCompany(
|
|
|
|
|
companyId: UniqueID,
|
|
|
|
|
customerId: UniqueID,
|
|
|
|
|
transaction?: any
|
|
|
|
|
): Promise<Result<Customer>> {
|
|
|
|
|
return this.repository.getByIdInCompany(companyId, customerId, transaction);
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-08-25 17:42:56 +00:00
|
|
|
* Actualiza parcialmente un cliente existente con nuevos datos.
|
2025-09-01 14:07:59 +00:00
|
|
|
* No lo guarda en el repositorio.
|
2025-08-11 17:49:52 +00:00
|
|
|
*
|
2025-08-25 17:42:56 +00:00
|
|
|
* @param companyId - Identificador de la empresa a la que pertenece el cliente.
|
|
|
|
|
* @param customerId - Identificador del cliente a actualizar.
|
|
|
|
|
* @param partial - Subconjunto de props válidas para aplicar.
|
2025-08-11 17:49:52 +00:00
|
|
|
* @param transaction - Transacción activa para la operación.
|
2025-08-25 17:42:56 +00:00
|
|
|
* @returns Result<Customer, Error> - Cliente actualizado o error.
|
2025-08-11 17:49:52 +00:00
|
|
|
*/
|
2025-08-25 17:42:56 +00:00
|
|
|
async updateCustomerByIdInCompany(
|
|
|
|
|
companyId: UniqueID,
|
2025-08-11 17:49:52 +00:00
|
|
|
customerId: UniqueID,
|
2025-09-01 14:07:59 +00:00
|
|
|
partial: CustomerPatchProps,
|
2025-08-25 17:42:56 +00:00
|
|
|
transaction?: any
|
2025-09-01 14:07:59 +00:00
|
|
|
): Promise<Result<Customer, Error>> {
|
2025-08-25 17:42:56 +00:00
|
|
|
const customerResult = await this.getCustomerByIdInCompany(companyId, customerId, transaction);
|
2025-08-11 17:49:52 +00:00
|
|
|
|
2025-08-25 17:42:56 +00:00
|
|
|
if (customerResult.isFailure) {
|
|
|
|
|
return Result.fail(customerResult.error);
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
2025-08-25 17:42:56 +00:00
|
|
|
const customer = customerResult.data;
|
|
|
|
|
const updatedCustomer = customer.update(partial);
|
2025-08-11 17:49:52 +00:00
|
|
|
|
2025-08-25 17:42:56 +00:00
|
|
|
if (updatedCustomer.isFailure) {
|
|
|
|
|
return Result.fail(updatedCustomer.error);
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
return Result.ok(updatedCustomer.data);
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-08-25 17:42:56 +00:00
|
|
|
* Elimina (o marca como eliminado) un cliente según su ID.
|
2025-08-11 17:49:52 +00:00
|
|
|
*
|
2025-08-25 17:42:56 +00:00
|
|
|
* @param companyId - Identificador de la empresa a la que pertenece el cliente.
|
|
|
|
|
* @param customerId - Identificador UUID del cliente.
|
2025-08-11 17:49:52 +00:00
|
|
|
* @param transaction - Transacción activa para la operación.
|
|
|
|
|
* @returns Result<boolean, Error> - Resultado de la operación.
|
|
|
|
|
*/
|
2025-08-25 17:42:56 +00:00
|
|
|
async deleteCustomerByIdInCompany(
|
|
|
|
|
companyId: UniqueID,
|
|
|
|
|
customerId: UniqueID,
|
|
|
|
|
transaction?: any
|
|
|
|
|
): Promise<Result<void>> {
|
|
|
|
|
return this.repository.deleteByIdInCompany(companyId, customerId, transaction);
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
}
|