From 1c03b76465fe25d3ae74aa083bd36b7ec570a113 Mon Sep 17 00:00:00 2001 From: Danilo Hoffmann Date: Fri, 14 Feb 2020 16:59:43 +0100 Subject: [PATCH] refactor: all related view entities return ProductViews instead of Products --- .../basket-feedback/basket-feedback.model.ts | 4 +-- src/app/core/models/basket/basket.model.ts | 5 ++-- .../core/models/line-item/line-item.model.ts | 4 +-- src/app/core/models/order/order.model.ts | 5 ++-- .../store/checkout/basket/basket.selectors.ts | 17 +++++++---- .../store/orders/orders.selectors.spec.ts | 4 +-- src/app/core/store/orders/orders.selectors.ts | 14 ++++++--- .../quote-request-item.model.ts | 4 +-- .../quote-request.selectors.spec.ts | 30 +++++++++++-------- .../quote-request/quote-request.selectors.ts | 7 +++-- .../pages/compare/compare-page.component.ts | 4 +-- .../product-compare-list.component.spec.ts | 8 +++-- .../product-compare-list.component.ts | 7 +++-- ...sket-validation-products.component.spec.ts | 6 +++- .../basket-validation-products.component.ts | 4 +-- .../basket-validation-results.component.ts | 4 +-- 16 files changed, 78 insertions(+), 49 deletions(-) diff --git a/src/app/core/models/basket-feedback/basket-feedback.model.ts b/src/app/core/models/basket-feedback/basket-feedback.model.ts index efbcbda7d5f..bf033f375f6 100644 --- a/src/app/core/models/basket-feedback/basket-feedback.model.ts +++ b/src/app/core/models/basket-feedback/basket-feedback.model.ts @@ -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; @@ -13,6 +13,6 @@ export interface BasketFeedback { } export interface BasketFeedbackView extends BasketFeedback { - product?: Product; + product?: ProductView; lineItem?: LineItem; } diff --git a/src/app/core/models/basket/basket.model.ts b/src/app/core/models/basket/basket.model.ts index 790900611fd..e83cb018379 100644 --- a/src/app/core/models/basket/basket.model.ts +++ b/src/app/core/models/basket/basket.model.ts @@ -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'; @@ -33,7 +34,7 @@ export interface Basket extends AbstractBasket {} export interface BasketView extends AbstractBasket {} export const createBasketView = memoize( - (basket, products, validationResults, basketInfo): BasketView => { + (basket, products, validationResults, basketInfo, categoryTree): BasketView => { if (!basket) { return; } @@ -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, diff --git a/src/app/core/models/line-item/line-item.model.ts b/src/app/core/models/line-item/line-item.model.ts index 63478430723..297761c6ff8 100644 --- a/src/app/core/models/line-item/line-item.model.ts +++ b/src/app/core/models/line-item/line-item.model.ts @@ -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; @@ -45,7 +45,7 @@ export interface LineItem { } export interface LineItemView extends LineItem { - product: Product; + product: ProductView; validationError?: BasketFeedback; info?: BasketFeedback; } diff --git a/src/app/core/models/order/order.model.ts b/src/app/core/models/order/order.model.ts index c7e011d92bd..0f4e7b302b3 100644 --- a/src/app/core/models/order/order.model.ts +++ b/src/app/core/models/order/order.model.ts @@ -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'; @@ -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; } @@ -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, })) : [], }; diff --git a/src/app/core/store/checkout/basket/basket.selectors.ts b/src/app/core/store/checkout/basket/basket.selectors.ts index 4e7ded81923..8dd3f480633 100644 --- a/src/app/core/store/checkout/basket/basket.selectors.ts +++ b/src/app/core/store/checkout/basket/basket.selectors.ts @@ -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'; @@ -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; } @@ -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 @@ -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 + ), })) : [], }; @@ -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( diff --git a/src/app/core/store/orders/orders.selectors.spec.ts b/src/app/core/store/orders/orders.selectors.spec.ts index 5c9950b9f4f..5c6cb5630d8 100644 --- a/src/app/core/store/orders/orders.selectors.spec.ts +++ b/src/app/core/store/orders/orders.selectors.spec.ts @@ -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'); }); }); @@ -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'); }); }); diff --git a/src/app/core/store/orders/orders.selectors.ts b/src/app/core/store/orders/orders.selectors.ts index f9297fbf10f..440fe3cbbca 100644 --- a/src/app/core/store/orders/orders.selectors.ts +++ b/src/app/core/store/orders/orders.selectors.ts @@ -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'; @@ -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( diff --git a/src/app/extensions/quoting/models/quote-request-item/quote-request-item.model.ts b/src/app/extensions/quoting/models/quote-request-item/quote-request-item.model.ts index 986ca150ea4..857c1430adf 100644 --- a/src/app/extensions/quoting/models/quote-request-item/quote-request-item.model.ts +++ b/src/app/extensions/quoting/models/quote-request-item/quote-request-item.model.ts @@ -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; @@ -24,5 +24,5 @@ export interface QuoteRequestItem { } export interface QuoteRequestItemView extends QuoteRequestItem { - product: Product; + product: ProductView; } diff --git a/src/app/extensions/quoting/store/quote-request/quote-request.selectors.spec.ts b/src/app/extensions/quoting/store/quote-request/quote-request.selectors.spec.ts index 9dc9dd24d58..e542cd7583c 100644 --- a/src/app/extensions/quoting/store/quote-request/quote-request.selectors.spec.ts +++ b/src/app/extensions/quoting/store/quote-request/quote-request.selectors.spec.ts @@ -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", + } + `); }); }); @@ -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'); }); }); }); diff --git a/src/app/extensions/quoting/store/quote-request/quote-request.selectors.ts b/src/app/extensions/quoting/store/quote-request/quote-request.selectors.ts index f38b6062719..4eab4e3d147 100644 --- a/src/app/extensions/quoting/store/quote-request/quote-request.selectors.ts +++ b/src/app/extensions/quoting/store/quote-request/quote-request.selectors.ts @@ -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'; @@ -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, })) ); diff --git a/src/app/pages/compare/compare-page.component.ts b/src/app/pages/compare/compare-page.component.ts index 0067d117e91..f19a5565ea7 100644 --- a/src/app/pages/compare/compare-page.component.ts +++ b/src/app/pages/compare/compare-page.component.ts @@ -2,7 +2,7 @@ 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', @@ -10,7 +10,7 @@ import { AllProductTypes } from 'ish-core/models/product/product.model'; changeDetection: ChangeDetectionStrategy.OnPush, }) export class ComparePageComponent implements OnInit { - compareProducts$: Observable; + compareProducts$: Observable; compareProductsCount$: Observable; constructor(private shoppingFacade: ShoppingFacade) {} diff --git a/src/app/pages/compare/product-compare-list/product-compare-list.component.spec.ts b/src/app/pages/compare/product-compare-list/product-compare-list.component.spec.ts index a03d8832cd7..7947465b0ca 100644 --- a/src/app/pages/compare/product-compare-list/product-compare-list.component.spec.ts +++ b/src/app/pages/compare/product-compare-list/product-compare-list.component.spec.ts @@ -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'; @@ -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: [ @@ -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', diff --git a/src/app/pages/compare/product-compare-list/product-compare-list.component.ts b/src/app/pages/compare/product-compare-list/product-compare-list.component.ts index 177d6da013e..2068afc4d44 100644 --- a/src/app/pages/compare/product-compare-list/product-compare-list.component.ts +++ b/src/app/pages/compare/product-compare-list/product-compare-list.component.ts @@ -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 @@ -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 @@ -42,7 +43,7 @@ export class ProductCompareListComponent implements OnChanges { @Output() removeProductCompare = new EventEmitter(); commonAttributeNames: string[]; - visibleProducts: Product[] = []; + visibleProducts: ProductView[] = []; currentPage = 1; getAttributeByAttributeName = AttributeHelper.getAttributeByAttributeName; diff --git a/src/app/shared/components/basket/basket-validation-products/basket-validation-products.component.spec.ts b/src/app/shared/components/basket/basket-validation-products/basket-validation-products.component.spec.ts index e552de1774f..acd4c18f225 100644 --- a/src/app/shared/components/basket/basket-validation-products/basket-validation-products.component.spec.ts +++ b/src/app/shared/components/basket/basket-validation-products/basket-validation-products.component.spec.ts @@ -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'; @@ -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(); diff --git a/src/app/shared/components/basket/basket-validation-products/basket-validation-products.component.ts b/src/app/shared/components/basket/basket-validation-products/basket-validation-products.component.ts index b3c8c969cc6..82f41377428 100644 --- a/src/app/shared/components/basket/basket-validation-products/basket-validation-products.component.ts +++ b/src/app/shared/components/basket/basket-validation-products/basket-validation-products.component.ts @@ -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 @@ -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 }[]; } diff --git a/src/app/shared/components/basket/basket-validation-results/basket-validation-results.component.ts b/src/app/shared/components/basket/basket-validation-results/basket-validation-results.component.ts index d72018992a5..a1c1bbb40d0 100644 --- a/src/app/shared/components/basket/basket-validation-results/basket-validation-results.component.ts +++ b/src/app/shared/components/basket/basket-validation-results/basket-validation-results.component.ts @@ -7,7 +7,7 @@ import { CheckoutFacade } from 'ish-core/facades/checkout.facade'; import { BasketFeedback } from 'ish-core/models/basket-feedback/basket-feedback.model'; import { BasketValidationResultType } from 'ish-core/models/basket-validation/basket-validation.model'; import { LineItemView } 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'; /** * Displays the basket validation result messages. In case of basket adjustments removed or undeliverable items are @@ -28,7 +28,7 @@ export class BasketValidationResultsComponent implements OnInit, OnDestroy { errorMessages$: Observable; infoMessages$: Observable; undeliverableItems$: Observable; - removedItems$: Observable<{ message: string; product: Product }[]>; + removedItems$: Observable<{ message: string; product: ProductView }[]>; itemHasBeenRemoved = false;