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 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> { 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, 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> { 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> { try { this._deleteById(CustomerInvoiceModel, id, false, transaction); return Result.ok(true); } catch (error: any) { return this._handleDatabaseError(error, this._customErrorMapper); } } async create(customerCustomerInvoice: CustomerInvoice, transaction?: Transaction): Promise { const customerCustomerInvoiceData = this._mapper.mapToPersistence(customerCustomerInvoice); await this._save(CustomerInvoiceModel, customerCustomerInvoice.id, customerCustomerInvoiceData, {}, transaction); } async update(customerCustomerInvoice: CustomerInvoice, transaction?: Transaction): Promise { const customerCustomerInvoiceData = this._mapper.mapToPersistence(customerCustomerInvoice); await this._save(CustomerInvoiceModel, customerCustomerInvoice.id, customerCustomerInvoiceData, {}, transaction); } }