92 lines
3.3 KiB
TypeScript
92 lines
3.3 KiB
TypeScript
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 { CustomerInvoice, ICustomerInvoiceProps } from "../aggregates";
|
|
import { ICustomerInvoiceRepository } from "../repositories";
|
|
import { ICustomerInvoiceService } from "./customer-invoice-service.interface";
|
|
|
|
export class CustomerInvoiceService implements ICustomerInvoiceService {
|
|
constructor(private readonly repo: ICustomerInvoiceRepository) {}
|
|
|
|
async findCustomerInvoices(
|
|
criteria: Criteria,
|
|
transaction?: Transaction
|
|
): Promise<Result<Collection<CustomerInvoice>, Error>> {
|
|
const customerInvoicesOrError = await this.repo.findAll(criteria, transaction);
|
|
if (customerInvoicesOrError.isFailure) {
|
|
return Result.fail(customerInvoicesOrError.error);
|
|
}
|
|
|
|
// Solo devolver usuarios activos
|
|
//const allCustomerInvoices = customerInvoicesOrError.data.filter((customerInvoice) => customerInvoice.isActive);
|
|
//return Result.ok(new Collection(allCustomerInvoices));
|
|
|
|
return customerInvoicesOrError;
|
|
}
|
|
|
|
async findCustomerInvoiceById(
|
|
customerInvoiceId: UniqueID,
|
|
transaction?: Transaction
|
|
): Promise<Result<CustomerInvoice>> {
|
|
return await this.repo.getById(customerInvoiceId, transaction);
|
|
}
|
|
|
|
async updateCustomerInvoiceById(
|
|
customerInvoiceId: UniqueID,
|
|
data: Partial<ICustomerInvoiceProps>,
|
|
transaction?: Transaction
|
|
): Promise<Result<CustomerInvoice, Error>> {
|
|
// Verificar si la factura existe
|
|
const customerInvoiceOrError = await this.repo.getById(customerInvoiceId, transaction);
|
|
if (customerInvoiceOrError.isFailure) {
|
|
return Result.fail(new Error("CustomerInvoice not found"));
|
|
}
|
|
|
|
return Result.fail(new Error("No implementado"));
|
|
|
|
/*const updatedCustomerInvoiceOrError = CustomerInvoice.update(customerInvoiceOrError.data, data);
|
|
if (updatedCustomerInvoiceOrError.isFailure) {
|
|
return Result.fail(
|
|
new Error(`Error updating customerInvoice: ${updatedCustomerInvoiceOrError.error.message}`)
|
|
);
|
|
}
|
|
|
|
const updateCustomerInvoice = updatedCustomerInvoiceOrError.data;
|
|
|
|
await this.repo.update(updateCustomerInvoice, transaction);
|
|
return Result.ok(updateCustomerInvoice);*/
|
|
}
|
|
|
|
async createCustomerInvoice(
|
|
customerInvoiceId: UniqueID,
|
|
data: ICustomerInvoiceProps,
|
|
transaction?: Transaction
|
|
): Promise<Result<CustomerInvoice, Error>> {
|
|
// Verificar si la factura existe
|
|
const customerInvoiceOrError = await this.repo.getById(customerInvoiceId, transaction);
|
|
if (customerInvoiceOrError.isSuccess) {
|
|
return Result.fail(new Error("CustomerInvoice exists"));
|
|
}
|
|
|
|
const newCustomerInvoiceOrError = CustomerInvoice.create(data, customerInvoiceId);
|
|
if (newCustomerInvoiceOrError.isFailure) {
|
|
return Result.fail(
|
|
new Error(`Error creating customerInvoice: ${newCustomerInvoiceOrError.error.message}`)
|
|
);
|
|
}
|
|
|
|
const newCustomerInvoice = newCustomerInvoiceOrError.data;
|
|
|
|
await this.repo.create(newCustomerInvoice, transaction);
|
|
return Result.ok(newCustomerInvoice);
|
|
}
|
|
|
|
async deleteCustomerInvoiceById(
|
|
customerInvoiceId: UniqueID,
|
|
transaction?: Transaction
|
|
): Promise<Result<boolean, Error>> {
|
|
return this.repo.deleteById(customerInvoiceId, transaction);
|
|
}
|
|
}
|