Skip to content

Commit

Permalink
refactor: map both net and gross prices to client model (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi committed Apr 8, 2020
1 parent 3598344 commit e5ea457
Show file tree
Hide file tree
Showing 44 changed files with 547 additions and 326 deletions.
4 changes: 2 additions & 2 deletions src/app/core/models/basket-rebate/basket-rebate.interface.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { PriceItem } from 'ish-core/models/price-item/price-item.interface';
import { PriceItemData } from 'ish-core/models/price-item/price-item.interface';

export interface BasketRebateData {
id: string;
promotionType: string;
amount: PriceItem;
amount: PriceItemData;
description?: string;
code?: string;
promotion?: string;
Expand Down
29 changes: 17 additions & 12 deletions src/app/core/models/basket-rebate/basket-rebate.mapper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { BasketRebateData } from './basket-rebate.interface';
import { BasketRebateMapper } from './basket-rebate.mapper';

describe('Basket Rebate Mapper', () => {
describe('fromData', () => {
it(`should return BasketRebate when getting BasketRebateData`, () => {
const basketRebateData = {
const basketRebate = BasketRebateMapper.fromData({
id: 'basketDiscountId',
promotionType: 'Discount',
amount: {
Expand All @@ -20,17 +19,23 @@ describe('Basket Rebate Mapper', () => {
description: 'Dicount description',
code: 'CODE5433',
promotion: 'FreeShippingOnLEDTVs',
} as BasketRebateData;
});

const basketRebate = BasketRebateMapper.fromData(basketRebateData);

expect(basketRebate).toBeTruthy();
expect(basketRebate.id).toBe(basketRebateData.id);
expect(basketRebate.rebateType).toBe(basketRebateData.promotionType);
expect(basketRebate.description).toBe(basketRebateData.description);
expect(basketRebate.code).toBe(basketRebateData.code);
expect(basketRebate.amount.value).toBePositive();
expect(basketRebate.promotionId).toBe(basketRebateData.promotion);
expect(basketRebate).toMatchInlineSnapshot(`
Object {
"amount": Object {
"currency": "USD",
"gross": 43.34,
"net": 40.34,
"type": "PriceItem",
},
"code": "CODE5433",
"description": "Dicount description",
"id": "basketDiscountId",
"promotionId": "FreeShippingOnLEDTVs",
"rebateType": "Discount",
}
`);
});
});
});
4 changes: 2 additions & 2 deletions src/app/core/models/basket-rebate/basket-rebate.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BasketRebateData } from 'ish-core/models/basket-rebate/basket-rebate.interface';
import { PriceMapper } from 'ish-core/models/price/price.mapper';
import { PriceItemMapper } from 'ish-core/models/price-item/price-item.mapper';

import { BasketRebate } from './basket-rebate.model';

Expand All @@ -8,7 +8,7 @@ export class BasketRebateMapper {
if (data) {
return {
id: data.id,
amount: PriceMapper.fromPriceItem(data.amount),
amount: PriceItemMapper.fromPriceItem(data.amount),
description: data.description,
rebateType: data.promotionType,
code: data.code,
Expand Down
4 changes: 2 additions & 2 deletions src/app/core/models/basket-rebate/basket-rebate.model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Price } from 'ish-core/models/price/price.model';
import { PriceItem } from 'ish-core/models/price-item/price-item.model';

export interface BasketRebate {
id: string;
amount: Price;
amount: PriceItem;
description?: string;
rebateType: string;
code?: string;
Expand Down
35 changes: 15 additions & 20 deletions src/app/core/models/basket-total/basket-total.interface.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import { PriceItem } from 'ish-core/models/price-item/price-item.interface';
import { Price } from 'ish-core/models/price/price.model';
import { PriceItemData } from 'ish-core/models/price-item/price-item.interface';

export interface BasketTotalData {
itemTotal: PriceItem;
undiscountedItemTotal?: PriceItem;
shippingTotal?: PriceItem;
undiscountedShippingTotal: PriceItem;
paymentCostsTotal?: PriceItem;
surchargeTotal?: PriceItem;
grandTotal: PriceItem;
itemTotal: PriceItemData;
undiscountedItemTotal?: PriceItemData;
shippingTotal?: PriceItemData;
undiscountedShippingTotal: PriceItemData;
paymentCostsTotal?: PriceItemData;
surchargeTotal?: PriceItemData;
grandTotal: PriceItemData;

itemValueDiscountsTotal?: PriceItem;
basketValueDiscountsTotal?: PriceItem;
itemValueDiscountsTotal?: PriceItemData;
basketValueDiscountsTotal?: PriceItemData;

itemShippingDiscountsTotal?: PriceItem;
basketShippingDiscountsTotal?: PriceItem;
itemShippingDiscountsTotal?: PriceItemData;
basketShippingDiscountsTotal?: PriceItemData;

shippingDiscountsTotal?: PriceItem;
valueDiscountsTotal: PriceItem;
discountTotal?: PriceItem;

taxTotalsByTaxRate?: {
calculatedTax: Price;
}[];
shippingDiscountsTotal?: PriceItemData;
valueDiscountsTotal: PriceItemData;
discountTotal?: PriceItemData;
}
27 changes: 14 additions & 13 deletions src/app/core/models/basket-total/basket-total.model.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import { BasketRebate } from 'ish-core/models/basket-rebate/basket-rebate.model';
import { PriceItem } from 'ish-core/models/price-item/price-item.model';
import { Price } from 'ish-core/models/price/price.model';

export interface BasketTotal {
itemTotal: Price;
undiscountedItemTotal?: Price;
shippingTotal?: Price;
undiscountedShippingTotal?: Price;
paymentCostsTotal?: Price;
dutiesAndSurchargesTotal?: Price;
itemTotal: PriceItem;
undiscountedItemTotal?: PriceItem;
shippingTotal?: PriceItem;
undiscountedShippingTotal?: PriceItem;
paymentCostsTotal?: PriceItem;
dutiesAndSurchargesTotal?: PriceItem;
taxTotal?: Price;
total: Price;
total: PriceItem;

itemRebatesTotal?: Price;
valueRebatesTotal?: Price;
itemRebatesTotal?: PriceItem;
valueRebatesTotal?: PriceItem;
valueRebates?: BasketRebate[];

itemShippingRebatesTotal?: Price;
shippingRebatesTotal?: Price;
itemShippingRebatesTotal?: PriceItem;
shippingRebatesTotal?: PriceItem;
shippingRebates?: BasketRebate[];

itemSurchargeTotalsByType?: {
amount: Price;
amount: PriceItem;
description: string;
displayName: string;
}[];

bucketSurchargeTotalsByType?: {
amount: Price;
amount: PriceItem;
description: string;
displayName: string;
}[];
Expand Down
6 changes: 3 additions & 3 deletions src/app/core/models/basket/basket.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { LineItemData } from 'ish-core/models/line-item/line-item.interface';
import { PaymentInstrument } from 'ish-core/models/payment-instrument/payment-instrument.model';
import { PaymentMethodBaseData } from 'ish-core/models/payment-method/payment-method.interface';
import { PaymentData } from 'ish-core/models/payment/payment.interface';
import { PriceItem } from 'ish-core/models/price-item/price-item.interface';
import { PriceItemData } from 'ish-core/models/price-item/price-item.interface';
import { ShippingMethodData } from 'ish-core/models/shipping-method/shipping-method.interface';

export interface BasketBaseData {
Expand All @@ -29,12 +29,12 @@ export interface BasketBaseData {
totalProductQuantity?: number;
surcharges?: {
itemSurcharges?: {
amount: PriceItem;
amount: PriceItemData;
description: string;
name: string;
}[];
bucketSurcharges?: {
amount: PriceItem;
amount: PriceItemData;
description: string;
name: string;
}[];
Expand Down
16 changes: 8 additions & 8 deletions src/app/core/models/basket/basket.mapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ describe('Basket Mapper', () => {
basket = BasketMapper.fromData(basketData);
expect(basket).toBeTruthy();

expect(basket.totals.itemTotal.value).toBe(basketData.data.totals.itemTotal.gross.value);
expect(basket.totals.itemSurchargeTotalsByType[0].amount.value).toBe(
expect(basket.totals.itemTotal.gross).toBe(basketData.data.totals.itemTotal.gross.value);
expect(basket.totals.itemSurchargeTotalsByType[0].amount.gross).toBe(
basketData.data.surcharges.itemSurcharges[0].amount.gross.value
);
expect(basket.totals.bucketSurchargeTotalsByType[0].amount.value).toBe(
expect(basket.totals.bucketSurchargeTotalsByType[0].amount.gross).toBe(
basketData.data.surcharges.bucketSurcharges[0].amount.gross.value
);
expect(basket.totals.isEstimated).toBeTrue();
Expand Down Expand Up @@ -226,7 +226,7 @@ describe('Basket Mapper', () => {
it('should return discounts if included', () => {
basket = BasketMapper.fromData(basketData);

expect(basket.totals.valueRebates[0].amount.value).toBePositive();
expect(basket.totals.valueRebates[0].amount.gross).toBePositive();
});

it('should return estimated as false if invoive address, shipping address and shipping method is set', () => {
Expand All @@ -249,9 +249,9 @@ describe('Basket Mapper', () => {
totals = BasketMapper.getTotals(basketBaseData);
expect(totals).toBeTruthy();

expect(totals.total.value).toBe(basketData.data.totals.grandTotal.gross.value);
expect(totals.itemTotal.value).toBe(basketData.data.totals.itemTotal.gross.value);
expect(totals.itemSurchargeTotalsByType[0].amount.value).toBe(
expect(totals.total.gross).toBe(basketData.data.totals.grandTotal.gross.value);
expect(totals.itemTotal.gross).toBe(basketData.data.totals.itemTotal.gross.value);
expect(totals.itemSurchargeTotalsByType[0].amount.gross).toBe(
basketData.data.surcharges.itemSurcharges[0].amount.gross.value
);
expect(totals.isEstimated).toBeFalse();
Expand All @@ -261,7 +261,7 @@ describe('Basket Mapper', () => {
totals = BasketMapper.getTotals(basketBaseData, basketIncludedData.discounts);
expect(totals).toBeTruthy();

expect(totals.valueRebates[0].amount.value).toBePositive();
expect(totals.valueRebates[0].amount.gross).toBePositive();
expect(totals.valueRebates[0].id).toEqual('discount_1');
});
});
Expand Down
30 changes: 15 additions & 15 deletions src/app/core/models/basket/basket.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BasketTotal } from 'ish-core/models/basket-total/basket-total.model';
import { BasketBaseData, BasketData } from 'ish-core/models/basket/basket.interface';
import { LineItemMapper } from 'ish-core/models/line-item/line-item.mapper';
import { PaymentMapper } from 'ish-core/models/payment/payment.mapper';
import { PriceMapper } from 'ish-core/models/price/price.mapper';
import { PriceItemMapper } from 'ish-core/models/price-item/price-item.mapper';
import { ShippingMethodMapper } from 'ish-core/models/shipping-method/shipping-method.mapper';

import { Basket } from './basket.model';
Expand Down Expand Up @@ -73,24 +73,24 @@ export class BasketMapper {

return totalsData
? {
itemTotal: PriceMapper.fromPriceItem(totalsData.itemTotal),
undiscountedItemTotal: PriceMapper.fromPriceItem(totalsData.undiscountedItemTotal),
shippingTotal: PriceMapper.fromPriceItem(totalsData.shippingTotal),
undiscountedShippingTotal: PriceMapper.fromPriceItem(totalsData.undiscountedShippingTotal),
paymentCostsTotal: PriceMapper.fromPriceItem(totalsData.paymentCostsTotal),
dutiesAndSurchargesTotal: PriceMapper.fromPriceItem(totalsData.surchargeTotal),
taxTotal: { ...totalsData.grandTotal.tax, type: 'Money' },
total: PriceMapper.fromPriceItem(totalsData.grandTotal),
itemTotal: PriceItemMapper.fromPriceItem(totalsData.itemTotal),
undiscountedItemTotal: PriceItemMapper.fromPriceItem(totalsData.undiscountedItemTotal),
shippingTotal: PriceItemMapper.fromPriceItem(totalsData.shippingTotal),
undiscountedShippingTotal: PriceItemMapper.fromPriceItem(totalsData.undiscountedShippingTotal),
paymentCostsTotal: PriceItemMapper.fromPriceItem(totalsData.paymentCostsTotal),
dutiesAndSurchargesTotal: PriceItemMapper.fromPriceItem(totalsData.surchargeTotal),
taxTotal: PriceItemMapper.fromSpecificPriceItem(totalsData.grandTotal, 'tax'),
total: PriceItemMapper.fromPriceItem(totalsData.grandTotal),

itemRebatesTotal: PriceMapper.fromPriceItem(totalsData.itemValueDiscountsTotal),
valueRebatesTotal: PriceMapper.fromPriceItem(totalsData.basketValueDiscountsTotal),
itemRebatesTotal: PriceItemMapper.fromPriceItem(totalsData.itemValueDiscountsTotal),
valueRebatesTotal: PriceItemMapper.fromPriceItem(totalsData.basketValueDiscountsTotal),
valueRebates:
data.discounts && data.discounts.valueBasedDiscounts && discounts
? data.discounts.valueBasedDiscounts.map(discountId => BasketRebateMapper.fromData(discounts[discountId]))
: undefined,

itemShippingRebatesTotal: PriceMapper.fromPriceItem(totalsData.itemShippingDiscountsTotal),
shippingRebatesTotal: PriceMapper.fromPriceItem(totalsData.basketShippingDiscountsTotal),
itemShippingRebatesTotal: PriceItemMapper.fromPriceItem(totalsData.itemShippingDiscountsTotal),
shippingRebatesTotal: PriceItemMapper.fromPriceItem(totalsData.basketShippingDiscountsTotal),
shippingRebates:
data.discounts && data.discounts.shippingBasedDiscounts && discounts
? data.discounts.shippingBasedDiscounts.map(discountId =>
Expand All @@ -101,15 +101,15 @@ export class BasketMapper {
itemSurchargeTotalsByType:
data.surcharges && data.surcharges.itemSurcharges
? data.surcharges.itemSurcharges.map(surcharge => ({
amount: PriceMapper.fromPriceItem(surcharge.amount),
amount: PriceItemMapper.fromPriceItem(surcharge.amount),
displayName: surcharge.name,
description: surcharge.description,
}))
: undefined,
bucketSurchargeTotalsByType:
data.surcharges && data.surcharges.bucketSurcharges
? data.surcharges.bucketSurcharges.map(surcharge => ({
amount: PriceMapper.fromPriceItem(surcharge.amount),
amount: PriceItemMapper.fromPriceItem(surcharge.amount),
displayName: surcharge.name,
description: surcharge.description,
}))
Expand Down
22 changes: 11 additions & 11 deletions src/app/core/models/line-item/line-item.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PriceItem } from 'ish-core/models/price-item/price-item.interface';
import { Price } from 'ish-core/models/price/price.model';
import { PriceItemData } from 'ish-core/models/price-item/price-item.interface';
import { PriceData } from 'ish-core/models/price/price.interface';

export interface LineItemData {
id: string;
Expand All @@ -13,21 +13,21 @@ export interface LineItemData {

surcharges?: [
{
amount: PriceItem;
amount: PriceItemData;
description?: string;
name?: string;
}
];
discounts?: string[];
pricing: {
salesTaxTotal?: Price;
shippingTaxTotal?: Price;
shippingTotal: PriceItem;
total: PriceItem;
valueRebatesTotal?: PriceItem;
price: PriceItem;
undiscountedPrice: PriceItem;
singleBasePrice: PriceItem;
salesTaxTotal?: PriceData;
shippingTaxTotal?: PriceData;
shippingTotal: PriceItemData;
total: PriceItemData;
valueRebatesTotal?: PriceItemData;
price: PriceItemData;
undiscountedPrice: PriceItemData;
singleBasePrice: PriceItemData;
};
hiddenGift: boolean;
freeGift: boolean;
Expand Down
19 changes: 10 additions & 9 deletions src/app/core/models/line-item/line-item.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BasketRebateData } from 'ish-core/models/basket-rebate/basket-rebate.interface';
import { BasketRebateMapper } from 'ish-core/models/basket-rebate/basket-rebate.mapper';
import { OrderItemData } from 'ish-core/models/order-item/order-item.interface';
import { PriceItemMapper } from 'ish-core/models/price-item/price-item.mapper';
import { PriceMapper } from 'ish-core/models/price/price.mapper';

import { LineItemData } from './line-item.interface';
Expand All @@ -13,11 +14,11 @@ export class LineItemMapper {
id: data.id,
position: data.position,
quantity: data.quantity,
price: data.pricing ? PriceMapper.fromPriceItem(data.pricing.price) : undefined,
singleBasePrice: data.pricing ? PriceMapper.fromPriceItem(data.pricing.singleBasePrice) : undefined,
price: data.pricing ? PriceItemMapper.fromPriceItem(data.pricing.price) : undefined,
singleBasePrice: data.pricing ? PriceItemMapper.fromPriceItem(data.pricing.singleBasePrice) : undefined,
itemSurcharges: data.surcharges
? data.surcharges.map(surcharge => ({
amount: PriceMapper.fromPriceItem(surcharge.amount),
amount: PriceItemMapper.fromPriceItem(surcharge.amount),
description: surcharge.description,
displayName: surcharge.name,
}))
Expand All @@ -31,12 +32,12 @@ export class LineItemMapper {
totals:
data.calculated || data.calculated === undefined
? {
salesTaxTotal: data.pricing.salesTaxTotal,
shippingTaxTotal: data.pricing.shippingTaxTotal,
shippingTotal: PriceMapper.fromPriceItem(data.pricing.shippingTotal),
total: PriceMapper.fromPriceItem(data.pricing.price),
undiscountedTotal: PriceMapper.fromPriceItem(data.pricing.undiscountedPrice),
valueRebatesTotal: PriceMapper.fromPriceItem(data.pricing.valueRebatesTotal),
salesTaxTotal: PriceMapper.fromData(data.pricing.salesTaxTotal),
shippingTaxTotal: PriceMapper.fromData(data.pricing.shippingTaxTotal),
shippingTotal: PriceItemMapper.fromPriceItem(data.pricing.shippingTotal),
total: PriceItemMapper.fromPriceItem(data.pricing.price),
undiscountedTotal: PriceItemMapper.fromPriceItem(data.pricing.undiscountedPrice),
valueRebatesTotal: PriceItemMapper.fromPriceItem(data.pricing.valueRebatesTotal),
}
: undefined,

Expand Down
Loading

0 comments on commit e5ea457

Please sign in to comment.