143 lines
4.1 KiB
TypeScript
143 lines
4.1 KiB
TypeScript
import {
|
|
ISequelizeMapper,
|
|
MapperParamsType,
|
|
SequelizeMapper,
|
|
ValidationErrorCollection,
|
|
ValidationErrorDetail,
|
|
extractOrPushError,
|
|
} from "@erp/core/api";
|
|
import { CurrencyCode, LanguageCode, UniqueID, UtcDate, maybeFromNullableVO } from "@repo/rdx-ddd";
|
|
import { Result } from "@repo/rdx-utils";
|
|
import {
|
|
CustomerInvoice,
|
|
CustomerInvoiceNumber,
|
|
CustomerInvoiceProps,
|
|
CustomerInvoiceSerie,
|
|
CustomerInvoiceStatus,
|
|
} from "../../domain";
|
|
import { CustomerInvoiceCreationAttributes, CustomerInvoiceModel } from "../sequelize";
|
|
import { CustomerInvoiceItemMapper } from "./customer-invoice-item.mapper";
|
|
|
|
export interface ICustomerInvoiceMapper
|
|
extends ISequelizeMapper<
|
|
CustomerInvoiceModel,
|
|
CustomerInvoiceCreationAttributes,
|
|
CustomerInvoice
|
|
> {}
|
|
|
|
export class CustomerInvoiceMapper
|
|
extends SequelizeMapper<CustomerInvoiceModel, CustomerInvoiceCreationAttributes, CustomerInvoice>
|
|
implements ICustomerInvoiceMapper
|
|
{
|
|
private customerInvoiceItemMapper: CustomerInvoiceItemMapper;
|
|
|
|
constructor() {
|
|
super();
|
|
this.customerInvoiceItemMapper = new CustomerInvoiceItemMapper(); // Instanciar el mapper de items
|
|
}
|
|
|
|
public mapToDomain(
|
|
source: CustomerInvoiceModel,
|
|
params?: MapperParamsType
|
|
): Result<CustomerInvoice, Error> {
|
|
const errors: ValidationErrorDetail[] = [];
|
|
|
|
const invoiceId = extractOrPushError(UniqueID.create(source.id), "id", errors);
|
|
const companyId = extractOrPushError(UniqueID.create(source.company_id), "company_id", errors);
|
|
|
|
const status = extractOrPushError(
|
|
CustomerInvoiceStatus.create(source.status),
|
|
"status",
|
|
errors
|
|
);
|
|
const series = extractOrPushError(CustomerInvoiceSerie.create(source.series), "series", errors);
|
|
const invoiceNumber = extractOrPushError(
|
|
CustomerInvoiceNumber.create(source.invoice_number),
|
|
"invoice_number",
|
|
errors
|
|
);
|
|
const issueDate = extractOrPushError(
|
|
UtcDate.createFromISO(source.issue_date),
|
|
"issue_date",
|
|
errors
|
|
);
|
|
|
|
const operationDate = extractOrPushError(
|
|
maybeFromNullableVO(source.operation_date, (value) => UtcDate.createFromISO(value)),
|
|
"operation_date",
|
|
errors
|
|
);
|
|
|
|
const languageCode = extractOrPushError(
|
|
LanguageCode.create(source.language_code),
|
|
"language_code",
|
|
errors
|
|
);
|
|
|
|
const currencyCode = extractOrPushError(
|
|
CurrencyCode.create(source.currency_code),
|
|
"currency_code",
|
|
errors
|
|
);
|
|
|
|
if (errors.length > 0) {
|
|
return Result.fail(
|
|
new ValidationErrorCollection("Customer invoice props mapping failed", errors)
|
|
);
|
|
}
|
|
|
|
// Mapear los items de la factura
|
|
/*const itemsOrErrors = this.customerInvoiceItemMapper.mapArrayToDomain(source.items, {
|
|
sourceParent: source,
|
|
...params,
|
|
});
|
|
|
|
if (itemsOrErrors.isFailure) {
|
|
return Result.fail(itemsOrErrors.error);
|
|
}*/
|
|
|
|
const invoiceProps: CustomerInvoiceProps = {
|
|
status: status!,
|
|
series: series!,
|
|
invoiceNumber: invoiceNumber!,
|
|
issueDate: issueDate!,
|
|
operationDate: operationDate!,
|
|
|
|
languageCode: languageCode!,
|
|
currencyCode: currencyCode!,
|
|
//items: itemsOrErrors.data,
|
|
};
|
|
|
|
return CustomerInvoice.create(invoiceProps, invoiceId);
|
|
}
|
|
|
|
public mapToPersistence(
|
|
source: CustomerInvoice,
|
|
params?: MapperParamsType
|
|
): CustomerInvoiceCreationAttributes {
|
|
const subtotal = source.calculateSubtotal();
|
|
const total = source.calculateTotal();
|
|
|
|
const items = this.customerInvoiceItemMapper.mapCollectionToPersistence(source.items, params);
|
|
|
|
return {
|
|
id: source.id.toString(),
|
|
invoice_status: source.status.toPrimitive(),
|
|
invoice_series: source.invoiceSeries.toPrimitive(),
|
|
invoice_number: source.invoiceNumber.toPrimitive(),
|
|
issue_date: source.issueDate.toPrimitive(),
|
|
operation_date: source.operationDate.toPrimitive(),
|
|
invoice_language: "es",
|
|
invoice_currency: source.currency || "EUR",
|
|
|
|
subtotal_amount: subtotal.amount,
|
|
subtotal_scale: subtotal.scale,
|
|
|
|
total_amount: total.amount,
|
|
total_scale: total.scale,
|
|
|
|
items,
|
|
};
|
|
}
|
|
}
|