import { Component, OnInit } from '@angular/core'; import { MatBottomSheet } from '@angular/material/bottom-sheet'; import { Router } from '@angular/router'; import { Observable } from 'rxjs'; import { ExitComponent } from 'src/app/components/exit/exit.component'; import { CookiesService } from 'src/app/services/cookies.service'; import { WpJsonService } from 'src/app/services/wp-json.service'; import { environment } from 'src/environments/environment'; import moment from 'moment'; import { Purchase, lvlPeriod } from 'src/app/interface/data'; import { lvlPeriods } from 'src/app/app.constants'; interface Moment extends moment.Moment {} export interface IPurchaseData { currentPeriod: Moment[]; lastPeriod: Moment[]; lastPurchases: Purchase[]; currentPurchases: Purchase[]; lastAmount?: number; currentAmount?: number; $loading: boolean; } @Component({ selector: 'app-guest-card', templateUrl: './guest-card.component.html', styleUrls: ['./guest-card.component.scss'], }) export class GuestCardComponent implements OnInit { public qrCodeSize: number = 85; private isQrCodeClicked: boolean = false; public customerInfo!: any; public purchases!: Purchase[]; public lastPurchase!: Purchase; public purchaseData: IPurchaseData = { currentPeriod: [], lastPeriod: [], lastPurchases: [], currentPurchases: [], $loading: true, get currentAmount():number { const amount = this.currentPurchases.reduce((accumulator, currentValue) => { if (currentValue.transactionType !== 'PayFromWallet') return accumulator; return accumulator + currentValue.orderSum!; }, 0); return amount * 1 }, get lastAmount():number { const amount = this.lastPurchases.reduce((accumulator, currentValue) => { if (currentValue.transactionType !== 'PayFromWallet') return accumulator; return accumulator + currentValue.orderSum!; }, 0); return amount * 1 } }; public discountLevel: number = 4.2; public lvlPeriods: lvlPeriod[] = lvlPeriods; public currentLvlPeriod!: lvlPeriod; public currentLvl: number = 1; constructor( private _bottomSheet: MatBottomSheet, private cookiesService: CookiesService, private router: Router, private wpJsonService: WpJsonService ) {} ngOnInit(): void { const token = this.cookiesService.getItem('token'); this.getCurrentQuarterOfYear(); this.wpJsonService .getCustomerInfo( environment.systemId, token || '', environment.icardProxy ) .subscribe({ next: (value) => { this.customerInfo = value.customer_info; this.getPurchases().subscribe((value) => { this.purchases = value[this.customerInfo?.id].filter( (purchase: Purchase) => [ 'PayFromWallet', 'CancelPayFromWallet', 'RefillWallet', 'RefillWalletFromOrder' ].includes(purchase.transactionType || '') ); this.lastPurchase = this.purchases.filter( (purchase: Purchase) => [ 'PayFromWallet', 'RefillWalletFromOrder' ].includes(purchase.transactionType || '') )[0] this.purchaseData.lastPurchases = this.purchases.filter((value: Purchase) => { return moment(value.transactionCreateDate).isBetween(this.purchaseData.lastPeriod[0], this.purchaseData.lastPeriod[1]) }) this.purchaseData.currentPurchases = this.purchases.filter((value: Purchase) => { return moment(value.transactionCreateDate).isBetween(this.purchaseData.currentPeriod[0], this.purchaseData.currentPeriod[1]) }) const currentAmount = this.purchaseData.currentAmount || 0 const index = this.lvlPeriods.findIndex((item) => item.start <= currentAmount && currentAmount <= (item.end || Infinity))! if(index != -1) { this.currentLvlPeriod = this.lvlPeriods[index]; this.currentLvl = index + 1; this.purchaseData.$loading = false; } }); }, }); } getNextLevel(): lvlPeriod { if(this.currentLvl == this.lvlPeriods.length) { return lvlPeriods[this.currentLvl - 1]; } return lvlPeriods[this.currentLvl]; } qrCodeClick() { this.isQrCodeClicked = !this.isQrCodeClicked; this.qrCodeSize = this.isQrCodeClicked ? 180 : 85; } deleteToken(): void { this.cookiesService.logout(); } logout() { const bottomSheet = this._bottomSheet.open(ExitComponent); bottomSheet.afterDismissed().subscribe({ next: (val) => { if (val) { this.deleteToken(); this.router.navigate(['/login']); } }, }); } getPurchases( start: Date | Moment = moment().subtract(1, 'months').startOf('month'), end: Date | Moment = moment() ): Observable { const token = this.cookiesService.getItem('token'); const delta = moment(end).diff(moment(start), 'days'); return this.wpJsonService.getTransactions( environment.systemId, token ?? '', environment.icardProxy ); } getCurrentQuarterOfYear() { const quarters = [ [ moment().subtract(1, 'years').endOf('year').subtract(3, 'months'), moment().subtract(1, 'years').endOf('year'), ], [moment().startOf('year'), moment().startOf('year').add(3, 'months')], [ moment().startOf('year').add(3, 'months'), moment().startOf('year').add(6, 'months'), ], [ moment().startOf('year').add(6, 'months'), moment().startOf('year').add(9, 'months'), ], [ moment().startOf('year').add(9, 'months'), moment().startOf('year').add(12, 'months'), ], ]; for (let i = 0; i < 4; i++) { if (moment().isBetween(quarters[i][0], quarters[i][1])) { this.purchaseData.lastPeriod = quarters[i - 1]; this.purchaseData.currentPeriod = quarters[i]; } } } getBalanceAmount(loyaltyPrograms: any[]) { return loyaltyPrograms.reduce((accumulator, currentValue) => { return accumulator + currentValue.balance; }, 0); } }