97 lines
3.0 KiB
TypeScript
97 lines
3.0 KiB
TypeScript
|
|
import { UniqueID, UtcDate } from "@common/domain";
|
||
|
|
import { Result } from "@common/helpers";
|
||
|
|
import {
|
||
|
|
ISequelizeMapper,
|
||
|
|
MapperParamsType,
|
||
|
|
SequelizeMapper,
|
||
|
|
} from "@common/infrastructure/sequelize/sequelize-mapper";
|
||
|
|
import { Invoice, InvoiceNumber, InvoiceSerie, InvoiceStatus } from "@contexts/invoices/domain/";
|
||
|
|
import { InvoiceCreationAttributes, InvoiceModel } from "../sequelize";
|
||
|
|
|
||
|
|
export interface IInvoiceMapper
|
||
|
|
extends ISequelizeMapper<InvoiceModel, InvoiceCreationAttributes, Invoice> {}
|
||
|
|
|
||
|
|
export class InvoiceMapper
|
||
|
|
extends SequelizeMapper<InvoiceModel, InvoiceCreationAttributes, Invoice>
|
||
|
|
implements IInvoiceMapper
|
||
|
|
{
|
||
|
|
public mapToDomain(source: InvoiceModel, params?: MapperParamsType): Result<Invoice, Error> {
|
||
|
|
const idOrError = UniqueID.create(source.id);
|
||
|
|
const statusOrError = InvoiceStatus.create(source.invoice_status);
|
||
|
|
const invoiceSeriesOrError = InvoiceSerie.create(source.invoice_series);
|
||
|
|
const invoiceNumberOrError = InvoiceNumber.create(source.invoice_number);
|
||
|
|
const issueDateOrError = UtcDate.create(source.issue_date);
|
||
|
|
const operationDateOrError = UtcDate.create(source.operation_date);
|
||
|
|
|
||
|
|
/*const subtotalOrError = MoneyValue.create({
|
||
|
|
amount: source.subtotal,
|
||
|
|
scale: 2,
|
||
|
|
currency_code: source.invoice_currency,
|
||
|
|
});
|
||
|
|
|
||
|
|
const totalOrError = MoneyValue.create({
|
||
|
|
amount: source.total,
|
||
|
|
scale: 2,
|
||
|
|
currency_code: source.invoice_currency,
|
||
|
|
});*/
|
||
|
|
|
||
|
|
const result = Result.combine([
|
||
|
|
idOrError,
|
||
|
|
statusOrError,
|
||
|
|
invoiceSeriesOrError,
|
||
|
|
invoiceNumberOrError,
|
||
|
|
issueDateOrError,
|
||
|
|
operationDateOrError,
|
||
|
|
//subtotalOrError,
|
||
|
|
//totalOrError,
|
||
|
|
]);
|
||
|
|
|
||
|
|
if (result.isFailure) {
|
||
|
|
return Result.fail(result.error);
|
||
|
|
}
|
||
|
|
|
||
|
|
const invoiceCurrency = source.invoice_currency || "EUR";
|
||
|
|
|
||
|
|
return Invoice.create(
|
||
|
|
{
|
||
|
|
status: statusOrError.data,
|
||
|
|
invoiceSeries: invoiceSeriesOrError.data,
|
||
|
|
invoiceNumber: invoiceNumberOrError.data,
|
||
|
|
issueDate: issueDateOrError.data,
|
||
|
|
operationDate: operationDateOrError.data,
|
||
|
|
invoiceCurrency,
|
||
|
|
|
||
|
|
//currency: source.invoice_currency,
|
||
|
|
//subtotal: subtotalOrError.data,
|
||
|
|
//total: totalOrError.data,
|
||
|
|
},
|
||
|
|
idOrError.data
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public mapToPersistence(source: Invoice, params?: MapperParamsType): InvoiceCreationAttributes {
|
||
|
|
const subtotal = source.calculateSubtotal();
|
||
|
|
const total = source.calculateTotal();
|
||
|
|
|
||
|
|
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.invoiceCurrency || "EUR",
|
||
|
|
|
||
|
|
subtotal_amount: subtotal.amount,
|
||
|
|
subtotal_scale: subtotal.scale,
|
||
|
|
|
||
|
|
total_amount: total.amount,
|
||
|
|
total_scale: total.scale,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const invoiceMapper: InvoiceMapper = new InvoiceMapper();
|
||
|
|
export { invoiceMapper };
|