From eef97fd35f33d5491296de8c2777fe98cb4465bb Mon Sep 17 00:00:00 2001 From: nikolay Date: Thu, 15 Jun 2023 15:40:19 +0400 Subject: [PATCH] =?UTF-8?q?dev=20#14494=20WPA=20CoffeeLike=20=D0=B8=D0=B7?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D0=B8=D1=82=D1=8C=20=D0=B0=D0=BF=D0=B8=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=D0=B4=D0=BD=D0=B5?= =?UTF-8?q?=D0=B9=20=D1=82=D1=80=D0=B0=D0=BD=D0=B7=D0=B0=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../last-order/last-order.component.ts | 4 +- .../guest-card/guest-card.component.html | 33 +----- .../pages/guest-card/guest-card.component.ts | 32 +----- .../app/services/loyalty-program.service.ts | 108 ++++-------------- angular/src/app/services/wp-json.service.ts | 33 +++--- angular/src/environments/environment.ts | 2 +- 6 files changed, 49 insertions(+), 163 deletions(-) diff --git a/angular/src/app/presentation-options/default-option/components/last-order/last-order.component.ts b/angular/src/app/presentation-options/default-option/components/last-order/last-order.component.ts index 0117951..ab3963c 100644 --- a/angular/src/app/presentation-options/default-option/components/last-order/last-order.component.ts +++ b/angular/src/app/presentation-options/default-option/components/last-order/last-order.component.ts @@ -7,9 +7,9 @@ import { Purchase } from 'src/app/interface/data'; styleUrls: ['./last-order.component.scss'] }) export class LastOrderComponent implements OnInit { - @Input() lastOrder!: Purchase; + @Input() lastOrder?: Purchase; @Input() loading!: boolean; - + constructor() { } ngOnInit(): void { diff --git a/angular/src/app/presentation-options/default-option/pages/guest-card/guest-card.component.html b/angular/src/app/presentation-options/default-option/pages/guest-card/guest-card.component.html index b273c7c..03d0f89 100644 --- a/angular/src/app/presentation-options/default-option/pages/guest-card/guest-card.component.html +++ b/angular/src/app/presentation-options/default-option/pages/guest-card/guest-card.component.html @@ -33,7 +33,7 @@ Осталось купить на сумму {{ loyaltyProgram.currentLvlPeriod.end - - (loyaltyProgram.purchaseData.currentAmount || 0) + + (customerInfo.OrdersSum) + 1 }} рублей, тогда кэшбек будет @@ -55,37 +55,8 @@

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - { - this.purchases = this.loyaltyProgram.filterPurchases(value[this.customerInfo?.id]) - this.lastPurchase = this.loyaltyProgram.getLastPurchase(this.purchases) + this.loyaltyProgram.setCurrentLvl(this.customerInfo.OrdersSum); - this.loyaltyProgram.setLastPurchases(this.purchases) - this.loyaltyProgram.setCurrentPurchases(this.purchases) - - const currentAmount = this.loyaltyProgram.purchaseData.currentAmount || 0 - this.loyaltyProgram.setCurrentLvl(currentAmount) - - this.loyaltyProgram.purchaseData.$loading = false; - }); + this.loyaltyProgram.getLastPurchase( + environment.systemId, + token || '', + ); }, }); } @@ -108,18 +100,4 @@ export class GuestCardComponent implements OnInit { }, }); } - - 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 - ); - } - } diff --git a/angular/src/app/services/loyalty-program.service.ts b/angular/src/app/services/loyalty-program.service.ts index f6646d7..98a8263 100644 --- a/angular/src/app/services/loyalty-program.service.ts +++ b/angular/src/app/services/loyalty-program.service.ts @@ -2,15 +2,15 @@ import { Injectable } from '@angular/core'; import moment from 'moment'; import { Moment, Purchase, lvlPeriod } from '../interface/data'; import { lvlPeriods } from '../app.constants'; +import { WpJsonService } from './wp-json.service'; +import { Observable } from 'rxjs'; export interface IPurchaseData { currentPeriod: Moment[]; lastPeriod: Moment[]; - lastPurchases: Purchase[]; - currentPurchases: Purchase[]; - lastAmount?: number; currentAmount?: number; $loading: boolean; + lastPurchase?: Purchase; } @Injectable({ @@ -19,59 +19,18 @@ export interface IPurchaseData { export class LoyaltyProgramService { public purchaseData: IPurchaseData = { currentPeriod: [], - lastPeriod: [], - lastPurchases: [], - currentPurchases: [], + lastPeriod: [], $loading: false, - get currentAmount(): number { - const amount = this.currentPurchases.reduce( - (accumulator, currentValue) => { - if ( - ['CancelPayFromWallet', 'CancelRefillWalletFromOrder'].includes( - currentValue.transactionType || '' - ) - ) { - return accumulator - currentValue.orderSum!; - } else if ( - ['PayFromWallet', 'RefillWalletFromOrder'].includes( - currentValue.transactionType || '' - ) - ) { - return accumulator + currentValue.orderSum!; - } - return accumulator; - }, - 0 - ); - - return amount * 1; - }, - get lastAmount(): number { - const amount = this.lastPurchases.reduce((accumulator, currentValue) => { - if ( - ['CancelPayFromWallet', 'CancelRefillWalletFromOrder'].includes( - currentValue.transactionType || '' - ) - ) { - return accumulator - currentValue.orderSum!; - } else if ( - ['PayFromWallet', 'RefillWalletFromOrder'].includes( - currentValue.transactionType || '' - ) - ) { - return accumulator + currentValue.orderSum!; - } - return accumulator; - }, 0); - return amount * 1; - }, + lastPurchase: undefined, }; public currentLvl: number = 1; public currentLvlPeriod!: lvlPeriod; - public lvlPeriods: lvlPeriod[] = lvlPeriods; + public lvlPeriods: lvlPeriod[] = lvlPeriods; - constructor() { + constructor( + private wpJsonService: WpJsonService, + ) { this.getCurrentQuarterOfYear(); } @@ -116,43 +75,6 @@ export class LoyaltyProgramService { }, 0); } - setLastPurchases(purchases: Purchase[]) { - this.purchaseData.lastPurchases = (purchases || []).filter((value: Purchase) => { - return moment(value.transactionCreateDate).isBetween( - this.purchaseData.lastPeriod[0], - this.purchaseData.lastPeriod[1] - ); - }); - } - - setCurrentPurchases(purchases: Purchase[]) { - this.purchaseData.currentPurchases = (purchases || []).filter((value: Purchase) => { - return moment(value.transactionCreateDate).isBetween( - this.purchaseData.currentPeriod[0], - this.purchaseData.currentPeriod[1] - ); - }); - } - - filterPurchases(purchases: Purchase[]) { - return (purchases || []).filter((purchase: Purchase) => - [ - 'PayFromWallet', - 'RefillWalletFromOrder', - 'CancelPayFromWallet', - 'CancelRefillWalletFromOrder', - ].includes(purchase.transactionType || '') - ); - } - - getLastPurchase(purchases: Purchase[]) { - return purchases.filter((purchase: Purchase) => - ['PayFromWallet', 'RefillWalletFromOrder'].includes( - purchase.transactionType || '' - ) - )[0]; - } - setCurrentLvl(currentAmount: number) { const index = this.lvlPeriods.findIndex( (item) => @@ -170,4 +92,16 @@ export class LoyaltyProgramService { } return lvlPeriods[this.currentLvl]; } + + getLastPurchase(systemId: string, token: string) { + this.purchaseData.$loading = true; + this.wpJsonService.getLastPurchase(systemId, token).subscribe({ + next: (res) => { + this.purchaseData.lastPurchase = res; + }, + complete: () => { + this.purchaseData.$loading = false; + } + }); + } } diff --git a/angular/src/app/services/wp-json.service.ts b/angular/src/app/services/wp-json.service.ts index c45d237..86959e8 100644 --- a/angular/src/app/services/wp-json.service.ts +++ b/angular/src/app/services/wp-json.service.ts @@ -1,12 +1,11 @@ import { Injectable } from '@angular/core'; -import {environment} from "../../environments/environment"; -import {HttpClient, HttpHeaders} from "@angular/common/http"; -import {CookiesService} from "./cookies.service"; -import {Observable, of, switchMap} from "rxjs"; -import {JsonRpcBody} from "./jsonrpc.service"; -import {DeliveryType, AcceptedOrder, Product} from "../interface/data"; -import {ActivatedRoute} from "@angular/router"; -import {Order} from "../models/order"; +import { environment } from "../../environments/environment"; +import { HttpClient, HttpHeaders } from "@angular/common/http"; +import { CookiesService } from "./cookies.service"; +import { Observable, of, switchMap } from "rxjs"; +import { JsonRpcBody } from "./jsonrpc.service"; +import { DeliveryType, AcceptedOrder, Product, Purchase } from "../interface/data"; +import { ActivatedRoute } from "@angular/router"; export enum Method { @@ -26,26 +25,26 @@ export class WpJsonService { private route: ActivatedRoute, ) { } - getDeliveryTypes(): Observable{ + getDeliveryTypes(): Observable { return this._request('orders/delivery-types', 'GET'); } - createOrder(order: any){ + createOrder(order: any) { return this._request('orders', 'POST', order); } - getOrders(): Observable{ + getOrders(): Observable { return this._request('orders', 'GET', null, true); } - getProductById(id: number): Observable{ + getProductById(id: number): Observable { return this._request(`products/${id}`, 'GET'); } getCustomerInfo(systemId: string, token: string, url: string): Observable { return this._request(`customer_info/${systemId}/${token}/`, 'GET', null, false, url).pipe( switchMap((response) => { - // TODO: typescript compile optional chaining ('response?.customer_info?.errorCode') without check ('response.customer_info.errorCode') + // TODO: typescript compile optional chaining ('response?.customer_info?.errorCode') without check ('response.customer_info.errorCode') if (response && response.customer_info && response.customer_info.errorCode !== 'Customer_CustomerNotFound') { return of(response) } else { @@ -57,6 +56,10 @@ export class WpJsonService { ) } + getLastPurchase(systemId: string, token: string): Observable { + return this._request(`last_trans/${systemId}/${token}/`, 'GET', null, false, environment.icardProxy); + } + newCustomer(systemId: string, token: string, url: string): Observable { return this._request(`new_customer/${systemId}/${token}/`, 'GET', null, false, url) } @@ -83,7 +86,7 @@ export class WpJsonService { headers = headers.set('Content-Type', 'application/json'); let urlToken = ''; if (token && token !== 'undefined' && auth) { - urlToken = '?token=' + token; + urlToken = '?token=' + token; } this.body = body; const options = { @@ -96,6 +99,6 @@ export class WpJsonService { url = baseUrl } return this.http - .request( method, url + path + urlToken, options); + .request(method, url + path + urlToken, options); } } diff --git a/angular/src/environments/environment.ts b/angular/src/environments/environment.ts index 647d303..1a7acb4 100644 --- a/angular/src/environments/environment.ts +++ b/angular/src/environments/environment.ts @@ -19,7 +19,7 @@ export const environment = { }, version: packageJson.version, appleWalletEndpoint: 'https://apple-wallet-iiko.it-retail.tech/apns/api', - icardProxy: 'http://localhost:4200/icard-proxy/', + icardProxy: 'https://coffee-like-test.lk.crm4retail.ru/api/icard-proxy/', appleWalletSecret: 'Token F5mbzEERAznGKVbB6l', clientName: 'coffeelike' };