/** * @Component Name: OTPAuthComponent * @Module: Checkout * @Module Functionality : To display the customer otp token * @Creation Date: Feb 08,2018 * @Author: sbaireddy * @Version: 1.0 */ import { Component, Input, Output, EventEmitter, ViewChild, OnInit, OnChanges, OnDestroy, AfterViewChecked} from '@angular/core'; import { NgForm } from '@angular/forms'; import * as Constants from '../../../shared/constants'; import Utils from '../../../shared/utils'; import { User } from '../../../shared/modal/user'; import { Cart } from '../../../shared/modal/cart'; import { Customer, ICreditProfile } from '../../../shared/modal/customer'; import { SharedService } from '../../../shared/shared.service'; import { UserService } from '../../../services/user.service'; import { CustomerService } from '../../../services/customer.service'; import { CheckoutService } from '../../../services/checkout.service'; import { CartService } from '../../../services/cart.service'; @Component( { selector: 'otp-auth-cmp', moduleId: module.id, templateUrl: 'otp-auth.component.html', styleUrls: ['otp-auth.component.css'] }) export class OTPAuthComponent implements OnInit, OnChanges, OnDestroy, AfterViewChecked { private COMPONENT_NAME: string = 'OTPAuthComponent'; storeId: number = Constants.STORE_ID_CD; user: User; @Input() cart: Cart; customerProfile: Customer; @Input() customer: Customer; @Output() onTokenVerified = new EventEmitter(false); otpAuthModel: any = {}; @ViewChild('otpAuthForm') otpAuthForm: NgForm; @Output() onFormChange = new EventEmitter(); otpTokenSend: boolean = false; dropDownArray: any; selectedOption: any; smsPhoneNumber: number; otpResponse: any; constructor( public sharedService: SharedService, public userService: UserService, public customerService: CustomerService, public checkoutService: CheckoutService, public cartService: CartService) { } /** *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.Here it is initializing all the states, payment methods and card types * @returns void */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; }); this.user = this.userService.getUser(); } /** * This function is called whenever one or more data-bound input properties change * @return {void} */ ngOnChanges(): void { const METHOD_NAME: string = 'ngOnChanges()'; if (this.customer && !this.customerProfile) { this.findCustomerProfile(this.customer.creditProfile.accountNumber); } } /** * This function is called after view checked * @returns void */ ngAfterViewChecked(): void { this.onFormChange.emit(this.otpAuthForm); } /** * 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 function is used format the phone number as 'xxx-xxx-xxxx' * if the phone number is valid. * @param {string} phoneNumber contains valid phone number. * @returns string */ formatPhoneNumber(phoneNumber: string): string { const METHOD_NAME: string = 'formatPhoneNumber()'; return Utils.formatPhoneNumber(phoneNumber); } /** * This function is used to set the otpTokenSend as true * when user clicks on button Send OTP Token * @returns void */ sendOTPToken(): void { const METHOD_NAME: string = 'sendOTPToken()'; this.sharedService.addLogging(this.COMPONENT_NAME,  METHOD_NAME,  'Sending the OTP Token'); if (this.selectedOption) { const accountNumber: number = this.customer.creditProfile.accountNumber; const inputData: any = {} inputData.accountNumber = accountNumber; if (this.selectedOption.type === 'sms') { inputData.phoneNumber = this.smsPhoneNumber; } else { inputData.emailAddress = this.selectedOption.items.name; } this.sharedService.showSpinner(true); // spinner this.customerService.otpGenerate(this.storeId, accountNumber, inputData).subscribe(result => { this.sharedService.showSpinner(false); // spinner if (result && result.code === 100) { this.otpResponse = result; this.otpTokenSend = true; } else { this.sharedService.showErrorMessage('_ERR_OTP_GENERATE_CODE', false); } }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error); }); } else { console.log('no option selected.'); } } /** * This function is used to set the token as false * if user clicks on button "Problems with your code?" * @returns void */ resendOTPToken(): void { const METHOD_NAME: string = 'resendOTPToken()'; this.otpTokenSend = false; } /** * This function is used for the verification of the token * @returns void */ validateOTPToken(): void { const METHOD_NAME: string = 'validateOTPToken()'; this.sharedService.addLogging(this.COMPONENT_NAME,  METHOD_NAME,  'validating the OTP token: '); const accountNumber: number = this.customer.creditProfile.accountNumber; const inputData: any = {} inputData.accoutNumber = accountNumber; if (this.selectedOption.type === 'sms') { inputData.phoneNumber = this.smsPhoneNumber; } else { inputData.emailAddress = this.selectedOption.items.name; } inputData.otp = this.otpAuthModel.otpToken; this.sharedService.showSpinner(true); // spinner this.customerService.otpValidate(this.storeId, accountNumber, inputData).subscribe(result => { this.sharedService.showSpinner(false); // spinner if (result && result.code === 100) { const otpinputData: any = { agentId: this.user.logonId, subject: Constants.CUSTOMER_VERIFICATION, description: Constants.OTP_VERIFIED } this.sharedService.addOrderNotes(this.storeId, this.cart.orderId, otpinputData); this.otpTokenSend = true; this.updateCartDetails(); } else { this.sharedService.showErrorMessage('_ERR_OTP_VALIDATE_CODE', false); } }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error); }); } /** * This function is used to find customer details from profileByBAN function of customer service. * @param {number} accountNumber - contains valid accountNumber of customer. * @returns void */ findCustomerProfile(accountNumber: number): void { const METHOD_NAME: string = 'findCustomerProfile()'; this.sharedService.showSpinner(true); // spinner this.customerService.profileByBAN(this.storeId, accountNumber.toString(), 'OTP_DETAIL').subscribe(result => { this.sharedService.showSpinner(false); // spinner this.customerProfile = result; this.formatDropdownArray(); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error); }); } /** * This function is used to send sms and email to the provided details. * @returns void */ formatDropdownArray(): void { const METHOD_NAME: string = 'formatDropdownArray()'; if (this.customerProfile && this.customerProfile.subscribers) { this.dropDownArray = new Array(); const dropDownItems: any = new Array(); // setting first primary account holder for (const sub of this.customerProfile.subscribers) { if (sub && sub.isPAH) { this.smsPhoneNumber = sub.phoneNumber; const formatPhoneNumber: string = 'Send SMS to ' + Utils.formatPhoneNumber(sub.phoneNumber.toString()); dropDownItems.push( {name: sub.phoneNumber, description: formatPhoneNumber, email: sub.emailAddress}); } } // setting authorized customers for (const sub of this.customerProfile.subscribers) { if (sub && sub.isAuthorized && !sub.isPAH) { if (!this.smsPhoneNumber) { this.smsPhoneNumber = sub.phoneNumber; } const formatPhoneNumber: string = 'Send SMS to ' + Utils.formatPhoneNumber(sub.phoneNumber.toString()); dropDownItems.push( {name: sub.phoneNumber, description: formatPhoneNumber, email: sub.emailAddress}); } } this.dropDownArray.push( {type: 'sms', items: dropDownItems, default: true}); this.selectedOption = {type: 'sms', items: dropDownItems, default: true}; // setting first primary account holder for (const sub of this.customerProfile.subscribers) { if (sub && (sub.isPAH || sub.isAuthorized) && sub.emailAddress) { this.dropDownArray.push( {type: 'email', items: { name: sub.emailAddress, description: 'Send Email to ' + sub.emailAddress, email: sub.emailAddress }, default: false}); } } if (this.customerProfile.paperlessBillingEmail) { const email: string = this.customerProfile.paperlessBillingEmail; this.dropDownArray.push( {type: 'paperless', items: { name: email, description: 'Send Email to ' + email, email: email }, default: false}); } } } /** * This function is used to update the email address and to send the mail. * @param {any} phoneNumber * @returns void */ updateEmail(phoneNumber: any): void { let smsItem: any; let emailItem: any; for (const item of this.dropDownArray) { if (item.type === 'sms') { for (const itm of item.items) { if (itm.name === phoneNumber) { smsItem = itm; break; } } } else if (item.type === 'email') { emailItem = item; } } // updating the email address if (smsItem) { if (smsItem.email) { if (emailItem) { for (const item of this.dropDownArray) { if (item.type === 'email') { item.items = {name: smsItem.email, description: 'Send Email to ' + smsItem.email, email: smsItem.email}; } } } else { this.dropDownArray.push( {type: 'email', items: { name: smsItem.email, description: 'Send Email to ' + smsItem.email, email: smsItem.email }, default: false}) } } else { if (emailItem) { for (const i in this.dropDownArray) { if (this.dropDownArray[i].type === 'email') { this.dropDownArray.slice(i, 1); } } } } } } updateCartDetails(): void { const METHOD_NAME: string = 'updateCartDetails()'; const inputData = { 'otpVerified': true } this.sharedService.showSpinner(true); // spinner this.cartService.updateCartDetails(this.storeId, this.cart.orderId, inputData).subscribe(result => { this.onTokenVerified.emit(true); this.sharedService.showSpinner(false); // spinner }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, null); console.error('error=>' + error); }); } }