import { AddItemToCart } from '../actions/global-cart-add-item-data/global-cart-add-item-data.actions'; import { Injectable } from '@angular/core'; import { select, Store } from '@ngrx/store'; import { State } from '../../../reducers'; import { GlobalCartDataState } from '../reducers/global-cart-data/global-cart-data.reducer'; import { selectGlobalCartDataState } from '../selectors/global-cart-data.selectors'; import { combineLatest, Observable } from 'rxjs'; import { ApiParamInfoService, FULFILLMENT_OPTIONS } from '@hdca/fe-angular-shared'; import { GlobalCartAddItemDataState } from '../reducers/global-cart-add-item-data/global-cart-add-item-data.reducer'; import { GlobalCartAddFbtDataState } from '../reducers/global-cart-add-item-data/global-cart-add-fbt-data.reducer'; import { selectGlobalCartAddItemDataState, selectGlobalCartAddFbtDataState } from '../selectors/global-cart-add-item-data.selectors'; import { distinctUntilChanged, map, filter } from 'rxjs/operators'; import { FulfillmentMessages, Bopis } from '../../../models/product-localized-data.interface'; import { LanguageService } from '@hdca/fe-angular-shared'; import { selectGlobalHeaderLocalizePickUpStoreId } from '../../global-header/selectors/global-header-localize.selectors'; import { selectRoutingProductId } from '../../global-header/selectors/global-routing.selectors'; // import { ShowStoreSelectorsProduct } from '@app/core/modules/store-selectors/actions/store-selectors-ui/store-selectors-ui.actions'; import { PRODUCT_UI_CONSTANTS } from '../constants/ui.constants'; import { GlobalCartAddApplianceItemDataState } from '../reducers/global-cart-add-appliance-item-data/global-cart-add-appliance-item-data.reducer'; import { selectGlobalCartAddApplianceItemDataState } from '../selectors/global-cart-add-appliance-item-data.selectors'; import { GlobalCartPartsAndServicesDataState } from '../reducers/global-cart-parts-and-services-data/global-cart-parts-and-services-data.reducer'; import { selectGlobalCartPartsAndServicesDataState } from '../selectors/global-cart-parts-and-services-data.selectors'; import { IRecommendationItem } from '../models/recommendations.interface'; import { selectGlobalCartRecommendationsState } from '../selectors/global-cart-recommendations.selectors'; // Event Transceiver import { AddToCartInfoBlock } from '../../event-transceiver/models/adobe-analytics-data-layer-models.interface'; import { shippingMethodTypes, productInteractionLocationTypes } from '../../event-transceiver/constants/adobe-analytics.constants'; import { of } from 'rxjs'; export interface GlobalCartAddItemCombinedData { productId: string; qty: number; selected: string; postalCode: string; pickUpStoreId: string; } interface ProductLocalizedFulfillmentUIState { selected: string; qty: number; postalCode: string; } const initialProductLocalizedFulfillmentUIState: ProductLocalizedFulfillmentUIState = { selected: undefined, qty: 1, postalCode: undefined }; @Injectable() export class GlobalCartService { productLocalizedFulfillmentUIState$: Observable = of(initialProductLocalizedFulfillmentUIState); constructor( private store: Store, private apiParam: ApiParamInfoService, private languageService: LanguageService ) {} setProductLocalizedFulfillmentUIState(fullFillmentObservable$: Observable) { this.productLocalizedFulfillmentUIState$ = fullFillmentObservable$; } // Method to get the current cart state getGlobalCartDataState$(): Observable { return this.store.pipe( select(selectGlobalCartDataState), distinctUntilChanged(), filter(state => !!state.type || !!state.error) ); } // Method to get the last item added state getGlobalCartAddItemState$(): Observable { return this.store.pipe( select(selectGlobalCartAddItemDataState), distinctUntilChanged() ); } // Method to get the last APPLIANCE item added state getGlobalCartAddApplianceItemState$(): Observable { return this.store.pipe( select(selectGlobalCartAddApplianceItemDataState), distinctUntilChanged() ); } // Method to get the parts and services state getGlobalCartPartsAndServicesState$(): Observable { return this.store.pipe( select(selectGlobalCartPartsAndServicesDataState), distinctUntilChanged() ); } // Method to get the last ITEMS added state (for FBT) getGlobalCartAddFbtState$(): Observable { return this.store.pipe( select(selectGlobalCartAddFbtDataState), distinctUntilChanged() ); } getUser(): string { return this.apiParam.getApiParamInfo().customerId; } getCartId(): string { return this.apiParam.getApiParamInfo().cartId; } getLocalizedPOS(): string { return this.apiParam.getApiParamInfo().localizedPOS; } /** * Method to return the formed string of type 'application/x-www-form-urlencoded' for add to card API based on input * @param code - Product id * @param qty - Product qty, defaults to 1 if not set * @param selectedFulfillment - This is the fulfillment option for which the product is being added as. If not passed, only code and qty will appear in the returned object. Not required for shipToHome items. * @param pickupStore - Required in the case where bopis or boss * @param postalCode - Required only for bodfs */ getFormedAddToCartParams(code: string, qty = 1, selectedFulfillment?: string, pickupStore?: string, postalCode?: string, isOnlineOrStore?: string) { let formedString = ''; switch (selectedFulfillment) { case FULFILLMENT_OPTIONS.SHIP_TO_HOME: { if (isOnlineOrStore && isOnlineOrStore === 'Store') { formedString = 'code=' + code + '&qty=' + qty + '&pickupStore=' + pickupStore; } else { formedString = 'code=' + code + '&qty=' + qty; } break; } case FULFILLMENT_OPTIONS.BOPIS: { formedString = 'code=' + code + '&qty=' + qty + '&pickupStore=' + pickupStore; break; } case FULFILLMENT_OPTIONS.BOSS: { formedString = 'code=' + code + '&qty=' + qty + '&boss=true' + '&pickupStore=' + pickupStore; break; } case FULFILLMENT_OPTIONS.BODFS: { formedString = 'code=' + code + '&qty=' + qty + '&bodfs=true' + '&postalCode=' + postalCode; break; } default: { if (isOnlineOrStore && isOnlineOrStore === 'Store') { formedString = 'code=' + code + '&qty=' + qty + '&pickupStore=' + pickupStore; } else { formedString = 'code=' + code + '&qty=' + qty; } break; } } return formedString; } public isVisibleButtonAddToCart( fulfillmentMessages?: FulfillmentMessages ): boolean { return ( fulfillmentMessages && fulfillmentMessages.availableMessages && fulfillmentMessages.availableMessages.addToCart ); } public isEnabledButtonAddToCart( fulfillmentMessages?: FulfillmentMessages ): boolean { return ( this.isVisibleButtonAddToCart(fulfillmentMessages) && fulfillmentMessages.addToCart.toLowerCase() === 'enabled' ); } public getProductLocalizedFulfillmentUIState$(): Observable { return this.productLocalizedFulfillmentUIState$; } public getPickUpStoreId$(): Observable { return this.store.pipe( select(selectGlobalHeaderLocalizePickUpStoreId), distinctUntilChanged() ); } public getCurrentProductId$(): Observable { return this.store.pipe( select(selectRoutingProductId), distinctUntilChanged() ); } public getRecommendations$(): Observable { return this.store.pipe( select(selectGlobalCartRecommendationsState) ); } public getAddItemParams$(): Observable { return combineLatest(this.getProductLocalizedFulfillmentUIState$, this.getPickUpStoreId$(), this.getCurrentProductId$()).pipe( map(combined => { return { ...combined[0], pickUpStoreId: combined[1], productId: combined[2] }; }) ); } /** * Add to cart functionality * @param productCode - Product Code * @param isStoreRequired - flag * @param slot - Slot of product [mostly applicable on carousels, otherwise its 1] * @param origin - Origin of event i.e certona, primary etc. * @param bopis - flag [optional] */ public addItemToCart$( price: number, productCode: string, isStoreRequired = true, slot: number, origin: productInteractionLocationTypes, bopis?: Bopis, isOnlineOrStore?: string, ): Observable { const addToCart = (storeId: string, qty: number, selected: string, postalCode: string) => { const user: string = this.getUser(); const cartId: string = this.getCartId(); const lang: string = this.languageService.getLanguage(); const params: string = this.getFormedAddToCartParams( productCode, qty, selected, storeId, postalCode, isOnlineOrStore ); if ( isStoreRequired && ((selected === FULFILLMENT_OPTIONS.BOPIS && bopis.messageTranslKey === PRODUCT_UI_CONSTANTS.PRODUCT_OUT_OF_STOCK_AT_KEY) || storeId === '9999') ) { // this.store.dispatch(new ShowStoreSelectorsProduct()); } else { const analyticsData: AddToCartInfoBlock = { product: { code: productCode }, itemQuantity: qty, isCartSwitch: false, displayPrice: price, displayShippingMethod: shippingMethodTypes.shipToHome, productInteractionLocation: origin, slotLocation: slot }; this.store.dispatch( new AddItemToCart(user, cartId, lang, storeId, params, [analyticsData]) ); } }; return combineLatest( this.getProductLocalizedFulfillmentUIState$(), this.getPickUpStoreId$(), (option, storeId) => { addToCart(storeId, option.qty, option.selected, option.postalCode); } ); } }