2024-05-26 17:09:43 +00:00
|
|
|
import { Currency, Language, UTCDateValue, UniqueID } from "@shared/contexts";
|
|
|
|
|
|
|
|
|
|
import { ISequelizeMapper, SequelizeMapper } from "@/contexts/common/infrastructure";
|
|
|
|
|
import { IQuoteProps, Quote } from "../../domain";
|
2024-07-01 17:12:15 +00:00
|
|
|
import { QuoteStatus } from "../../domain/entities/Quotes/QuoteStatus";
|
2024-05-26 17:09:43 +00:00
|
|
|
import { ISalesContext } from "../Sales.context";
|
|
|
|
|
import { QuoteCreationAttributes, Quote_Model } from "../sequelize";
|
|
|
|
|
import { IQuoteItemMapper, createQuoteItemMapper } from "./quoteItem.mapper";
|
|
|
|
|
|
|
|
|
|
export interface IQuoteMapper
|
|
|
|
|
extends ISequelizeMapper<Quote_Model, QuoteCreationAttributes, Quote> {}
|
|
|
|
|
|
|
|
|
|
export const createQuoteMapper = (context: ISalesContext): IQuoteMapper =>
|
|
|
|
|
new QuoteMapper({
|
|
|
|
|
context,
|
|
|
|
|
quoteItemMapper: createQuoteItemMapper(context),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
class QuoteMapper
|
|
|
|
|
extends SequelizeMapper<Quote_Model, QuoteCreationAttributes, Quote>
|
|
|
|
|
implements IQuoteMapper
|
|
|
|
|
{
|
|
|
|
|
public constructor(props: { quoteItemMapper: IQuoteItemMapper; context: ISalesContext }) {
|
|
|
|
|
super(props);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected toDomainMappingImpl(source: Quote_Model): Quote {
|
|
|
|
|
const id = this.mapsValue(source, "id", UniqueID.create);
|
|
|
|
|
|
|
|
|
|
const items = (this.props.quoteItemMapper as IQuoteItemMapper).mapArrayToDomain(source.items, {
|
|
|
|
|
sourceParent: source,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const props: IQuoteProps = {
|
|
|
|
|
status: this.mapsValue(source, "status", QuoteStatus.create),
|
|
|
|
|
date: this.mapsValue(source, "issue_date", UTCDateValue.create),
|
|
|
|
|
currency: this.mapsValue(source, "quote_currency", Currency.createFromCode),
|
|
|
|
|
language: this.mapsValue(source, "quote_language", Language.createFromCode),
|
2024-07-01 17:12:15 +00:00
|
|
|
customer: source.customer_information,
|
2024-05-26 17:09:43 +00:00
|
|
|
|
|
|
|
|
items,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const quoteOrError = Quote.create(props, id);
|
|
|
|
|
|
|
|
|
|
if (quoteOrError.isFailure) {
|
|
|
|
|
throw quoteOrError.error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return quoteOrError.object;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected toPersistenceMappingImpl(source: Quote) {
|
|
|
|
|
const items = (this.props.quoteItemMapper as IQuoteItemMapper).mapCollectionToPersistence(
|
|
|
|
|
source.items,
|
|
|
|
|
{ sourceParent: source }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const quote: QuoteCreationAttributes = {
|
|
|
|
|
id: source.id.toPrimitive(),
|
|
|
|
|
status: source.status.toPrimitive(),
|
|
|
|
|
date: source.date.toPrimitive(),
|
|
|
|
|
currency_code: source.currency.toPrimitive(),
|
2024-07-01 17:12:15 +00:00
|
|
|
lang_code: source.language.toPrimitive(),
|
|
|
|
|
customer_information: source.customer,
|
2024-05-26 17:09:43 +00:00
|
|
|
subtotal: 0,
|
|
|
|
|
total: 0,
|
|
|
|
|
items,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return quote;
|
|
|
|
|
}
|
|
|
|
|
}
|