/** * @Component Name: AuthenticateComponent * @Module: Home * @Module Functionality : authenticate the customer orders * @Creation Date: August 12,2017 * @Author: sbaireddy * @Version: 1.0 */ import { Component, Input, OnInit, OnChanges, OnDestroy } from '@angular/core'; import { Router } from '@angular/router'; import Utils from '../../../../shared/utils'; import * as Constants from '../../../../shared/constants'; import * as FormConstants from '../../../../shared/form.constants'; import { IKeyValue } from '../../../../shared/modal/key.value'; import { ORDER_NOTES_REASONS } from '../../../../shared/form.constants'; import { Cart } from '../../../../shared/modal/cart'; import { User } from '../../../../shared/modal/user'; import { Customer, ICreditProfile } from '../../../../shared/modal/customer'; import { SharedService } from '../../../../shared/shared.service'; import { LookupService } from '../../../../services/lookup.service'; import { CartService } from '../../../../services/cart.service'; import { UserService } from '../../../../services/user.service'; import { CustomerService } from '../../../../services/customer.service'; declare var jQuery: any; @Component({ moduleId: module.id, selector: 'authenticate-cmp', templateUrl: 'authenticate.component.html', styleUrls: ['authenticate.component.css'] }) export class AuthenticateComponent implements OnInit, OnDestroy, OnChanges { private COMPONENT_NAME: string = 'AuthenticateComponent'; visible: boolean = false; visibleAnimate: boolean = false; storeId: number; user: User; @Input() cart: Cart; @Input() canRedirectToPayReview: boolean; public isorderlookup: boolean = false; authenticateModel: any = { identifierType: 'PWD', identifier: '' }; type: string = 'password'; constructor( public router: Router, public sharedService: SharedService, public cartService: CartService, public customerService: CustomerService, public lookupService: LookupService, public userService: UserService) { } /** * This function is used to initialize the directive/component after Angular * first displays the data-bound properties.It is invoked only once when the * directive is instantiated. * @return void */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; } /** * This function is called whenever one or more data-bound input properties change * @returns void */ ngOnChanges(): void { const METHOD_NAME: string = 'ngOnChanges()'; } /** * Cleanup just before Angular destroys the directive/component. * Unsubscribe Observables and detach event handlers to avoid memory leaks. * @return {void} */ ngOnDestroy(): void { const METHOD_NAME: string = 'ngOnDestroy()'; this.user = null; this.cart = null; this.authenticateModel = null; } /** * This function used to display the object. * @return void */ public show(): void { const METHOD_NAME: string = 'show()'; this.visible = true; setTimeout(() => this.visibleAnimate = true, 100); this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; }); this.authenticateModel = { identifierType: 'PWD', identifier: '' }; if (this.cart && this.cart.customer) { if (this.cart.customer.creditProfile && this.cart.customer.creditProfile.identifierType) { this.authenticateModel.identifierType = this.cart.customer.creditProfile.identifierType; } } } /** * This function is used to hide the object. * @returns void */ public hide(): void { const METHOD_NAME: string = 'hide()'; this.visibleAnimate = false; setTimeout(() => this.visible = false, 300); } /** * This function is used to hide the object when nouse event performed * @param {MouseEvent} event contains valid mouse event * @returns void */ public onContainerClicked(event: MouseEvent): void { const METHOD_NAME: string = 'onContainerClicked()'; if ((event.target).classList.contains('modal')) { this.hide(); } } /** * This function is used to confirm the authentication. * @returns void */ confirmAuthenticate(): void { const METHOD_NAME: string = 'confirmAuthenticate()'; this.user = this.userService.getUser(); const inputData: any = { agentId: this.user.logonId, subject: FormConstants.ACCOUNT_VERIFICATION }; const creditProfile: ICreditProfile = (this.cart.customer && this.cart.customer.creditProfile) ? this.cart.customer.creditProfile : null; if (creditProfile.accountNumber) { this.sharedService.showSpinner(true); // spinner this.lookupService.lookupCustomer(this.storeId, creditProfile.accountNumber, null, this.authenticateModel.identifier, this.authenticateModel.identifierType, 'FULL' ).subscribe(result => { this.sharedService.showSpinner(false); // spinner const phone1 = (this.cart.customer && this.cart.customer.phone1) ? this.cart.customer.phone1 : null; const email1 = (this.cart.customer && this.cart.customer.email1) ? this.cart.customer.email1 : null; var brpAuIndicator = (this.cart.customer && this.cart.customer.brpAuIndicator) ? this.cart.customer.brpAuIndicator : ''; this.cart.customer = Object.assign(this.cart.customer, result); this.cart.customer.orderId = this.cart.orderId; this.cart.customer.brpAuIndicator = brpAuIndicator; if (!this.cart.customer.phone1 && phone1) { this.cart.customer.phone1 = phone1 }; if (!this.cart.customer.email1 && email1) { this.cart.customer.email1 = email1 }; if (this.cart.customer.subscribers) {// updating the virtual rateplan for (const subscriber of this.cart.customer.subscribers) { if (subscriber.isGSM && subscriber.isPooledPlan) { this.cart.customer.creditProfile.virtualRatePlan = subscriber.rateplanSOC; break; } } } this.sharedService.showSpinner(true); // spinner this.customerService.createCustomer(this.storeId, this.cart.customer).subscribe(result => { this.sharedService.showSpinner(false); // spinner inputData.description = Constants.SUCCESS_DESC; this.sharedService.addOrderNotes(this.storeId, this.cart.orderId, inputData); this.lookupService.setOrder(null); this.customerService.setCustomer(null); this.cart.customer.addressId = result.addressId; this.cartService.setCart(this.cart); if (this.canRedirectToPayReview) { this.router.navigate(['checkout/pay-review']); } else { this.router.navigate(['home/fullcart']); } }, error => { this.sharedService.showSpinner(false); // spinner console.error('error=>' + error); this.sharedService.showErrorMessage(error, true); inputData.description = Constants.FAILURE; this.sharedService.addOrderNotes(this.storeId, this.cart.orderId, inputData); }); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error); inputData.description = 'Failure'; this.sharedService.addOrderNotes(this.storeId, this.cart.orderId, inputData); }); } else { if (creditProfile.identifier && creditProfile.identifierType) { if (creditProfile.identifierType === this.authenticateModel.identifierType && creditProfile.identifier === this.authenticateModel.identifier) { this.lookupService.setOrder(null); this.customerService.setCustomer(null); this.cartService.setCart(this.cart); this.cart.customer.nonTMOMobilePin = creditProfile.identifier; inputData.description = FormConstants.DESCRIPTION_SUCCESS; this.sharedService.addOrderNotes(this.storeId, this.cart.orderId, inputData); if (this.canRedirectToPayReview) { this.router.navigate(['checkout/pay-review']); } else { this.router.navigate(['home/fullcart']); } } else { inputData.description = FormConstants.DESCRIPTION_FAILURE; this.sharedService.addOrderNotes(this.storeId, this.cart.orderId, inputData); this.sharedService.showErrorMessage('_ERR_INVAID_PIN_SSN', false); } } else { this.lookupService.setOrder(null); this.customerService.setCustomer(null); this.cartService.setCart(this.cart); inputData.description = FormConstants.DESCRIPTION_SUCCESS; this.sharedService.addOrderNotes(this.storeId, this.cart.orderId, inputData); if (this.canRedirectToPayReview) { this.router.navigate(['checkout/pay-review']); } else { this.router.navigate(['home/fullcart']); } } } this.hide(); } }