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";
|
2025-06-12 06:55:17 +00:00
|
|
|
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";
|
2025-06-11 15:13:44 +00:00
|
|
|
|
2025-06-12 06:55:17 +00:00
|
|
|
export class CustomerInvoiceRepository
|
|
|
|
|
extends SequelizeRepository<CustomerInvoice>
|
|
|
|
|
implements ICustomerInvoiceRepository
|
|
|
|
|
{
|
2025-06-11 15:13:44 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 06:55:17 +00:00
|
|
|
async customerInvoiceExists(
|
|
|
|
|
id: UniqueID,
|
|
|
|
|
transaction?: Transaction
|
|
|
|
|
): Promise<Result<boolean, Error>> {
|
2025-06-11 15:13:44 +00:00
|
|
|
try {
|
2025-06-12 06:55:17 +00:00
|
|
|
const _customerInvoice = await this._getById(CustomerInvoiceModel, id, {}, transaction);
|
2025-06-11 15:13:44 +00:00
|
|
|
|
2025-06-12 06:55:17 +00:00
|
|
|
return Result.ok(Boolean(id.equals(_customerInvoice.id)));
|
2025-06-11 15:13:44 +00:00
|
|
|
} 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),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 06:55:17 +00:00
|
|
|
async create(customerInvoice: CustomerInvoice, transaction?: Transaction): Promise<void> {
|
|
|
|
|
const customerInvoiceData = this._mapper.mapToPersistence(customerInvoice);
|
|
|
|
|
await this._save(
|
|
|
|
|
CustomerInvoiceModel,
|
|
|
|
|
customerInvoice.id,
|
|
|
|
|
customerInvoiceData,
|
|
|
|
|
{},
|
|
|
|
|
transaction
|
|
|
|
|
);
|
2025-06-11 15:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-12 06:55:17 +00:00
|
|
|
async update(customerInvoice: CustomerInvoice, transaction?: Transaction): Promise<void> {
|
|
|
|
|
const customerInvoiceData = this._mapper.mapToPersistence(customerInvoice);
|
|
|
|
|
await this._save(
|
|
|
|
|
CustomerInvoiceModel,
|
|
|
|
|
customerInvoice.id,
|
|
|
|
|
customerInvoiceData,
|
|
|
|
|
{},
|
|
|
|
|
transaction
|
|
|
|
|
);
|
2025-06-11 15:13:44 +00:00
|
|
|
}
|
|
|
|
|
}
|