import { Criteria } from "@repo/rdx-criteria/server"; import { UniqueID } from "@repo/rdx-ddd"; import { Collection, Result } from "@repo/rdx-utils"; import { Transaction } from "sequelize"; import { ICustomerInvoiceProps, CustomerInvoice } from "../aggregates"; import { ICustomerInvoiceRepository } from "../repositories"; import { ICustomerInvoiceService } from "./customerCustomerInvoice-service.interface"; export class CustomerInvoiceService implements ICustomerInvoiceService { constructor(private readonly repo: ICustomerInvoiceRepository) {} async findCustomerInvoices( criteria: Criteria, transaction?: Transaction ): Promise, Error>> { const customerCustomerInvoicesOrError = await this.repo.findAll(criteria, transaction); if (customerCustomerInvoicesOrError.isFailure) { return Result.fail(customerCustomerInvoicesOrError.error); } // Solo devolver usuarios activos //const allCustomerInvoices = customerCustomerInvoicesOrError.data.filter((customerCustomerInvoice) => customerCustomerInvoice.isActive); //return Result.ok(new Collection(allCustomerInvoices)); return customerCustomerInvoicesOrError; } async findCustomerInvoiceById(customerCustomerInvoiceId: UniqueID, transaction?: Transaction): Promise> { return await this.repo.getById(customerCustomerInvoiceId, transaction); } async updateCustomerInvoiceById( customerCustomerInvoiceId: UniqueID, data: Partial, transaction?: Transaction ): Promise> { // Verificar si la factura existe const customerCustomerInvoiceOrError = await this.repo.getById(customerCustomerInvoiceId, transaction); if (customerCustomerInvoiceOrError.isFailure) { return Result.fail(new Error("CustomerInvoice not found")); } return Result.fail(new Error("No implementado")); /*const updatedCustomerInvoiceOrError = CustomerInvoice.update(customerCustomerInvoiceOrError.data, data); if (updatedCustomerInvoiceOrError.isFailure) { return Result.fail( new Error(`Error updating customerCustomerInvoice: ${updatedCustomerInvoiceOrError.error.message}`) ); } const updateCustomerInvoice = updatedCustomerInvoiceOrError.data; await this.repo.update(updateCustomerInvoice, transaction); return Result.ok(updateCustomerInvoice);*/ } async createCustomerInvoice( customerCustomerInvoiceId: UniqueID, data: ICustomerInvoiceProps, transaction?: Transaction ): Promise> { // Verificar si la factura existe const customerCustomerInvoiceOrError = await this.repo.getById(customerCustomerInvoiceId, transaction); if (customerCustomerInvoiceOrError.isSuccess) { return Result.fail(new Error("CustomerInvoice exists")); } const newCustomerInvoiceOrError = CustomerInvoice.create(data, customerCustomerInvoiceId); if (newCustomerInvoiceOrError.isFailure) { return Result.fail(new Error(`Error creating customerCustomerInvoice: ${newCustomerInvoiceOrError.error.message}`)); } const newCustomerInvoice = newCustomerInvoiceOrError.data; await this.repo.create(newCustomerInvoice, transaction); return Result.ok(newCustomerInvoice); } async deleteCustomerInvoiceById( customerCustomerInvoiceId: UniqueID, transaction?: Transaction ): Promise> { return this.repo.deleteById(customerCustomerInvoiceId, transaction); } }