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

92 lines
3.3 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";
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 {
constructor(private readonly repo: ICustomerInvoiceRepository) {}
async findCustomerInvoices(
criteria: Criteria,
transaction?: Transaction
): Promise<Result<Collection<CustomerInvoice>, Error>> {
2025-06-12 06:55:17 +00:00
const customerInvoicesOrError = await this.repo.findAll(criteria, transaction);
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-12 06:55:17 +00:00
async findCustomerInvoiceById(
customerInvoiceId: UniqueID,
transaction?: Transaction
): Promise<Result<CustomerInvoice>> {
return await this.repo.getById(customerInvoiceId, transaction);
2025-06-11 15:13:44 +00:00
}
async updateCustomerInvoiceById(
2025-06-12 06:55:17 +00:00
customerInvoiceId: UniqueID,
2025-06-24 18:38:57 +00:00
data: Partial<CustomerInvoiceProps>,
2025-06-11 15:13:44 +00:00
transaction?: Transaction
): Promise<Result<CustomerInvoice, Error>> {
// Verificar si la factura existe
2025-06-12 06:55:17 +00:00
const customerInvoiceOrError = await this.repo.getById(customerInvoiceId, transaction);
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-12 06:55:17 +00:00
const customerInvoiceOrError = await this.repo.getById(customerInvoiceId, transaction);
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;
await this.repo.create(newCustomerInvoice, transaction);
return Result.ok(newCustomerInvoice);
}
async deleteCustomerInvoiceById(
2025-06-12 06:55:17 +00:00
customerInvoiceId: UniqueID,
2025-06-11 15:13:44 +00:00
transaction?: Transaction
): Promise<Result<boolean, Error>> {
2025-06-12 06:55:17 +00:00
return this.repo.deleteById(customerInvoiceId, transaction);
2025-06-11 15:13:44 +00:00
}
}