поправил модель данных при отправке заказа
This commit is contained in:
gofnnp 2023-01-24 15:35:11 +04:00
parent 2e3da3d0b2
commit df49c2a77e
5 changed files with 205 additions and 82 deletions

38
angular/proxy.confi.json Normal file
View File

@ -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"
}
}

View File

@ -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<number>((previousValue, currentValue) => {
return previousValue + currentValue.options.reduce<number>((previousOptionValue, currentOptionValue) => {
return previousOptionValue + Number(currentOptionValue.price ? currentOptionValue.price * (currentOptionValue.quantity ?? 0) : 0);
}, 0);
}, 0);
get finalPrice(): number {
const modifiersPrice = this.modifier_data.reduce<number>(
(previousValue, currentValue) => {
return (
previousValue +
currentValue.options.reduce<number>(
(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;
}
}

View File

@ -10,6 +10,7 @@ export interface OrderInfo {
deliveryData?: DeliveryData;
phone: string;
token: string | undefined;
terminal_id: string;
}
export class Order {

View File

@ -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<DeliveryType[]> {
return await lastValueFrom(this.wpJsonService.getDeliveryTypes());
}
async getOrder(refresh = false): Promise<Order> {
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<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[] = []
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<any> {
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: 'Заказ создан',
});
},
})
);
}
}

View File

@ -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: ['Менделеевск'],