Uecko_ERP/modules/customer-invoices/src/api/domain/services/customer-invoice.service.ts

87 lines
3.5 KiB
TypeScript
Raw Normal View History

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";
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<Result<Collection<CustomerInvoice>, 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<Result<CustomerInvoice>> {
return await this.repo.getById(customerCustomerInvoiceId, transaction);
}
async updateCustomerInvoiceById(
customerCustomerInvoiceId: UniqueID,
data: Partial<ICustomerInvoiceProps>,
transaction?: Transaction
): Promise<Result<CustomerInvoice, Error>> {
// 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<Result<CustomerInvoice, Error>> {
// 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<Result<boolean, Error>> {
return this.repo.deleteById(customerCustomerInvoiceId, transaction);
}
}