98 lines
3.8 KiB
TypeScript
98 lines
3.8 KiB
TypeScript
|
|
import { ISequelizeMapper, MapperParamsType, SequelizeMapper } from "@erp/core/api";
|
||
|
|
import { UniqueID, UtcDate } from "@repo/rdx-ddd";
|
||
|
|
import { Result } from "@repo/rdx-utils";
|
||
|
|
import { CustomerInvoice, CustomerInvoiceNumber, CustomerInvoiceSerie, CustomerInvoiceStatus } from "../../domain";
|
||
|
|
import { CustomerInvoiceCreationAttributes, CustomerInvoiceModel } from "../sequelize";
|
||
|
|
import { CustomerInvoiceItemMapper } from "./customerCustomerInvoice-item.mapper";
|
||
|
|
|
||
|
|
export interface ICustomerInvoiceMapper
|
||
|
|
extends ISequelizeMapper<CustomerInvoiceModel, CustomerInvoiceCreationAttributes, CustomerInvoice> {}
|
||
|
|
|
||
|
|
export class CustomerInvoiceMapper
|
||
|
|
extends SequelizeMapper<CustomerInvoiceModel, CustomerInvoiceCreationAttributes, CustomerInvoice>
|
||
|
|
implements ICustomerInvoiceMapper
|
||
|
|
{
|
||
|
|
private customerCustomerInvoiceItemMapper: CustomerInvoiceItemMapper;
|
||
|
|
|
||
|
|
constructor() {
|
||
|
|
super();
|
||
|
|
this.customerCustomerInvoiceItemMapper = new CustomerInvoiceItemMapper(); // Instanciar el mapper de items
|
||
|
|
}
|
||
|
|
|
||
|
|
public mapToDomain(source: CustomerInvoiceModel, params?: MapperParamsType): Result<CustomerInvoice, Error> {
|
||
|
|
const idOrError = UniqueID.create(source.id);
|
||
|
|
const statusOrError = CustomerInvoiceStatus.create(source.customerCustomerInvoice_status);
|
||
|
|
const customerCustomerInvoiceSeriesOrError = CustomerInvoiceSerie.create(source.customerCustomerInvoice_series);
|
||
|
|
const customerCustomerInvoiceNumberOrError = CustomerInvoiceNumber.create(source.customerCustomerInvoice_number);
|
||
|
|
const issueDateOrError = UtcDate.create(source.issue_date);
|
||
|
|
const operationDateOrError = UtcDate.create(source.operation_date);
|
||
|
|
|
||
|
|
const result = Result.combine([
|
||
|
|
idOrError,
|
||
|
|
statusOrError,
|
||
|
|
customerCustomerInvoiceSeriesOrError,
|
||
|
|
customerCustomerInvoiceNumberOrError,
|
||
|
|
issueDateOrError,
|
||
|
|
operationDateOrError,
|
||
|
|
]);
|
||
|
|
|
||
|
|
if (result.isFailure) {
|
||
|
|
return Result.fail(result.error);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Mapear los items de la factura
|
||
|
|
const itemsOrErrors = this.customerCustomerInvoiceItemMapper.mapArrayToDomain(source.items, {
|
||
|
|
sourceParent: source,
|
||
|
|
...params,
|
||
|
|
});
|
||
|
|
|
||
|
|
if (itemsOrErrors.isFailure) {
|
||
|
|
return Result.fail(itemsOrErrors.error);
|
||
|
|
}
|
||
|
|
|
||
|
|
const customerCustomerInvoiceCurrency = source.customerCustomerInvoice_currency || "EUR";
|
||
|
|
|
||
|
|
return CustomerInvoice.create(
|
||
|
|
{
|
||
|
|
status: statusOrError.data,
|
||
|
|
customerCustomerInvoiceSeries: customerCustomerInvoiceSeriesOrError.data,
|
||
|
|
customerCustomerInvoiceNumber: customerCustomerInvoiceNumberOrError.data,
|
||
|
|
issueDate: issueDateOrError.data,
|
||
|
|
operationDate: operationDateOrError.data,
|
||
|
|
customerCustomerInvoiceCurrency,
|
||
|
|
items: itemsOrErrors.data,
|
||
|
|
},
|
||
|
|
idOrError.data
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public mapToPersistence(source: CustomerInvoice, params?: MapperParamsType): CustomerInvoiceCreationAttributes {
|
||
|
|
const subtotal = source.calculateSubtotal();
|
||
|
|
const total = source.calculateTotal();
|
||
|
|
|
||
|
|
const items = this.customerCustomerInvoiceItemMapper.mapCollectionToPersistence(source.items, params);
|
||
|
|
|
||
|
|
return {
|
||
|
|
id: source.id.toString(),
|
||
|
|
customerCustomerInvoice_status: source.status.toPrimitive(),
|
||
|
|
customerCustomerInvoice_series: source.customerCustomerInvoiceSeries.toPrimitive(),
|
||
|
|
customerCustomerInvoice_number: source.customerCustomerInvoiceNumber.toPrimitive(),
|
||
|
|
issue_date: source.issueDate.toPrimitive(),
|
||
|
|
operation_date: source.operationDate.toPrimitive(),
|
||
|
|
customerCustomerInvoice_language: "es",
|
||
|
|
customerCustomerInvoice_currency: source.customerCustomerInvoiceCurrency || "EUR",
|
||
|
|
|
||
|
|
subtotal_amount: subtotal.amount,
|
||
|
|
subtotal_scale: subtotal.scale,
|
||
|
|
|
||
|
|
total_amount: total.amount,
|
||
|
|
total_scale: total.scale,
|
||
|
|
|
||
|
|
items,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const customerCustomerInvoiceMapper: CustomerInvoiceMapper = new CustomerInvoiceMapper();
|
||
|
|
export { customerCustomerInvoiceMapper };
|