From df49c2a77e65e9148dab4a8a37cca3826efe2cde Mon Sep 17 00:00:00 2001 From: gofnnp Date: Tue, 24 Jan 2023 15:35:11 +0400 Subject: [PATCH] =?UTF-8?q?dev=20#13306=20=D0=BF=D0=BE=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D0=BB=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D1=8C=20?= =?UTF-8?q?=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20=D0=BF=D1=80=D0=B8=20?= =?UTF-8?q?=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B5=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=BA=D0=B0=D0=B7=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- angular/proxy.confi.json | 38 ++++++ angular/src/app/models/order-product.ts | 91 +++++++++---- angular/src/app/models/order.ts | 1 + angular/src/app/services/order.service.ts | 155 ++++++++++++++-------- angular/src/environments/environment.ts | 2 +- 5 files changed, 205 insertions(+), 82 deletions(-) create mode 100644 angular/proxy.confi.json diff --git a/angular/proxy.confi.json b/angular/proxy.confi.json new file mode 100644 index 0000000..f69607a --- /dev/null +++ b/angular/proxy.confi.json @@ -0,0 +1,38 @@ +{ + "/api": { + "target": "https://apple-push-notifications.it-retail.tech/apns/api", + "secure": false, + "pathRewrite": { + "^/api": "" + }, + "changeOrigin": true, + "logLevel": "debug" + }, + "/icard-proxy": { + "target": "https://p1.icard-proxy.crm4retail.ru", + "secure": false, + "pathRewrite": { + "^/icard-proxy": "" + }, + "changeOrigin": true, + "logLevel": "debug" + }, + "/static": { + "target": "https://sakura.lk.crm4retail.ru/static", + "secure": false, + "pathRewrite": { + "^/static": "" + }, + "changeOrigin": true, + "logLevel": "debug" + }, + "/it-retail": { + "target": "https://demo-stand.lk.crm4retail.ru/it-retail", + "secure": false, + "pathRewrite": { + "^/it-retail": "" + }, + "changeOrigin": true, + "logLevel": "debug" + } +} \ No newline at end of file diff --git a/angular/src/app/models/order-product.ts b/angular/src/app/models/order-product.ts index 2287d4d..200925f 100644 --- a/angular/src/app/models/order-product.ts +++ b/angular/src/app/models/order-product.ts @@ -1,8 +1,26 @@ -import {CartModifier, Modifier, Product} from "../interface/data"; -export class OrderProduct implements Product{ +import { CartModifier, Modifier, Product } from '../interface/data'; +export interface OrderProductToJson { + id: string; + amount: number; + price: number; + quantity: number; + name: string; + options?: OrderProductOption[]; +} - constructor(product: Product,guid: string, amount: number = 1) { +export interface OrderProductOption { + option: string; + variants: { + variant: string; + id: string; + quantity: number | undefined; + }[]; + id: string; +} + +export class OrderProduct implements Product { + constructor(product: Product, guid: string, amount: number = 1) { this.category_id = product.category_id; this.currency_symbol = product.currency_symbol; this.description = product.description; @@ -20,7 +38,6 @@ export class OrderProduct implements Product{ this.modifiers_group = product.modifiers_group; } - public amount: number; public category_id: number; public currency_symbol: string; @@ -37,34 +54,58 @@ export class OrderProduct implements Product{ public groupId: string; public modifiers_group: string[]; - - get finalPrice(): number{ - const modifiersPrice = this.modifier_data.reduce((previousValue, currentValue) => { - return previousValue + currentValue.options.reduce((previousOptionValue, currentOptionValue) => { - return previousOptionValue + Number(currentOptionValue.price ? currentOptionValue.price * (currentOptionValue.quantity ?? 0) : 0); - }, 0); - }, 0); + get finalPrice(): number { + const modifiersPrice = this.modifier_data.reduce( + (previousValue, currentValue) => { + return ( + previousValue + + currentValue.options.reduce( + (previousOptionValue, currentOptionValue) => { + return ( + previousOptionValue + + Number( + currentOptionValue.price + ? currentOptionValue.price * + (currentOptionValue.quantity ?? 0) + : 0 + ) + ); + }, + 0 + ) + ); + }, + 0 + ); return (Number(this.price) + modifiersPrice) * this.amount; } - toJson(){ - return { + toJson(): OrderProductToJson { + const product: OrderProductToJson = { id: this.id, amount: this.amount * this.price, price: this.price, - options: this.modifier_data?.map((modifier) => { - return { - option: modifier.name, - variants: modifier.options.map((option) => ({ - variant: option.name, - id: option.id, - quantity: option.quantity - })).filter((option) => option.quantity) || null, - id: modifier.id - } - }).filter((modifier) => modifier.variants.length), quantity: this.amount, name: this.name, - } + }; + const options = this.modifier_data + ?.map((modifier) => { + return { + option: modifier.name, + variants: + modifier.options + .map((option) => ({ + variant: option.name, + id: option.id, + quantity: option.quantity, + })) + .filter((option) => option.quantity) || null, + id: modifier.id, + }; + }) + .filter((modifier) => modifier.variants.length); + + if (options.length) product.options = options; + return product; } } diff --git a/angular/src/app/models/order.ts b/angular/src/app/models/order.ts index 2839718..df239dd 100644 --- a/angular/src/app/models/order.ts +++ b/angular/src/app/models/order.ts @@ -10,6 +10,7 @@ export interface OrderInfo { deliveryData?: DeliveryData; phone: string; token: string | undefined; + terminal_id: string; } export class Order { diff --git a/angular/src/app/services/order.service.ts b/angular/src/app/services/order.service.ts index 04790b6..e226764 100644 --- a/angular/src/app/services/order.service.ts +++ b/angular/src/app/services/order.service.ts @@ -1,14 +1,21 @@ -import {Injectable} from '@angular/core'; -import {CartService} from "./cart.service"; -import {WpJsonService} from "./wp-json.service"; -import {forkJoin, lastValueFrom, Observable, tap} from "rxjs"; -import {Cart, DeliveryData, DeliveryType, Modifier, Product, UserData} from "../interface/data"; -import {Order} from "../models/order"; -import {OrderProduct} from "../models/order-product"; -import {JsonrpcService, RpcService} from "./jsonrpc.service"; -import {CookiesService} from "./cookies.service"; -import {MessageService} from "primeng/api"; -import {map} from "rxjs/operators"; +import { Injectable } from '@angular/core'; +import { CartService } from './cart.service'; +import { WpJsonService } from './wp-json.service'; +import { forkJoin, lastValueFrom, Observable, tap } from 'rxjs'; +import { + Cart, + DeliveryData, + DeliveryType, + Modifier, + Product, + UserData, +} from '../interface/data'; +import { Order } from '../models/order'; +import { OrderProduct } from '../models/order-product'; +import { JsonrpcService, RpcService } from './jsonrpc.service'; +import { CookiesService } from './cookies.service'; +import { MessageService } from 'primeng/api'; +import { map } from 'rxjs/operators'; import { cloneDeep } from 'lodash'; import { environment } from 'src/environments/environment'; @@ -16,7 +23,6 @@ import { environment } from 'src/environments/environment'; providedIn: 'root', }) export class OrderService { - private order!: Order; constructor( @@ -24,48 +30,71 @@ export class OrderService { private wpJsonService: WpJsonService, private jsonRpcService: JsonrpcService, private cookiesService: CookiesService, - private messageService: MessageService, - ) { - } + private messageService: MessageService + ) {} async getDeliveryTypes(): Promise { return await lastValueFrom(this.wpJsonService.getDeliveryTypes()); } - async getOrder(refresh = false): Promise { if (!this.order || refresh) { const cart = this.cartService.getCart(); - + if (cart.products.length) { const products = await this.getProducts(cart); - const additionalInfo = this.jsonRpcService.rpc({ - method: 'getAdditionalInfo', - params: [] - }, RpcService.authService, true); - - const tokenData = this.jsonRpcService.rpc({ - method: 'getTokenData', - params: [this.cookiesService.getItem('token')], - }, RpcService.authService, true); - - const info = await lastValueFrom(forkJoin([additionalInfo, tokenData, products])); - const token = this.cookiesService.getItem('token') - this.order = new Order({products: products, userData: info[0]?.data, phone: info[1].data?.mobile_number, token: token}); + const additionalInfo = this.jsonRpcService.rpc( + { + method: 'getAdditionalInfo', + params: [], + }, + RpcService.authService, + true + ); + + const tokenData = this.jsonRpcService.rpc( + { + method: 'getTokenData', + params: [this.cookiesService.getItem('token')], + }, + RpcService.authService, + true + ); + + const info = await lastValueFrom( + forkJoin([additionalInfo, tokenData, products]) + ); + const token = this.cookiesService.getItem('token'); + const terminal = + JSON.parse( + this.cookiesService.getItem('selectedTerminal') || 'null' + ) || this.cartService.selectedTerminal$; + this.order = new Order({ + products: products, + userData: info[0]?.data, + phone: info[1].data?.mobile_number, + token: token, + terminal_id: terminal.id, + }); } else if (this.order) { - this.order.products.length = 0 + this.order.products.length = 0; } } return this.order; - } async getProducts(cart: Cart): Promise { - const terminal = JSON.parse(this.cookiesService.getItem('selectedTerminal') || 'null') || this.cartService.selectedTerminal$ - const allData = await lastValueFrom(this.wpJsonService.getAllData(`${terminal.label}${terminal.id}`)) - const products: OrderProduct[] = [] + const terminal = + JSON.parse(this.cookiesService.getItem('selectedTerminal') || 'null') || + this.cartService.selectedTerminal$; + const allData = await lastValueFrom( + this.wpJsonService.getAllData(`${terminal.label}${terminal.id}`) + ); + const products: OrderProduct[] = []; for (let i = 0; i < cart.products.length; i++) { - const productSub = allData.products.find((product: any) => product.id === cart.products[i].id) + const productSub = allData.products.find( + (product: any) => product.id === cart.products[i].id + ); const product = Object.assign(cloneDeep(cart.products[i]), { category_id: 0, price: productSub.price, @@ -77,16 +106,22 @@ export class OrderService { modifier_data: cart.products[i].modifiers, stock_status: 'instock', groupId: productSub.groupId, - modifiers_group: productSub.modifiers_group - }) - const orderProduct: OrderProduct = new OrderProduct(product, cart.products[i].guid, cart.products[i].amount) - products.push(orderProduct) + modifiers_group: productSub.modifiers_group, + }); + const orderProduct: OrderProduct = new OrderProduct( + product, + cart.products[i].guid, + cart.products[i].amount + ); + products.push(orderProduct); } - return products + return products; } removeFromCart(productGuid: string): void { - this.order.products = this.order.products.filter(value => value.guid !== productGuid); + this.order.products = this.order.products.filter( + (value) => value.guid !== productGuid + ); this.cartService.removeFromCart(productGuid); } @@ -99,19 +134,27 @@ export class OrderService { } submit(): Observable { - return this.wpJsonService.createOrder(this.order.toJson(), environment.webhookItRetail).pipe( - tap({ - next: (_) => { - this.jsonRpcService.rpc({ - method: 'updateAdditionalInfo', - params: [this.order.userData, this.order.deliveryData] - }, RpcService.authService, true).subscribe(); - this.messageService.add({ - severity:'success', - summary: 'Заказ создан', - }); - }, - }), - ); + return this.wpJsonService + .createOrder(this.order.toJson(), environment.webhookItRetail) + .pipe( + tap({ + next: (_) => { + this.jsonRpcService + .rpc( + { + method: 'updateAdditionalInfo', + params: [this.order.userData, this.order.deliveryData], + }, + RpcService.authService, + true + ) + .subscribe(); + this.messageService.add({ + severity: 'success', + summary: 'Заказ создан', + }); + }, + }) + ); } } diff --git a/angular/src/environments/environment.ts b/angular/src/environments/environment.ts index 81e096a..5dda9ac 100644 --- a/angular/src/environments/environment.ts +++ b/angular/src/environments/environment.ts @@ -20,7 +20,7 @@ export const environment = { version: packageJson.version, appleWalletEndpoint: 'http://192.168.0.179:4200/apns/api', appleWalletSecret: 'Token F5mbzEERAznGKVbB6l', - webhookItRetail: 'https://webhook.it-retail.tech/handlers/tillda/1eb3fb56-3c4c-43b7-9a04-ce532ab7548f', + webhookItRetail: 'http://192.168.0.14:4200/it-retail/handlers/tillda/1eb3fb56-3c4c-43b7-9a04-ce532ab7548f', icardProxy: 'http://192.168.0.14:4200/icard-proxy/', clientName: 'demo-stand', cities: ['Менделеевск'],