2025-10-04 16:29:14 +00:00
|
|
|
// application/customer-application-service.ts
|
2026-03-09 20:23:48 +00:00
|
|
|
import type { Criteria } from "@repo/rdx-criteria/server";
|
|
|
|
|
import type { UniqueID } from "@repo/rdx-ddd";
|
|
|
|
|
import { type Collection, Result } from "@repo/rdx-utils";
|
|
|
|
|
import type { Transaction } from "sequelize";
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
Customer,
|
|
|
|
|
type CustomerPatchProps,
|
|
|
|
|
type ICustomerCreateProps,
|
|
|
|
|
type ICustomerRepository,
|
|
|
|
|
} from "../domain";
|
|
|
|
|
import type { CustomerListDTO } from "../infrastructure";
|
2025-08-11 17:49:52 +00:00
|
|
|
|
2025-10-04 16:29:14 +00:00
|
|
|
export class CustomerApplicationService {
|
2025-08-11 17:49:52 +00:00
|
|
|
constructor(private readonly repository: ICustomerRepository) {}
|
|
|
|
|
|
2025-10-23 17:29:52 +00:00
|
|
|
/**
|
|
|
|
|
* Construye un nuevo agregado CustomerInvoice a partir de props validadas.
|
|
|
|
|
*
|
|
|
|
|
* @param companyId - Identificador UUID 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).
|
|
|
|
|
* @returns Result<Customer, Error> - El agregado construido o un error si falla la creación.
|
|
|
|
|
*/
|
|
|
|
|
buildCustomerInCompany(
|
|
|
|
|
companyId: UniqueID,
|
2026-03-09 20:23:48 +00:00
|
|
|
props: Omit<ICustomerCreateProps, "companyId">,
|
2025-10-23 17:29:52 +00:00
|
|
|
customerId?: UniqueID
|
|
|
|
|
): Result<Customer, Error> {
|
|
|
|
|
return Customer.create({ ...props, companyId }, customerId);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-11 17:49:52 +00:00
|
|
|
/**
|
2025-10-04 16:29:14 +00:00
|
|
|
* Guarda un nuevo cliente y devuelve el cliente guardado.
|
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-10-04 16:29:14 +00:00
|
|
|
* @param customer - El cliente a guardar.
|
2025-08-11 17:49:52 +00:00
|
|
|
* @param transaction - Transacción activa para la operación.
|
2025-10-04 16:29:14 +00:00
|
|
|
* @returns Result<Customer, Error> - El cliente guardado o un error si falla la operación.
|
2025-08-11 17:49:52 +00:00
|
|
|
*/
|
2025-10-04 16:29:14 +00:00
|
|
|
async createCustomerInCompany(
|
2025-08-25 17:42:56 +00:00
|
|
|
companyId: UniqueID,
|
2025-10-04 16:29:14 +00:00
|
|
|
customer: Customer,
|
|
|
|
|
transaction?: Transaction
|
|
|
|
|
): Promise<Result<Customer, Error>> {
|
|
|
|
|
const result = await this.repository.create(customer, transaction);
|
|
|
|
|
if (result.isFailure) return Result.fail(result.error);
|
2025-08-11 17:49:52 +00:00
|
|
|
|
2025-10-04 16:29:14 +00:00
|
|
|
return this.getCustomerByIdInCompany(companyId, customer.id, transaction);
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-10-04 16:29:14 +00:00
|
|
|
* Actualiza un cliente existente y devuelve el cliente actualizado.
|
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-10-04 16:29:14 +00:00
|
|
|
* @param customer - El cliente a guardar.
|
2025-08-11 17:49:52 +00:00
|
|
|
* @param transaction - Transacción activa para la operación.
|
2025-10-04 16:29:14 +00:00
|
|
|
* @returns Result<Customer, Error> - El cliente guardado o un error si falla la operación.
|
2025-08-11 17:49:52 +00:00
|
|
|
*/
|
2025-10-04 16:29:14 +00:00
|
|
|
async updateCustomerInCompany(
|
2025-08-25 17:42:56 +00:00
|
|
|
companyId: UniqueID,
|
2025-10-04 16:29:14 +00:00
|
|
|
customer: Customer,
|
|
|
|
|
transaction?: Transaction
|
|
|
|
|
): Promise<Result<Customer, Error>> {
|
|
|
|
|
const result = await this.repository.update(customer, transaction);
|
|
|
|
|
if (result.isFailure) return Result.fail(result.error);
|
|
|
|
|
|
|
|
|
|
return this.getCustomerByIdInCompany(companyId, customer.id, 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-10-04 16:29:14 +00:00
|
|
|
* Solo en memoria. 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.
|
2025-09-03 18:04:09 +00:00
|
|
|
* @param changes - 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-10-04 16:29:14 +00:00
|
|
|
async patchCustomerByIdInCompany(
|
2025-08-25 17:42:56 +00:00
|
|
|
companyId: UniqueID,
|
2025-08-11 17:49:52 +00:00
|
|
|
customerId: UniqueID,
|
2025-09-03 18:04:09 +00:00
|
|
|
changes: CustomerPatchProps,
|
2025-10-04 16:29:14 +00:00
|
|
|
transaction?: Transaction
|
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);
|
|
|
|
|
if (customerResult.isFailure) {
|
|
|
|
|
return Result.fail(customerResult.error);
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-04 16:29:14 +00:00
|
|
|
const updated = customerResult.data.update(changes);
|
|
|
|
|
if (updated.isFailure) {
|
|
|
|
|
return Result.fail(updated.error);
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-04 16:29:14 +00:00
|
|
|
return Result.ok(updated.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,
|
2025-10-04 16:29:14 +00:00
|
|
|
transaction?: Transaction
|
|
|
|
|
): Promise<Result<boolean, Error>> {
|
2025-08-25 17:42:56 +00:00
|
|
|
return this.repository.deleteByIdInCompany(companyId, customerId, transaction);
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
2025-10-04 16:29:14 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* Comprueba si existe o no en persistencia un cliente con el ID proporcionado
|
|
|
|
|
*
|
|
|
|
|
* @param companyId - Identificador UUID de la empresa a la que pertenece el cliente.
|
|
|
|
|
* @param customerId - Identificador UUID del cliente
|
|
|
|
|
* @param transaction - Transacción activa para la operación.
|
|
|
|
|
* @returns Result<Boolean, Error> - Existe el cliente o no.
|
|
|
|
|
*/
|
|
|
|
|
async existsByIdInCompany(
|
|
|
|
|
companyId: UniqueID,
|
|
|
|
|
customerId: UniqueID,
|
|
|
|
|
transaction?: Transaction
|
|
|
|
|
): Promise<Result<boolean, Error>> {
|
|
|
|
|
return this.repository.existsByIdInCompany(companyId, customerId, transaction);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Recupera un cliente por su identificador único.
|
|
|
|
|
*
|
|
|
|
|
* @param companyId - Identificador UUID de la empresa a la que pertenece el cliente.
|
|
|
|
|
* @param customerId - Identificador UUID del cliente.
|
|
|
|
|
* @param transaction - Transacción activa para la operación.
|
|
|
|
|
* @returns Result<Customer, Error> - Cliente encontrado o error.
|
|
|
|
|
*/
|
|
|
|
|
async getCustomerByIdInCompany(
|
|
|
|
|
companyId: UniqueID,
|
|
|
|
|
customerId: UniqueID,
|
|
|
|
|
transaction?: Transaction
|
|
|
|
|
): Promise<Result<Customer, Error>> {
|
|
|
|
|
return this.repository.getByIdInCompany(companyId, customerId, transaction);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Obtiene una colección de clientes que cumplen con los filtros definidos en un objeto Criteria.
|
|
|
|
|
*
|
|
|
|
|
* @param companyId - Identificador UUID de la empresa a la que pertenece el cliente.
|
|
|
|
|
* @param criteria - Objeto con condiciones de filtro, paginación y orden.
|
|
|
|
|
* @param transaction - Transacción activa para la operación.
|
|
|
|
|
* @returns Result<Collection<Customer>, Error> - Colección de clientes o error.
|
|
|
|
|
*/
|
|
|
|
|
async findCustomerByCriteriaInCompany(
|
|
|
|
|
companyId: UniqueID,
|
|
|
|
|
criteria: Criteria,
|
|
|
|
|
transaction?: Transaction
|
|
|
|
|
): Promise<Result<Collection<CustomerListDTO>, Error>> {
|
|
|
|
|
return this.repository.findByCriteriaInCompany(companyId, criteria, transaction);
|
|
|
|
|
}
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|