поправил модель данных при отправке заказа
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"; import { CartModifier, Modifier, Product } from '../interface/data';
export class OrderProduct implements Product{
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.category_id = product.category_id;
this.currency_symbol = product.currency_symbol; this.currency_symbol = product.currency_symbol;
this.description = product.description; this.description = product.description;
@ -20,7 +38,6 @@ export class OrderProduct implements Product{
this.modifiers_group = product.modifiers_group; this.modifiers_group = product.modifiers_group;
} }
public amount: number; public amount: number;
public category_id: number; public category_id: number;
public currency_symbol: string; public currency_symbol: string;
@ -37,34 +54,58 @@ export class OrderProduct implements Product{
public groupId: string; public groupId: string;
public modifiers_group: string[]; public modifiers_group: string[];
get finalPrice(): number {
get finalPrice(): number{ const modifiersPrice = this.modifier_data.reduce<number>(
const modifiersPrice = this.modifier_data.reduce<number>((previousValue, currentValue) => { (previousValue, currentValue) => {
return previousValue + currentValue.options.reduce<number>((previousOptionValue, currentOptionValue) => { return (
return previousOptionValue + Number(currentOptionValue.price ? currentOptionValue.price * (currentOptionValue.quantity ?? 0) : 0); previousValue +
}, 0); currentValue.options.reduce<number>(
}, 0); (previousOptionValue, currentOptionValue) => {
return (
previousOptionValue +
Number(
currentOptionValue.price
? currentOptionValue.price *
(currentOptionValue.quantity ?? 0)
: 0
)
);
},
0
)
);
},
0
);
return (Number(this.price) + modifiersPrice) * this.amount; return (Number(this.price) + modifiersPrice) * this.amount;
} }
toJson(){ toJson(): OrderProductToJson {
return { const product: OrderProductToJson = {
id: this.id, id: this.id,
amount: this.amount * this.price, amount: this.amount * this.price,
price: 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, quantity: this.amount,
name: this.name, 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; deliveryData?: DeliveryData;
phone: string; phone: string;
token: string | undefined; token: string | undefined;
terminal_id: string;
} }
export class Order { export class Order {

View File

@ -1,14 +1,21 @@
import {Injectable} from '@angular/core'; import { Injectable } from '@angular/core';
import {CartService} from "./cart.service"; import { CartService } from './cart.service';
import {WpJsonService} from "./wp-json.service"; import { WpJsonService } from './wp-json.service';
import {forkJoin, lastValueFrom, Observable, tap} from "rxjs"; import { forkJoin, lastValueFrom, Observable, tap } from 'rxjs';
import {Cart, DeliveryData, DeliveryType, Modifier, Product, UserData} from "../interface/data"; import {
import {Order} from "../models/order"; Cart,
import {OrderProduct} from "../models/order-product"; DeliveryData,
import {JsonrpcService, RpcService} from "./jsonrpc.service"; DeliveryType,
import {CookiesService} from "./cookies.service"; Modifier,
import {MessageService} from "primeng/api"; Product,
import {map} from "rxjs/operators"; 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 { cloneDeep } from 'lodash';
import { environment } from 'src/environments/environment'; import { environment } from 'src/environments/environment';
@ -16,7 +23,6 @@ import { environment } from 'src/environments/environment';
providedIn: 'root', providedIn: 'root',
}) })
export class OrderService { export class OrderService {
private order!: Order; private order!: Order;
constructor( constructor(
@ -24,48 +30,71 @@ export class OrderService {
private wpJsonService: WpJsonService, private wpJsonService: WpJsonService,
private jsonRpcService: JsonrpcService, private jsonRpcService: JsonrpcService,
private cookiesService: CookiesService, private cookiesService: CookiesService,
private messageService: MessageService, private messageService: MessageService
) { ) {}
}
async getDeliveryTypes(): Promise<DeliveryType[]> { async getDeliveryTypes(): Promise<DeliveryType[]> {
return await lastValueFrom(this.wpJsonService.getDeliveryTypes()); return await lastValueFrom(this.wpJsonService.getDeliveryTypes());
} }
async getOrder(refresh = false): Promise<Order> { async getOrder(refresh = false): Promise<Order> {
if (!this.order || refresh) { if (!this.order || refresh) {
const cart = this.cartService.getCart(); const cart = this.cartService.getCart();
if (cart.products.length) { if (cart.products.length) {
const products = await this.getProducts(cart); const products = await this.getProducts(cart);
const additionalInfo = this.jsonRpcService.rpc({ const additionalInfo = this.jsonRpcService.rpc(
method: 'getAdditionalInfo', {
params: [] method: 'getAdditionalInfo',
}, RpcService.authService, true); params: [],
},
const tokenData = this.jsonRpcService.rpc({ RpcService.authService,
method: 'getTokenData', true
params: [this.cookiesService.getItem('token')], );
}, RpcService.authService, true);
const tokenData = this.jsonRpcService.rpc(
const info = await lastValueFrom(forkJoin([additionalInfo, tokenData, products])); {
const token = this.cookiesService.getItem('token') method: 'getTokenData',
this.order = new Order({products: products, userData: info[0]?.data, phone: info[1].data?.mobile_number, token: token}); 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) { } else if (this.order) {
this.order.products.length = 0 this.order.products.length = 0;
} }
} }
return this.order; return this.order;
} }
async getProducts(cart: Cart): Promise<OrderProduct[]> { async getProducts(cart: Cart): Promise<OrderProduct[]> {
const terminal = JSON.parse(this.cookiesService.getItem('selectedTerminal') || 'null') || this.cartService.selectedTerminal$ const terminal =
const allData = await lastValueFrom(this.wpJsonService.getAllData(`${terminal.label}${terminal.id}`)) JSON.parse(this.cookiesService.getItem('selectedTerminal') || 'null') ||
const products: OrderProduct[] = [] 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++) { 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]), { const product = Object.assign(cloneDeep(cart.products[i]), {
category_id: 0, category_id: 0,
price: productSub.price, price: productSub.price,
@ -77,16 +106,22 @@ export class OrderService {
modifier_data: cart.products[i].modifiers, modifier_data: cart.products[i].modifiers,
stock_status: 'instock', stock_status: 'instock',
groupId: productSub.groupId, groupId: productSub.groupId,
modifiers_group: productSub.modifiers_group modifiers_group: productSub.modifiers_group,
}) });
const orderProduct: OrderProduct = new OrderProduct(product, cart.products[i].guid, cart.products[i].amount) const orderProduct: OrderProduct = new OrderProduct(
products.push(orderProduct) product,
cart.products[i].guid,
cart.products[i].amount
);
products.push(orderProduct);
} }
return products return products;
} }
removeFromCart(productGuid: string): void { 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); this.cartService.removeFromCart(productGuid);
} }
@ -99,19 +134,27 @@ export class OrderService {
} }
submit(): Observable<any> { submit(): Observable<any> {
return this.wpJsonService.createOrder(this.order.toJson(), environment.webhookItRetail).pipe( return this.wpJsonService
tap({ .createOrder(this.order.toJson(), environment.webhookItRetail)
next: (_) => { .pipe(
this.jsonRpcService.rpc({ tap({
method: 'updateAdditionalInfo', next: (_) => {
params: [this.order.userData, this.order.deliveryData] this.jsonRpcService
}, RpcService.authService, true).subscribe(); .rpc(
this.messageService.add({ {
severity:'success', method: 'updateAdditionalInfo',
summary: 'Заказ создан', 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, version: packageJson.version,
appleWalletEndpoint: 'http://192.168.0.179:4200/apns/api', appleWalletEndpoint: 'http://192.168.0.179:4200/apns/api',
appleWalletSecret: 'Token F5mbzEERAznGKVbB6l', 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/', icardProxy: 'http://192.168.0.14:4200/icard-proxy/',
clientName: 'demo-stand', clientName: 'demo-stand',
cities: ['Менделеевск'], cities: ['Менделеевск'],