Uecko_ERP/modules/customer-invoices/src/api/infrastructure/sequelize/customer-invoice.repository.ts

108 lines
3.8 KiB
TypeScript
Raw Normal View History

2025-06-11 15:13:44 +00:00
import { SequelizeRepository } from "@erp/core/api";
import { Criteria } from "@repo/rdx-criteria/server";
import { UniqueID } from "@repo/rdx-ddd";
import { Collection, Result } from "@repo/rdx-utils";
import { Sequelize, Transaction } from "sequelize";
import { ICustomerInvoiceRepository, CustomerInvoice } from "../../domain";
import { ICustomerInvoiceMapper } from "../mappers/customerCustomerInvoice.mapper";
import { CustomerInvoiceItemModel } from "./customerCustomerInvoice-item.model";
import { CustomerInvoiceModel } from "./customerCustomerInvoice.model";
export class CustomerInvoiceRepository extends SequelizeRepository<CustomerInvoice> implements ICustomerInvoiceRepository {
private readonly _mapper!: ICustomerInvoiceMapper;
/**
* 🔹 Función personalizada para mapear errores de unicidad en autenticación
*/
private _customErrorMapper(error: Error): string | null {
if (error.name === "SequelizeUniqueConstraintError") {
return "CustomerInvoice with this email already exists";
}
return null;
}
constructor(database: Sequelize, mapper: ICustomerInvoiceMapper) {
super(database);
this._mapper = mapper;
}
async customerCustomerInvoiceExists(id: UniqueID, transaction?: Transaction): Promise<Result<boolean, Error>> {
try {
const _customerCustomerInvoice = await this._getById(CustomerInvoiceModel, id, {}, transaction);
return Result.ok(Boolean(id.equals(_customerCustomerInvoice.id)));
} catch (error: any) {
return this._handleDatabaseError(error, this._customErrorMapper);
}
}
async findAll(
criteria: Criteria,
transaction?: Transaction
): Promise<Result<Collection<CustomerInvoice>, Error>> {
try {
const rawCustomerInvoices = await CustomerInvoiceModel.findAll({
include: [
{
model: CustomerInvoiceItemModel,
as: "items",
},
],
transaction,
...this.convertCriteria(criteria),
});
console.error("aqui");
return this._mapper.mapArrayToDomain(rawCustomerInvoices);
} catch (error: unknown) {
console.error("Error in findAll", error);
return this._handleDatabaseError(error, this._customErrorMapper);
}
}
async getById(id: UniqueID, transaction?: Transaction): Promise<Result<CustomerInvoice, Error>> {
try {
const rawCustomerInvoice: any = await this._getById(
CustomerInvoiceModel,
id,
{
include: [
{
model: CustomerInvoiceItemModel,
as: "items",
},
],
},
transaction
);
if (!rawCustomerInvoice === true) {
return Result.fail(new Error(`CustomerInvoice with id ${id.toString()} not exists`));
}
} catch (error: any) {
return this._handleDatabaseError(error, this._customErrorMapper);
}
}
async deleteById(id: UniqueID, transaction?: Transaction): Promise<Result<boolean, Error>> {
try {
this._deleteById(CustomerInvoiceModel, id, false, transaction);
return Result.ok<boolean>(true);
} catch (error: any) {
return this._handleDatabaseError(error, this._customErrorMapper);
}
}
async create(customerCustomerInvoice: CustomerInvoice, transaction?: Transaction): Promise<void> {
const customerCustomerInvoiceData = this._mapper.mapToPersistence(customerCustomerInvoice);
await this._save(CustomerInvoiceModel, customerCustomerInvoice.id, customerCustomerInvoiceData, {}, transaction);
}
async update(customerCustomerInvoice: CustomerInvoice, transaction?: Transaction): Promise<void> {
const customerCustomerInvoiceData = this._mapper.mapToPersistence(customerCustomerInvoice);
await this._save(CustomerInvoiceModel, customerCustomerInvoice.id, customerCustomerInvoiceData, {}, transaction);
}
}