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 { CustomerInvoice, ICustomerInvoiceRepository } from "../../domain"; import { ICustomerInvoiceMapper } from "../mappers/customer-invoice.mapper"; import { CustomerInvoiceItemModel } from "./customer-invoice-item.model"; import { CustomerInvoiceModel } from "./customer-invoice.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 customerInvoiceExists( id: UniqueID, transaction?: Transaction ): Promise> { try { const _customerInvoice = await this._getById(CustomerInvoiceModel, id, {}, transaction); return Result.ok(Boolean(id.equals(_customerInvoice.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), }); 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(customerInvoice: CustomerInvoice, transaction?: Transaction): Promise { const customerInvoiceData = this._mapper.mapToPersistence(customerInvoice); await this._save( CustomerInvoiceModel, customerInvoice.id, customerInvoiceData, {}, transaction ); } async update(customerInvoice: CustomerInvoice, transaction?: Transaction): Promise { const customerInvoiceData = this._mapper.mapToPersistence(customerInvoice); await this._save( CustomerInvoiceModel, customerInvoice.id, customerInvoiceData, {}, transaction ); } }