Skip to content

Commit

Permalink
refactor: all related view entities return ProductViews instead of Pr…
Browse files Browse the repository at this point in the history
…oducts
  • Loading branch information
dhhyi committed Feb 28, 2020
1 parent ccfc51e commit ecaf2c5
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 49 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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 QuoteRequestItem {
id?: string;
Expand All @@ -24,5 +24,5 @@ export interface QuoteRequestItem {
}

export interface QuoteRequestItemView extends QuoteRequestItem {
product: Product;
product: ProductView;
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,23 @@ describe('Quote Request Selectors', () => {
});

it('should set "selected" to selected quote item id and set selected quote request', () => {
const expected = {
id: 'test',
state: 'New',
items: [
{
productSKU: 'test',
product: { sku: 'test' } as Product,
},
],
};

expect(getSelectedQuoteRequestId(store$.state)).toEqual('test');
expect(getSelectedQuoteRequestWithProducts(store$.state)).toEqual(expected);
expect(getSelectedQuoteRequestWithProducts(store$.state)).toMatchInlineSnapshot(`
Object {
"id": "test",
"items": Array [
Object {
"product": Object {
"attributes": Array [],
"defaultCategory": [Function],
"sku": "test",
},
"productSKU": "test",
},
],
"state": "New",
}
`);
});
});

Expand Down Expand Up @@ -154,7 +158,7 @@ describe('Quote Request Selectors', () => {
expect(activeQuoteRequest).toBeTruthy();
const items = activeQuoteRequest.items;
expect(items).toHaveLength(1);
expect(items[0]).toHaveProperty('product', { sku: 'test' });
expect(items[0]).toHaveProperty('product.sku', 'test');
});
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createSelector, createSelectorFactory, defaultMemoize } from '@ngrx/store';
import { isEqual } from 'lodash-es';

import { createProductView } from 'ish-core/models/product-view/product-view.model';
import { getCategoryTree } from 'ish-core/store/shopping/categories';
import { getProductEntities } from 'ish-core/store/shopping/products';

import { QuoteRequestHelper } from '../../models/quote-request/quote-request.helper';
Expand Down Expand Up @@ -40,10 +42,11 @@ export const getQuoteRequestItems = createSelector(
export const getQuoteRequestItemsWithProducts = createSelector(
getQuoteRequestItems,
getProductEntities,
(items, products) =>
getCategoryTree,
(items, products, categoryTree) =>
items.map(item => ({
...item,
product: item.productSKU ? products[item.productSKU] : undefined,
product: item.productSKU ? createProductView(products[item.productSKU], categoryTree) : undefined,
}))
);

Expand Down
4 changes: 2 additions & 2 deletions src/app/pages/compare/compare-page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';

import { ShoppingFacade } from 'ish-core/facades/shopping.facade';
import { AllProductTypes } from 'ish-core/models/product/product.model';
import { ProductView } from 'ish-core/models/product-view/product-view.model';

@Component({
selector: 'ish-compare-page',
templateUrl: './compare-page.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ComparePageComponent implements OnInit {
compareProducts$: Observable<AllProductTypes[]>;
compareProducts$: Observable<ProductView[]>;
compareProductsCount$: Observable<number>;

constructor(private shoppingFacade: ShoppingFacade) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { MockComponent, MockPipe } from 'ng-mocks';

import { FeatureToggleModule } from 'ish-core/feature-toggle.module';
import { AttributeToStringPipe } from 'ish-core/models/attribute/attribute.pipe';
import { ProductView, createProductView } from 'ish-core/models/product-view/product-view.model';
import { Product } from 'ish-core/models/product/product.model';
import { ProductRoutePipe } from 'ish-core/routing/product/product-route.pipe';
import { configurationReducer } from 'ish-core/store/configuration/configuration.reducer';
import { ngrxTesting } from 'ish-core/utils/dev/ngrx-testing';
import { categoryTree } from 'ish-core/utils/dev/test-data-utils';
import { ProductAddToBasketComponent } from 'ish-shared/components/product/product-add-to-basket/product-add-to-basket.component';
import { ProductAttributesComponent } from 'ish-shared/components/product/product-attributes/product-attributes.component';
import { ProductIdComponent } from 'ish-shared/components/product/product-id/product-id.component';
Expand All @@ -27,8 +29,8 @@ describe('Product Compare List Component', () => {
let component: ProductCompareListComponent;
let element: HTMLElement;
let translate: TranslateService;
let compareProduct1: Product;
let compareProduct2: Product;
let compareProduct1: ProductView;
let compareProduct2: ProductView;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
Expand Down Expand Up @@ -62,7 +64,7 @@ describe('Product Compare List Component', () => {
translate.setDefaultLang('en');
translate.use('en');
element = fixture.nativeElement;
compareProduct1 = { sku: '111', inStock: true, availability: true } as Product;
compareProduct1 = createProductView({ sku: '111', inStock: true, availability: true } as Product, categoryTree());
compareProduct1.attributes = [
{
name: 'Optical zoom',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnChanges, Output } from '@angular/core';

import { AttributeHelper } from 'ish-core/models/attribute/attribute.helper';
import { Product, ProductHelper } from 'ish-core/models/product/product.model';
import { ProductView } from 'ish-core/models/product-view/product-view.model';
import { ProductHelper } from 'ish-core/models/product/product.model';

/**
* The Product Compare List Component
Expand All @@ -24,7 +25,7 @@ export class ProductCompareListComponent implements OnChanges {
/**
* The list of products to compare
*/
@Input() compareProducts: Product[] = [];
@Input() compareProducts: ProductView[] = [];

/**
* The maximum number of products to be compared on one page
Expand All @@ -42,7 +43,7 @@ export class ProductCompareListComponent implements OnChanges {
@Output() removeProductCompare = new EventEmitter<string>();

commonAttributeNames: string[];
visibleProducts: Product[] = [];
visibleProducts: ProductView[] = [];
currentPage = 1;

getAttributeByAttributeName = AttributeHelper.getAttributeByAttributeName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { TranslateModule } from '@ngx-translate/core';
import { MockComponent, MockPipe } from 'ng-mocks';

import { PricePipe } from 'ish-core/models/price/price.pipe';
import { createProductView } from 'ish-core/models/product-view/product-view.model';
import { Product } from 'ish-core/models/product/product.model';
import { ProductRoutePipe } from 'ish-core/routing/product/product-route.pipe';
import { categoryTree } from 'ish-core/utils/dev/test-data-utils';
import { ProductInventoryComponent } from 'ish-shared/components/product/product-inventory/product-inventory.component';
import { ProductImageComponent } from 'ish-shell/header/product-image/product-image.component';

Expand Down Expand Up @@ -47,7 +49,9 @@ describe('Basket Validation Products Component', () => {
});

it('should display an validation product if there is a validation product', () => {
component.items = [{ message: 'validation message', product: { sku: '4713' } as Product }];
component.items = [
{ message: 'validation message', product: createProductView({ sku: '4713' } as Product, categoryTree()) },
];

fixture.detectChanges();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';

import { Product } from 'ish-core/models/product/product.model';
import { ProductView } from 'ish-core/models/product-view/product-view.model';

/**
* Displays the products that are not valid any more after basket validation and that has been removed from basket
Expand All @@ -14,5 +14,5 @@ import { Product } from 'ish-core/models/product/product.model';
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class BasketValidationProductsComponent {
@Input() items: { message: string; product: Product }[];
@Input() items: { message: string; product: ProductView }[];
}
Loading

0 comments on commit ecaf2c5

Please sign in to comment.