Skip to content

Commit

Permalink
refactor: all checkout related view entities return ProductViews inst…
Browse files Browse the repository at this point in the history
…ead of Products
  • Loading branch information
dhhyi committed Feb 15, 2020
1 parent a61104e commit 8936f60
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/app/core/models/basket-feedback/basket-feedback.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LineItem } from 'ish-core/models/line-item/line-item.model';
import { Product } from 'ish-core/models/product/product.model';
import { ProductView } from 'ish-core/models/product-view/product-view.model';

export interface BasketFeedback {
code: string;
Expand All @@ -13,6 +13,6 @@ export interface BasketFeedback {
}

export interface BasketFeedbackView extends BasketFeedback {
product?: Product;
product?: ProductView;
lineItem?: LineItem;
}
5 changes: 3 additions & 2 deletions src/app/core/models/basket/basket.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BasketTotal } from 'ish-core/models/basket-total/basket-total.model';
import { BasketValidationResultType } from 'ish-core/models/basket-validation/basket-validation.model';
import { LineItem, LineItemView } from 'ish-core/models/line-item/line-item.model';
import { Payment } from 'ish-core/models/payment/payment.model';
import { createProductView } from 'ish-core/models/product-view/product-view.model';
import { VariationProductMaster } from 'ish-core/models/product/product-variation-master.model';
import { VariationProduct } from 'ish-core/models/product/product-variation.model';
import { Product } from 'ish-core/models/product/product.model';
Expand All @@ -33,7 +34,7 @@ export interface Basket extends AbstractBasket<LineItem> {}
export interface BasketView extends AbstractBasket<LineItemView> {}

export const createBasketView = memoize(
(basket, products, validationResults, basketInfo): BasketView => {
(basket, products, validationResults, basketInfo, categoryTree): BasketView => {
if (!basket) {
return;
}
Expand All @@ -43,7 +44,7 @@ export const createBasketView = memoize(
lineItems: basket.lineItems
? basket.lineItems.map(li => ({
...li,
product: products[li.productSKU],
product: createProductView(products[li.productSKU], categoryTree),
name: products && products[li.productSKU] ? products[li.productSKU].name : undefined,
inStock: products && products[li.productSKU] ? products[li.productSKU].inStock : undefined,
availability: products && products[li.productSKU] ? products[li.productSKU].availability : undefined,
Expand Down
4 changes: 2 additions & 2 deletions src/app/core/models/line-item/line-item.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BasketFeedback } from 'ish-core/models/basket-feedback/basket-feedback.model';
import { BasketRebate } from 'ish-core/models/basket-rebate/basket-rebate.model';
import { Price } from 'ish-core/models/price/price.model';
import { Product } from 'ish-core/models/product/product.model';
import { ProductView } from 'ish-core/models/product-view/product-view.model';

export interface LineItem {
id: string;
Expand Down Expand Up @@ -45,7 +45,7 @@ export interface LineItem {
}

export interface LineItemView extends LineItem {
product: Product;
product: ProductView;
validationError?: BasketFeedback;
info?: BasketFeedback;
}
5 changes: 3 additions & 2 deletions src/app/core/models/order/order.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Dictionary } from '@ngrx/entity';
import { memoize } from 'lodash-es';

import { Basket, BasketView } from 'ish-core/models/basket/basket.model';
import { createProductView } from 'ish-core/models/product-view/product-view.model';
import { VariationProductMaster } from 'ish-core/models/product/product-variation-master.model';
import { VariationProduct } from 'ish-core/models/product/product-variation.model';
import { Product } from 'ish-core/models/product/product.model';
Expand All @@ -26,7 +27,7 @@ export interface Order extends Basket, AbstractOrder {}
export interface OrderView extends BasketView, AbstractOrder {}

export const createOrderView = memoize(
(order, products): OrderView => {
(order, products, categoryTree): OrderView => {
if (!order) {
return;
}
Expand All @@ -36,7 +37,7 @@ export const createOrderView = memoize(
lineItems: order.lineItems
? order.lineItems.map(li => ({
...li,
product: products ? products[li.productSKU] : undefined,
product: products ? createProductView(products[li.productSKU], categoryTree) : undefined,
}))
: [],
};
Expand Down
17 changes: 12 additions & 5 deletions src/app/core/store/checkout/basket/basket.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { isEqual } from 'lodash-es';
import { AddressHelper } from 'ish-core/models/address/address.helper';
import { BasketValidationResultType } from 'ish-core/models/basket-validation/basket-validation.model';
import { BasketView, createBasketView } from 'ish-core/models/basket/basket.model';
import { createProductView } from 'ish-core/models/product-view/product-view.model';
import { getCheckoutState } from 'ish-core/store/checkout/checkout-store';
import { getCategoryTree } from 'ish-core/store/shopping/categories';
import { getProductEntities } from 'ish-core/store/shopping/products';
import { getLoggedInCustomer } from 'ish-core/store/user';

Expand All @@ -16,7 +18,8 @@ const getBasketState = createSelector(
export const getBasketValidationResults = createSelector(
getBasketState,
getProductEntities,
(basket, products): BasketValidationResultType => {
getCategoryTree,
(basket, products, categoryTree): BasketValidationResultType => {
if (!basket || !basket.validationResults) {
return;
}
Expand All @@ -27,7 +30,7 @@ export const getBasketValidationResults = createSelector(
infos: basketResults.infos
? basketResults.infos.map(info => ({
...info,
product: info.parameters && products[info.parameters.productSku],
product: info.parameters && createProductView(products[info.parameters.productSku], categoryTree),
}))
: [],
errors: basketResults.errors
Expand All @@ -41,7 +44,10 @@ export const getBasketValidationResults = createSelector(
error.parameters &&
error.parameters.lineItemId &&
basket.basket.lineItems.find(item => item.id === error.parameters.lineItemId) &&
products[basket.basket.lineItems.find(item => item.id === error.parameters.lineItemId).productSKU],
createProductView(
products[basket.basket.lineItems.find(item => item.id === error.parameters.lineItemId).productSKU],
categoryTree
),
}))
: [],
};
Expand All @@ -61,8 +67,9 @@ export const getCurrentBasket = createSelector(
getProductEntities,
getBasketValidationResults,
getBasketInfo,
(basket, products, validationResults, basketInfo): BasketView =>
createBasketView(basket.basket, products, validationResults, basketInfo)
getCategoryTree,
(basket, products, validationResults, basketInfo, categoryTree): BasketView =>
createBasketView(basket.basket, products, validationResults, basketInfo, categoryTree)
);

export const getCurrentBasketId = createSelector(
Expand Down
4 changes: 2 additions & 2 deletions src/app/core/store/orders/orders.selectors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('Orders Selectors', () => {
expect(loadedOrders[1].documentNo).toEqual(orders[1].documentNo);
expect(loadedOrders[1].lineItems).toHaveLength(1);
expect(loadedOrders[1].lineItems[0].id).toEqual('test2');
expect(loadedOrders[1].lineItems[0].product).toEqual({ sku: 'sku' });
expect(loadedOrders[1].lineItems[0].product).toHaveProperty('sku', 'sku');
});
});

Expand Down Expand Up @@ -146,7 +146,7 @@ describe('Orders Selectors', () => {
expect(loadedOrder.documentNo).toEqual(orders[0].documentNo);
expect(loadedOrder.lineItems).toHaveLength(1);
expect(loadedOrder.lineItems[0].id).toEqual('test');
expect(loadedOrder.lineItems[0].product).toEqual({ sku: 'sku' });
expect(loadedOrder.lineItems[0].product).toHaveProperty('sku', 'sku');
});
});

Expand Down
14 changes: 10 additions & 4 deletions src/app/core/store/orders/orders.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createSelector } from '@ngrx/store';

import { OrderView, createOrderView } from 'ish-core/models/order/order.model';
import { getCoreState } from 'ish-core/store/core-store';
import { getCategoryTree } from 'ish-core/store/shopping/categories';
import { getProductEntities } from 'ish-core/store/shopping/products';

import { orderAdapter } from './orders.reducer';
Expand All @@ -22,20 +23,25 @@ export const getSelectedOrder = createSelector(
getOrderEntities,
getSelectedOrderId,
getProductEntities,
(entities, id, products): OrderView => (id && entities[id] ? createOrderView(entities[id], products) : undefined)
getCategoryTree,
(entities, id, products, categoryTree): OrderView =>
id && entities[id] ? createOrderView(entities[id], products, categoryTree) : undefined
);

export const getOrders = createSelector(
getOrdersInternal,
getProductEntities,
(orders, products): OrderView[] => (!orders ? [] : orders.map(e => createOrderView(e, products)))
getCategoryTree,
(orders, products, categoryTree): OrderView[] =>
!orders ? [] : orders.map(e => createOrderView(e, products, categoryTree))
);

export const getOrder = createSelector(
getOrdersInternal,
getProductEntities,
(entities, products, props: { orderId: string }): OrderView =>
createOrderView(entities.find(e => e.id === props.orderId), products)
getCategoryTree,
(entities, products, categoryTree, props: { orderId: string }): OrderView =>
createOrderView(entities.find(e => e.id === props.orderId), products, categoryTree)
);

export const getOrdersLoading = createSelector(
Expand Down

0 comments on commit 8936f60

Please sign in to comment.