95 lines
3.1 KiB
TypeScript
95 lines
3.1 KiB
TypeScript
|
|
import { ISequelizeMapper, MapperParamsType, SequelizeMapper } from "@erp/core/api";
|
||
|
|
import { UniqueID, UtcDate } from "@repo/rdx-ddd";
|
||
|
|
import { Result } from "@repo/rdx-utils";
|
||
|
|
import { Customer, CustomerNumber, CustomerSerie, CustomerStatus } from "../../domain";
|
||
|
|
import { CustomerCreationAttributes, CustomerModel } from "../sequelize";
|
||
|
|
import { CustomerItemMapper } from "./customer-item.mapper";
|
||
|
|
|
||
|
|
export interface ICustomerMapper
|
||
|
|
extends ISequelizeMapper<CustomerModel, CustomerCreationAttributes, Customer> {}
|
||
|
|
|
||
|
|
export class CustomerMapper
|
||
|
|
extends SequelizeMapper<CustomerModel, CustomerCreationAttributes, Customer>
|
||
|
|
implements ICustomerMapper
|
||
|
|
{
|
||
|
|
private customerItemMapper: CustomerItemMapper;
|
||
|
|
|
||
|
|
constructor() {
|
||
|
|
super();
|
||
|
|
this.customerItemMapper = new CustomerItemMapper(); // Instanciar el mapper de items
|
||
|
|
}
|
||
|
|
|
||
|
|
public mapToDomain(source: CustomerModel, params?: MapperParamsType): Result<Customer, Error> {
|
||
|
|
const idOrError = UniqueID.create(source.id);
|
||
|
|
const statusOrError = CustomerStatus.create(source.invoice_status);
|
||
|
|
const customerSeriesOrError = CustomerSerie.create(source.invoice_series);
|
||
|
|
const customerNumberOrError = CustomerNumber.create(source.invoice_number);
|
||
|
|
const issueDateOrError = UtcDate.createFromISO(source.issue_date);
|
||
|
|
const operationDateOrError = UtcDate.createFromISO(source.operation_date);
|
||
|
|
|
||
|
|
const result = Result.combine([
|
||
|
|
idOrError,
|
||
|
|
statusOrError,
|
||
|
|
customerSeriesOrError,
|
||
|
|
customerNumberOrError,
|
||
|
|
issueDateOrError,
|
||
|
|
operationDateOrError,
|
||
|
|
]);
|
||
|
|
|
||
|
|
if (result.isFailure) {
|
||
|
|
return Result.fail(result.error);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Mapear los items de la factura
|
||
|
|
const itemsOrErrors = this.customerItemMapper.mapArrayToDomain(source.items, {
|
||
|
|
sourceParent: source,
|
||
|
|
...params,
|
||
|
|
});
|
||
|
|
|
||
|
|
if (itemsOrErrors.isFailure) {
|
||
|
|
return Result.fail(itemsOrErrors.error);
|
||
|
|
}
|
||
|
|
|
||
|
|
const customerCurrency = source.invoice_currency || "EUR";
|
||
|
|
|
||
|
|
return Customer.create(
|
||
|
|
{
|
||
|
|
status: statusOrError.data,
|
||
|
|
invoiceSeries: customerSeriesOrError.data,
|
||
|
|
invoiceNumber: customerNumberOrError.data,
|
||
|
|
issueDate: issueDateOrError.data,
|
||
|
|
operationDate: operationDateOrError.data,
|
||
|
|
currency: customerCurrency,
|
||
|
|
items: itemsOrErrors.data,
|
||
|
|
},
|
||
|
|
idOrError.data
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public mapToPersistence(source: Customer, params?: MapperParamsType): CustomerCreationAttributes {
|
||
|
|
const subtotal = source.calculateSubtotal();
|
||
|
|
const total = source.calculateTotal();
|
||
|
|
|
||
|
|
const items = this.customerItemMapper.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,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|