import { Component, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { BsModalRef, BsModalService } from 'ngx-bootstrap'; import { ToastrService } from 'ngx-toastr'; import { ManageConfigService } from '../../../services/manage.configuration.service'; import { AlertModalComponent } from '../../common/alert-modal/alert-modal.component'; @Component({ selector: 'app-portal-config', templateUrl: './portal-config.component.html', styleUrls: ['./portal-config.component.css'] }) export class PortalConfigComponent implements OnInit { manageConfigForm: FormGroup; alertConfirm: boolean; bsModalRef: BsModalRef; config: any[]; defaultMinCartValue: Number; defaultDiscountValue: Number; defaultWalletMinCartValue: Number; defaulWallettDiscountValue: Number; maxWalletCredit: Number; walletDiscountMinValue: Number; cartRuleValue: string; defaultHandlingFee: Number; defaultHandlingMinCartValue: Number; discountValueError: boolean; constructor(private fb: FormBuilder, private toastr: ToastrService, private modalService: BsModalService, private manageConfigService: ManageConfigService) { } ngOnInit(): void { this.discountValueError = false; this.setTheDefaultValues(); } /* @purpose: to set the default values of the exisiting manage configuration */ setTheDefaultValues() { this.manageConfigService.getExisitingConfiguration().subscribe((result: { status: string, message: string, data }) => { this.config = result.data if(this.config[0].upfront_discount) this.cartRuleValue = 'Upfront'; if(this.config[0].wallet_credit_discount) this.cartRuleValue = 'wallet'; this.defaultDiscountValue = this.config[0].upfront_discount_value; this.defaultMinCartValue = this.config[0].upfront_min_cart_value; this.defaultWalletMinCartValue = this.config[0].wallet_min_cart_value; this.defaulWallettDiscountValue = this.config[0].wallet_discount_provided; this.defaultHandlingFee = this.config[0].handling_fee; this.defaultHandlingMinCartValue = this.config[0].handling_min_cart_value; this.maxWalletCredit = this.config[0].max_wallet_applied; this.walletDiscountMinValue = this.config[0].wallet_dicount_min_cart; this.manageConfigForm = this.fb.group({ has_notification: [this.config[0].has_notification], notification: [this.config[0].notification], searchDump: [this.config[0].enable_search_dump], cartRuleOption: [this.cartRuleValue], minCartValue: [this.config[0].upfront_min_cart_value], discountValue: [this.config[0].upfront_discount_value], wallet_min_cart_value: [this.config[0].wallet_min_cart_value], wallet_discount_provided:[this.config[0].wallet_discount_provided], max_wallet_applied: [this.config[0].max_wallet_applied], wallet_dicount_min_cart: [this.config[0].wallet_dicount_min_cart], handlingFee: [this.config[0].handling_fee], handlingMinCartValue: [this.config[0].handling_min_cart_value], referralCredit : [this.config[0].referral_credit] } ); }); } /* @purpose: to set the validators to below fields */ setWalletDiscountValidators() { this.manageConfigForm.controls.wallet_dicount_min_cart.setValidators([Validators.pattern('^[0-9]*$'), Validators.required]); this.manageConfigForm.controls.wallet_dicount_min_cart.updateValueAndValidity(); } setWalletCreditValidators() { this.manageConfigForm.controls.max_wallet_applied.setValidators([Validators.pattern('^[0-9]*$'), Validators.required]); this.manageConfigForm.controls.max_wallet_applied.updateValueAndValidity(); } minCartValidators() { this.manageConfigForm.controls.minCartValue.setValidators([Validators.pattern('^[0-9]*$'), Validators.required]); this.manageConfigForm.controls.minCartValue.updateValueAndValidity(); } minWalletCartValidators() { this.manageConfigForm.controls.wallet_min_cart_value.setValidators([Validators.pattern('^[0-9]*$'), Validators.required]); this.manageConfigForm.controls.wallet_min_cart_value.updateValueAndValidity(); } walletDiscountValidators() { this.manageConfigForm.controls.wallet_discount_provided.setValidators([Validators.pattern('^[0-9]*$'), Validators.required]); this.manageConfigForm.controls.wallet_discount_provided.updateValueAndValidity(); } discountValidators() { this.manageConfigForm.controls.discountValue.setValidators([Validators.pattern('^[0-9]*$'), Validators.required]); this.manageConfigForm.controls.discountValue.updateValueAndValidity(); } handlingFeeValidators(){ this.manageConfigForm.controls.handlingFee.setValidators([Validators.pattern('^[0-9]*$'), Validators.required]); this.manageConfigForm.controls.handlingFee.updateValueAndValidity(); } handlingFeeMinCartValueValidators(){ this.manageConfigForm.controls.handlingMinCartValue.setValidators([Validators.pattern('^[0-9]*$'), Validators.required]); this.manageConfigForm.controls.handlingMinCartValue.updateValueAndValidity(); } referralCreditValidators(){ this.manageConfigForm.controls.referralCredit.setValidators([Validators.pattern('^[0-9]*$'), Validators.required]); this.manageConfigForm.controls.referralCredit.updateValueAndValidity(); } /* @usage: called when cart rule is selected or changed @purpose: to enable the feilds based on cart rule selected */ toggleCartRule(value) { this.manageConfigForm.get('cartRuleOption').setValue(value); this.removeWalletValidators(); this.removeUpFrontValidators(); this.manageConfigForm.get('minCartValue').setValue(this.defaultMinCartValue); this.manageConfigForm.get('discountValue').setValue(this.defaultDiscountValue); this.manageConfigForm.get('wallet_discount_provided').setValue(this.defaulWallettDiscountValue); this.manageConfigForm.get('wallet_min_cart_value').setValue(this.defaultWalletMinCartValue); this.manageConfigForm.get('max_wallet_applied').setValue(this.maxWalletCredit); this.manageConfigForm.get('wallet_dicount_min_cart').setValue(this.walletDiscountMinValue); } //Remove the validators removeUpFrontValidators() { this.manageConfigForm.controls.minCartValue.clearValidators(); this.manageConfigForm.controls.minCartValue.updateValueAndValidity(); this.manageConfigForm.controls.discountValue.clearValidators(); this.manageConfigForm.controls.discountValue.updateValueAndValidity(); } removeWalletValidators() { this.manageConfigForm.controls.wallet_discount_provided.clearValidators(); this.manageConfigForm.controls.wallet_discount_provided.updateValueAndValidity(); this.manageConfigForm.controls.wallet_min_cart_value.clearValidators(); this.manageConfigForm.controls.wallet_min_cart_value.updateValueAndValidity(); this.manageConfigForm.controls.wallet_dicount_min_cart.clearValidators(); this.manageConfigForm.controls.wallet_dicount_min_cart.updateValueAndValidity(); this.manageConfigForm.controls.max_wallet_applied.clearValidators(); this.manageConfigForm.controls.max_wallet_applied.updateValueAndValidity(); } /* @usage: called when serach dump is submitted @purpose: to update the value of dump serach in db */ onSearchSubmit() { const serachDump = { enable_search_dump: this.manageConfigForm.get('searchDump').value, manage_configurations_id: 1 }; this.manageConfigService.updateManageConfiguration(serachDump).subscribe((result: { status: string, message: string, data }) => { if (result.status === 'success') { this.toastr.success('Search Dump is updated'); } }, (error) => { this.toastr.error("Something Went Wrong! Please try to refresh"); }, ); } /* @usage: called when notification is submitted @purpose: to update the value of notification and its value in db */ applyNotification() { this.manageConfigForm.controls.notification.setValidators([Validators.required]); this.manageConfigForm.controls.notification.updateValueAndValidity(); if(this.manageConfigForm.get('notification').value !== '') { const updateNotification = { has_notification: this.manageConfigForm.get('has_notification').value,notification:this.manageConfigForm.get('notification').value, manage_configurations_id: 1 }; this.manageConfigService.updateManageConfiguration(updateNotification).subscribe((result: { status: string, message: string, data }) => { if (result.status === 'success') { this.toastr.success('Existing notification is updated'); this.manageConfigService.getExisitingConfiguration().subscribe((result: { status: string, message: string, data }) => { this.config = result.data; this.manageConfigForm.get('notification').setValue(result.data[0].notification); this.manageConfigForm.get('has_notification').setValue(result.data[0].has_notification); }); } }, (error) => { this.toastr.error("Something Went Wrong! Please try to refresh"); }, );} else { this.manageConfigForm.controls.notification.markAsTouched(); } } //open the popup to confirm the delete and then update the values in db deleteNotification() { this.bsModalRef = this.modalService.show(AlertModalComponent, { backdrop: 'static', keyboard: false, animated: true, ignoreBackdropClick: true, initialState: { title: 'Alert', data: 'Are you sure to delete existing Notification.', buttonText: 'Cancel', deleteBtn: 'Delete', } }); let confirm: boolean; this.bsModalRef.content.event.subscribe(res => { this.alertConfirm = res; if (this.alertConfirm) { const deleteNotification = { has_notification: false,notification:'', manage_configurations_id: 1 }; this.manageConfigService.updateManageConfiguration(deleteNotification).subscribe((result: { status: string, message: string, data }) => { if (result.status === 'success') { this.toastr.success('Existing notification is deleted'); this.manageConfigForm.controls.notification.clearValidators(); this.manageConfigForm.controls.notification.updateValueAndValidity(); this.manageConfigForm.get('notification').setValue(''); this.manageConfigForm.get('has_notification').setValue(false); } }, (error) => { this.toastr.error("Something Went Wrong! Please try to refresh"); }, ); } }); } /* @usage: called when delete is clicked in rule cart @purpose: to make both the cart rules false in db */ deleteRule() { const resetCartRules = { upfront_discount: false, wallet_credit_discount: false, manage_configurations_id: 1 }; this.manageConfigService.updateManageConfiguration(resetCartRules).subscribe((result: { status: string, message: string, data }) => { if (result.status === 'success') { this.toastr.success('Existing Configuration is deleted'); this.manageConfigForm.get('cartRuleOption').setValue(''); } }, (error) => { this.toastr.error("Something Went Wrong! Please try to refresh"); }, ); } applyRule() { if(this.manageConfigForm.get('cartRuleOption').value === null || this.manageConfigForm.get('cartRuleOption').value === ''){ this.toastr.error("Please select any rule to apply!"); }else{ if (this.manageConfigForm.get('cartRuleOption').value === 'Upfront') { this.minCartValidators(); this.discountValidators(); this.manageConfigForm.controls['minCartValue'].markAllAsTouched(); this.manageConfigForm.controls['discountValue'].markAllAsTouched(); if ( (this.manageConfigForm.get('minCartValue').value !== null || this.manageConfigForm.get('minCartValue').value !== '' ) && ( this.manageConfigForm.get('discountValue').value !== null || this.manageConfigForm.get('discountValue').value !== '' )) { const upFrontRule = {upfront_discount:true,wallet_credit_discount:false,manage_configurations_id:1, upfront_min_cart_value:this.manageConfigForm.get('minCartValue').value, upfront_discount_value:this.manageConfigForm.get('discountValue').value } if(this.manageConfigForm.valid && !this.lessThanOrEqualTo(this.manageConfigForm.get('discountValue').value, this.manageConfigForm.get('minCartValue').value)) this.manageConfigService.updateManageConfiguration(upFrontRule).subscribe((result: { status: string, message: string, data }) => { if (result.status === 'success') { this.toastr.success('Existing configuration is updated'); this.defaultMinCartValue = this.manageConfigForm.get('minCartValue').value; this.defaultDiscountValue = this.manageConfigForm.get('discountValue').value; } }, (error) => { this.toastr.error("Something Went Wrong! Please try to refresh"); }, ); } } else { this.minWalletCartValidators(); this.walletDiscountValidators(); this.setWalletDiscountValidators(); this.setWalletCreditValidators(); this.manageConfigForm.controls['wallet_min_cart_value'].markAllAsTouched(); this.manageConfigForm.controls['wallet_discount_provided'].markAllAsTouched(); this.manageConfigForm.controls['max_wallet_applied'].markAllAsTouched(); this.manageConfigForm.controls['wallet_dicount_min_cart'].markAllAsTouched(); if (this.manageConfigForm.get('wallet_min_cart_value').value && this.manageConfigForm.get('wallet_discount_provided').value && this.manageConfigForm.get('max_wallet_applied').value && this.manageConfigForm.get('wallet_dicount_min_cart').value ) { const walletRuleValues = {wallet_credit_discount:true,upfront_discount:false,manage_configurations_id:1, wallet_min_cart_value:this.manageConfigForm.get('wallet_min_cart_value').value, wallet_discount_provided:this.manageConfigForm.get('wallet_discount_provided').value, wallet_dicount_min_cart:this.manageConfigForm.get('wallet_dicount_min_cart').value, max_wallet_applied:this.manageConfigForm.get('max_wallet_applied').value, } if(this.manageConfigForm.valid) this.manageConfigService.updateManageConfiguration(walletRuleValues).subscribe((result: { status: string, message: string, data }) => { if (result.status === 'success') { this.toastr.success('Existing configuration is updated'); this.defaultWalletMinCartValue = this.manageConfigForm.get('wallet_min_cart_value').value; this.defaulWallettDiscountValue = this.manageConfigForm.get('wallet_discount_provided').value; this.maxWalletCredit = this.manageConfigForm.get('max_wallet_applied').value; this.walletDiscountMinValue = this.manageConfigForm.get('wallet_dicount_min_cart').value; } }, (error) => { this.toastr.error("Something Went Wrong! Please try to refresh"); }, ); } } } } onReferralCreditSubmit() { this.referralCreditValidators(); this.manageConfigForm.controls['referralCredit'].markAllAsTouched(); if ( this.manageConfigForm.get('referralCredit').value !== '') { const referralCreditRule = { manage_configurations_id:1, referral_credit:this.manageConfigForm.get('referralCredit').value, } if ( this.manageConfigForm.valid ) { this.manageConfigService.updateManageConfiguration(referralCreditRule).subscribe((result: { status: string, message: string, data }) => { if (result.status === 'success') { this.toastr.success('Existing configuration is updated'); this.manageConfigForm.get('referralCredit').setValue(result.data.referral_credit); } }, (error) => { this.toastr.error("Something Went Wrong! Please try to refresh"); }, ); } } } onHandlingFeeSubmit(){ this.handlingFeeValidators(); this.handlingFeeMinCartValueValidators(); this.manageConfigForm.controls['handlingFee'].markAllAsTouched(); this.manageConfigForm.controls['handlingMinCartValue'].markAllAsTouched(); if ( this.manageConfigForm.get('handlingFee').value !== '' && this.manageConfigForm.get('handlingMinCartValue').value !== '') { const handlingFeeRule = { manage_configurations_id:1, handling_fee:this.manageConfigForm.get('handlingFee').value, handling_min_cart_value:this.manageConfigForm.get('handlingMinCartValue').value } if ( this.manageConfigForm.valid ) { this.manageConfigService.updateManageConfiguration(handlingFeeRule).subscribe((result: { status: string, message: string, data }) => { if (result.status === 'success') { this.toastr.success('Existing configuration is updated'); this.defaultHandlingFee = result.data.handling_fee; this.defaultHandlingMinCartValue = result.data.handling_min_cart_value; } }, (error) => { this.toastr.error("Something Went Wrong! Please try to refresh"); }, ); } } } lessThanOrEqualTo(value1: string, value2: string) { if (value2 === '') { return false; } const number1: number = Number(value1); const number2: number = Number(value2); return number1 > number2; } lessThan(value1: string, value2:string) { if(value2===''){ return false; } const number1: number = Number(value1); const number2: number = Number(value2); return number1 >= number2; } onWalletCreditsSubmit(){ this.setWalletDiscountValidators(); this.setWalletCreditValidators(); this.manageConfigForm.controls['max_wallet_applied'].markAllAsTouched(); this.manageConfigForm.controls['wallet_dicount_min_cart'].markAllAsTouched(); if(this.manageConfigForm.get('max_wallet_applied').value && this.manageConfigForm.get('wallet_dicount_min_cart').value){ const walletRuleValues = { manage_configurations_id:1, wallet_dicount_min_cart:this.manageConfigForm.get('wallet_dicount_min_cart').value, max_wallet_applied:this.manageConfigForm.get('max_wallet_applied').value, } if(this.manageConfigForm.valid && !this.lessThan(this.manageConfigForm.get('max_wallet_applied').value, this.manageConfigForm.get('wallet_dicount_min_cart').value)){ this.manageConfigService.updateManageConfiguration(walletRuleValues).subscribe((result: { status: string, message: string, data }) => { if (result.status === 'success') { this.toastr.success('Existing configuration is updated'); this.maxWalletCredit = this.manageConfigForm.get('max_wallet_applied').value; this.walletDiscountMinValue = this.manageConfigForm.get('wallet_dicount_min_cart').value; } }, (error) => { this.toastr.error("Something Went Wrong! Please try to refresh"); }, ); } } } //Default getter setter for fields get cartRuleOption() { return this.manageConfigForm.get('cartRuleOption') } get minCartValue() { return this.manageConfigForm.get('minCartValue') } get discountValue() { return this.manageConfigForm.get('discountValue') } get wallet_dicount_min_cart() { return this.manageConfigForm.get('wallet_dicount_min_cart') } get max_wallet_applied() { return this.manageConfigForm.get('max_wallet_applied') } get has_notification() { return this.manageConfigForm.get('has_notification') } get searchDump() { return this.manageConfigForm.get('searchDump') } get notification() {return this.manageConfigForm.get('notification')} get wallet_min_cart_value() {return this.manageConfigForm.get('wallet_min_cart_value')} get wallet_discount_provided() {return this.manageConfigForm.get('wallet_discount_provided')} get handlingFee() {return this.manageConfigForm.get('handlingFee')} get handlingMinCartValue() {return this.manageConfigForm.get('handlingMinCartValue')} get referralCredit() { return this.manageConfigForm.get('referralCredit')} }