2025-06-11 15:13:44 +00:00
|
|
|
import { Criteria } from "@repo/rdx-criteria/server";
|
|
|
|
|
import { UniqueID } from "@repo/rdx-ddd";
|
|
|
|
|
import { Collection, Result } from "@repo/rdx-utils";
|
|
|
|
|
import { Transaction } from "sequelize";
|
2025-06-24 18:38:57 +00:00
|
|
|
import { CustomerInvoice, CustomerInvoiceProps } from "../aggregates";
|
2025-06-11 15:13:44 +00:00
|
|
|
import { ICustomerInvoiceRepository } from "../repositories";
|
2025-06-12 06:55:17 +00:00
|
|
|
import { ICustomerInvoiceService } from "./customer-invoice-service.interface";
|
2025-06-11 15:13:44 +00:00
|
|
|
|
|
|
|
|
export class CustomerInvoiceService implements ICustomerInvoiceService {
|
2025-06-26 11:32:55 +00:00
|
|
|
constructor(private readonly repository: ICustomerInvoiceRepository) {}
|
2025-06-11 15:13:44 +00:00
|
|
|
|
2025-06-26 11:32:55 +00:00
|
|
|
/**
|
|
|
|
|
* Construye un nuevo agregado CustomerInvoice a partir de props validadas.
|
|
|
|
|
*
|
|
|
|
|
* @param props - Las propiedades ya validadas para crear la factura.
|
2025-06-26 18:05:33 +00:00
|
|
|
* @param id - Identificador UUID de la factura (opcional).
|
2025-06-26 11:32:55 +00:00
|
|
|
* @returns Result<CustomerInvoice, Error> - El agregado construido o un error si falla la creación.
|
|
|
|
|
*/
|
2025-06-26 18:05:33 +00:00
|
|
|
build(props: CustomerInvoiceProps, id?: UniqueID): Result<CustomerInvoice, Error> {
|
|
|
|
|
return CustomerInvoice.create(props, id);
|
2025-06-26 11:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Guarda una instancia de CustomerInvoice en persistencia.
|
|
|
|
|
*
|
|
|
|
|
* @param invoice - El agregado a guardar.
|
|
|
|
|
* @param transaction - Transacción activa para la operación.
|
|
|
|
|
* @returns Result<CustomerInvoice, Error> - El agregado guardado o un error si falla la operación.
|
|
|
|
|
*/
|
|
|
|
|
async save(invoice: CustomerInvoice, transaction: any): Promise<Result<CustomerInvoice, Error>> {
|
|
|
|
|
const saved = await this.repository.save(invoice, transaction);
|
|
|
|
|
return saved.isSuccess ? Result.ok(invoice) : Result.fail(saved.error);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-26 18:05:33 +00:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* Comprueba si existe o no en persistencia una factura con el ID proporcionado
|
|
|
|
|
*
|
|
|
|
|
* @param id - Identificador UUID de la factura.
|
|
|
|
|
* @param transaction - Transacción activa para la operación.
|
|
|
|
|
* @returns Result<Boolean, Error> - Existe la factura o no.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
async existsById(id: UniqueID, transaction?: any): Promise<Result<boolean, Error>> {
|
|
|
|
|
return this.repository.existsById(id, transaction);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-26 11:32:55 +00:00
|
|
|
/**
|
|
|
|
|
* Obtiene una colección de facturas que cumplen con los filtros definidos en un objeto Criteria.
|
|
|
|
|
*
|
|
|
|
|
* @param criteria - Objeto con condiciones de filtro, paginación y orden.
|
|
|
|
|
* @param transaction - Transacción activa para la operación.
|
|
|
|
|
* @returns Result<Collection<CustomerInvoice>, Error> - Colección de facturas o error.
|
|
|
|
|
*/
|
|
|
|
|
async findByCriteria(
|
2025-06-11 15:13:44 +00:00
|
|
|
criteria: Criteria,
|
|
|
|
|
transaction?: Transaction
|
|
|
|
|
): Promise<Result<Collection<CustomerInvoice>, Error>> {
|
2025-06-27 10:35:53 +00:00
|
|
|
const customerInvoicesOrError = await this.repository.findByCriteria(criteria, transaction);
|
2025-06-12 06:55:17 +00:00
|
|
|
if (customerInvoicesOrError.isFailure) {
|
|
|
|
|
return Result.fail(customerInvoicesOrError.error);
|
2025-06-11 15:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Solo devolver usuarios activos
|
2025-06-12 06:55:17 +00:00
|
|
|
//const allCustomerInvoices = customerInvoicesOrError.data.filter((customerInvoice) => customerInvoice.isActive);
|
2025-06-11 15:13:44 +00:00
|
|
|
//return Result.ok(new Collection(allCustomerInvoices));
|
|
|
|
|
|
2025-06-12 06:55:17 +00:00
|
|
|
return customerInvoicesOrError;
|
2025-06-11 15:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-26 11:32:55 +00:00
|
|
|
/**
|
|
|
|
|
* Recupera una factura por su identificador único.
|
|
|
|
|
*
|
|
|
|
|
* @param id - Identificador UUID de la factura.
|
|
|
|
|
* @param transaction - Transacción activa para la operación.
|
|
|
|
|
* @returns Result<CustomerInvoice, Error> - Factura encontrada o error.
|
|
|
|
|
*/
|
|
|
|
|
async getById(id: UniqueID, transaction?: Transaction): Promise<Result<CustomerInvoice>> {
|
2025-06-26 18:05:33 +00:00
|
|
|
return await this.repository.findById(id, transaction);
|
2025-06-11 15:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-26 11:32:55 +00:00
|
|
|
/**
|
|
|
|
|
* Actualiza parcialmente una factura existente con nuevos datos.
|
|
|
|
|
*
|
|
|
|
|
* @param id - Identificador de la factura a actualizar.
|
|
|
|
|
* @param changes - Subconjunto de props válidas para aplicar.
|
|
|
|
|
* @param transaction - Transacción activa para la operación.
|
|
|
|
|
* @returns Result<CustomerInvoice, Error> - Factura actualizada o error.
|
|
|
|
|
*/
|
|
|
|
|
async updateById(
|
2025-06-12 06:55:17 +00:00
|
|
|
customerInvoiceId: UniqueID,
|
2025-06-26 11:32:55 +00:00
|
|
|
changes: Partial<CustomerInvoiceProps>,
|
2025-06-11 15:13:44 +00:00
|
|
|
transaction?: Transaction
|
|
|
|
|
): Promise<Result<CustomerInvoice, Error>> {
|
|
|
|
|
// Verificar si la factura existe
|
2025-06-26 11:32:55 +00:00
|
|
|
const customerInvoiceOrError = await this.repository.getById(customerInvoiceId, transaction);
|
2025-06-12 06:55:17 +00:00
|
|
|
if (customerInvoiceOrError.isFailure) {
|
2025-06-11 15:13:44 +00:00
|
|
|
return Result.fail(new Error("CustomerInvoice not found"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result.fail(new Error("No implementado"));
|
|
|
|
|
|
2025-06-12 06:55:17 +00:00
|
|
|
/*const updatedCustomerInvoiceOrError = CustomerInvoice.update(customerInvoiceOrError.data, data);
|
2025-06-11 15:13:44 +00:00
|
|
|
if (updatedCustomerInvoiceOrError.isFailure) {
|
|
|
|
|
return Result.fail(
|
2025-06-12 06:55:17 +00:00
|
|
|
new Error(`Error updating customerInvoice: ${updatedCustomerInvoiceOrError.error.message}`)
|
2025-06-11 15:13:44 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const updateCustomerInvoice = updatedCustomerInvoiceOrError.data;
|
|
|
|
|
|
|
|
|
|
await this.repo.update(updateCustomerInvoice, transaction);
|
|
|
|
|
return Result.ok(updateCustomerInvoice);*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async createCustomerInvoice(
|
2025-06-12 06:55:17 +00:00
|
|
|
customerInvoiceId: UniqueID,
|
2025-06-24 18:38:57 +00:00
|
|
|
data: CustomerInvoiceProps,
|
2025-06-11 15:13:44 +00:00
|
|
|
transaction?: Transaction
|
|
|
|
|
): Promise<Result<CustomerInvoice, Error>> {
|
|
|
|
|
// Verificar si la factura existe
|
2025-06-26 11:32:55 +00:00
|
|
|
const customerInvoiceOrError = await this.repository.getById(customerInvoiceId, transaction);
|
2025-06-12 06:55:17 +00:00
|
|
|
if (customerInvoiceOrError.isSuccess) {
|
2025-06-11 15:13:44 +00:00
|
|
|
return Result.fail(new Error("CustomerInvoice exists"));
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 06:55:17 +00:00
|
|
|
const newCustomerInvoiceOrError = CustomerInvoice.create(data, customerInvoiceId);
|
2025-06-11 15:13:44 +00:00
|
|
|
if (newCustomerInvoiceOrError.isFailure) {
|
2025-06-12 06:55:17 +00:00
|
|
|
return Result.fail(
|
|
|
|
|
new Error(`Error creating customerInvoice: ${newCustomerInvoiceOrError.error.message}`)
|
|
|
|
|
);
|
2025-06-11 15:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const newCustomerInvoice = newCustomerInvoiceOrError.data;
|
|
|
|
|
|
2025-06-26 11:32:55 +00:00
|
|
|
await this.repository.create(newCustomerInvoice, transaction);
|
2025-06-11 15:13:44 +00:00
|
|
|
return Result.ok(newCustomerInvoice);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-26 11:32:55 +00:00
|
|
|
/**
|
|
|
|
|
* Elimina (o marca como eliminada) una factura según su ID.
|
|
|
|
|
*
|
|
|
|
|
* @param id - Identificador UUID de la factura.
|
|
|
|
|
* @param transaction - Transacción activa para la operación.
|
|
|
|
|
* @returns Result<boolean, Error> - Resultado de la operación.
|
|
|
|
|
*/
|
2025-06-26 18:25:38 +00:00
|
|
|
async deleteById(id: UniqueID, transaction?: Transaction): Promise<Result<void, Error>> {
|
2025-06-26 11:32:55 +00:00
|
|
|
return this.repository.deleteById(id, transaction);
|
2025-06-11 15:13:44 +00:00
|
|
|
}
|
|
|
|
|
}
|