2021-10-25 17:49:50 +00:00
|
|
|
import { Injectable } from '@angular/core';
|
2023-10-26 16:44:22 +00:00
|
|
|
import { BehaviorSubject, Observable, lastValueFrom } from 'rxjs';
|
2021-11-09 18:39:16 +00:00
|
|
|
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
|
2021-10-25 20:01:06 +00:00
|
|
|
import { Order } from './order.model';
|
|
|
|
import { UserService } from '../user.service';
|
2021-11-05 14:30:35 +00:00
|
|
|
import { FullnodeService } from '../fullnode.service';
|
2021-10-25 20:01:06 +00:00
|
|
|
import { User } from '../user.model';
|
2021-11-22 20:37:45 +00:00
|
|
|
import { Owner } from '../owner.model';
|
2022-07-18 00:46:56 +00:00
|
|
|
import { LineItem } from '../items/lineitem.model';
|
|
|
|
import { newLineItem } from '../items/newlineitem.model';
|
2021-10-25 17:49:50 +00:00
|
|
|
|
2022-07-12 18:22:17 +00:00
|
|
|
import { ConfigData } from '../configdata';
|
|
|
|
|
2022-05-18 20:51:39 +00:00
|
|
|
var Buffer = require('buffer/').Buffer;
|
|
|
|
|
2021-10-25 17:49:50 +00:00
|
|
|
@Injectable({providedIn: 'root'})
|
|
|
|
|
|
|
|
export class OrderService {
|
2022-07-12 18:22:17 +00:00
|
|
|
beUrl = ConfigData.Be_URL;
|
2021-11-22 20:37:45 +00:00
|
|
|
private dataStore: {allOrders: Order[], user: User, order: Order, owner: Owner } = {
|
2021-11-05 21:32:01 +00:00
|
|
|
allOrders: [],
|
2021-10-27 12:59:43 +00:00
|
|
|
user:{
|
|
|
|
address: '',
|
|
|
|
session: '',
|
2021-11-11 15:18:38 +00:00
|
|
|
blocktime: 0,
|
|
|
|
pin: '',
|
|
|
|
validated: false
|
2021-10-27 12:59:43 +00:00
|
|
|
},
|
2021-11-22 20:37:45 +00:00
|
|
|
owner: {
|
|
|
|
_id: '',
|
|
|
|
name: '',
|
|
|
|
address: '',
|
|
|
|
currency: 'usd',
|
|
|
|
tax: false,
|
|
|
|
taxValue: 0,
|
|
|
|
vat: false,
|
2022-01-17 20:49:08 +00:00
|
|
|
vatValue: 0,
|
2022-01-18 22:40:20 +00:00
|
|
|
paid: false,
|
2022-05-18 20:51:39 +00:00
|
|
|
zats: false,
|
|
|
|
invoices: false,
|
2022-07-14 16:11:04 +00:00
|
|
|
expiration: new Date(Date.now()).toISOString(),
|
2022-07-18 20:29:27 +00:00
|
|
|
payconf: false,
|
2023-05-15 14:48:57 +00:00
|
|
|
crmToken: '',
|
2023-10-20 13:26:37 +00:00
|
|
|
viewkey: '',
|
|
|
|
tips: false
|
2021-11-22 20:37:45 +00:00
|
|
|
},
|
2021-10-27 12:59:43 +00:00
|
|
|
order: {
|
|
|
|
address: '',
|
|
|
|
session: '',
|
|
|
|
timestamp: '',
|
|
|
|
closed: false,
|
2021-11-22 20:37:45 +00:00
|
|
|
currency: '',
|
2021-11-05 14:30:35 +00:00
|
|
|
price: 0,
|
|
|
|
total: 0,
|
|
|
|
totalZec: 0,
|
2022-05-24 18:18:46 +00:00
|
|
|
paid: false,
|
2022-08-10 19:12:28 +00:00
|
|
|
externalInvoice: '',
|
|
|
|
shortCode: '',
|
2023-06-19 15:30:41 +00:00
|
|
|
token: '',
|
2023-10-22 13:08:18 +00:00
|
|
|
taxAmount: 0,
|
|
|
|
vatAmount: 0,
|
|
|
|
tipAmount: 0,
|
2021-10-27 12:59:43 +00:00
|
|
|
lines: [
|
|
|
|
{
|
|
|
|
qty: 1,
|
|
|
|
name: '',
|
|
|
|
cost:0
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
2021-10-25 20:01:06 +00:00
|
|
|
private _orderUpdated: BehaviorSubject<Order> = new BehaviorSubject(this.dataStore.order);
|
|
|
|
public readonly orderUpdate: Observable<Order> = this._orderUpdated.asObservable();
|
2021-11-05 14:30:35 +00:00
|
|
|
private _totalUpdated: BehaviorSubject<number> = new BehaviorSubject(this.dataStore.order.total);
|
2021-10-27 20:21:55 +00:00
|
|
|
public readonly totalUpdate: Observable<number> = this._totalUpdated.asObservable();
|
2021-11-05 21:32:01 +00:00
|
|
|
private _allOrdersUpdated: BehaviorSubject<Order[]> = new BehaviorSubject(this.dataStore.allOrders);
|
|
|
|
public readonly allOrdersUpdate: Observable<Order[]> = this._allOrdersUpdated.asObservable();
|
2023-10-22 13:08:18 +00:00
|
|
|
private _taxUpdated: BehaviorSubject<number> = new BehaviorSubject(this.dataStore.owner.taxValue);
|
|
|
|
public readonly taxUpdate: Observable<number> = this._taxUpdated.asObservable();
|
|
|
|
private _vatUpdated: BehaviorSubject<number> = new BehaviorSubject(this.dataStore.owner.vatValue);
|
|
|
|
public readonly vatUpdate: Observable<number> = this._vatUpdated.asObservable();
|
2023-10-26 16:44:22 +00:00
|
|
|
private _tipUpdated: BehaviorSubject<boolean> = new BehaviorSubject(this.dataStore.owner.tips);
|
|
|
|
public readonly tipUpdate: Observable<boolean> = this._tipUpdated.asObservable();
|
2021-10-25 20:01:06 +00:00
|
|
|
public userUpdate: Observable<User>;
|
2021-11-22 20:37:45 +00:00
|
|
|
public ownerUpdate: Observable<Owner>;
|
2021-11-09 18:39:16 +00:00
|
|
|
private reqHeaders: HttpHeaders;
|
2023-05-08 18:33:49 +00:00
|
|
|
private reqParams: HttpParams;
|
|
|
|
private session: null|string;
|
2021-10-25 17:49:50 +00:00
|
|
|
|
2021-10-25 20:01:06 +00:00
|
|
|
constructor(
|
|
|
|
private http: HttpClient,
|
2021-11-05 14:30:35 +00:00
|
|
|
public fullnodeService: FullnodeService,
|
2021-10-25 20:01:06 +00:00
|
|
|
public userService: UserService
|
|
|
|
) {
|
2022-07-12 18:22:17 +00:00
|
|
|
var auth = 'Basic ' + Buffer.from(ConfigData.UsrPwd).toString('base64');
|
2023-05-08 18:33:49 +00:00
|
|
|
this.session = localStorage.getItem('s4z_token');
|
2022-05-18 20:51:39 +00:00
|
|
|
this.reqHeaders = new HttpHeaders().set('Authorization', auth);
|
2023-05-08 18:33:49 +00:00
|
|
|
this.reqParams = new HttpParams().append('session', this.session!);
|
2021-10-25 20:01:06 +00:00
|
|
|
this.userUpdate = userService.userUpdate;
|
2021-11-22 20:37:45 +00:00
|
|
|
this.ownerUpdate = userService.ownerUpdate;
|
2021-10-25 20:01:06 +00:00
|
|
|
this.userUpdate.subscribe((user) => {
|
|
|
|
this.dataStore.user = user;
|
2021-10-27 20:21:55 +00:00
|
|
|
this.getOrder();
|
2021-10-25 20:01:06 +00:00
|
|
|
});
|
2021-11-22 20:37:45 +00:00
|
|
|
this.ownerUpdate.subscribe((owner) => {
|
|
|
|
this.dataStore.owner = owner;
|
2023-10-22 13:08:18 +00:00
|
|
|
if (this.dataStore.owner.tax) {
|
|
|
|
this._taxUpdated.next(Object.assign({}, this.dataStore).owner.taxValue);
|
|
|
|
} else {
|
|
|
|
this._taxUpdated.next(0);
|
|
|
|
}
|
|
|
|
if (this.dataStore.owner.vat) {
|
|
|
|
this._vatUpdated.next(Object.assign({}, this.dataStore).owner.vatValue);
|
|
|
|
} else {
|
|
|
|
this._vatUpdated.next(0);
|
|
|
|
}
|
2023-10-26 16:44:22 +00:00
|
|
|
this._tipUpdated.next(Object.assign({}, this.dataStore).owner.tips);
|
2021-11-22 20:37:45 +00:00
|
|
|
});
|
2021-10-25 17:49:50 +00:00
|
|
|
}
|
2021-10-25 20:01:06 +00:00
|
|
|
|
2021-10-27 20:21:55 +00:00
|
|
|
getOrder() {
|
2023-05-08 18:33:49 +00:00
|
|
|
let obs = this.http.get<{message: string, order: any}>(this.beUrl+'api/order', { headers:this.reqHeaders, params: this.reqParams, observe: 'response'});
|
2021-10-25 20:01:06 +00:00
|
|
|
|
|
|
|
obs.subscribe((OrderDataResponse) => {
|
|
|
|
if (OrderDataResponse.status == 200) {
|
|
|
|
this.dataStore.order = OrderDataResponse.body!.order;
|
|
|
|
this._orderUpdated.next(Object.assign({}, this.dataStore).order);
|
2021-11-05 14:30:35 +00:00
|
|
|
this._totalUpdated.next(Object.assign({}, this.dataStore).order.total);
|
2021-10-25 20:01:06 +00:00
|
|
|
} else {
|
|
|
|
console.log('No order found');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return obs;
|
|
|
|
}
|
|
|
|
|
2021-11-05 21:32:01 +00:00
|
|
|
getAllOrders(){
|
|
|
|
var address = this.dataStore.user.address;
|
2023-05-08 18:33:49 +00:00
|
|
|
const params = this.reqParams.append('address', address);
|
2021-11-09 18:39:16 +00:00
|
|
|
let obs = this.http.get<{message: string, orders: any}>(this.beUrl+'api/allorders', { headers:this.reqHeaders, params:params, observe: 'response'});
|
2021-11-05 21:32:01 +00:00
|
|
|
obs.subscribe((OrdersData) => {
|
|
|
|
if (OrdersData.status == 200 ){
|
|
|
|
console.log('getAllOrder:', OrdersData.body);
|
|
|
|
this.dataStore.allOrders = OrdersData.body!.orders;
|
|
|
|
this._allOrdersUpdated.next(Object.assign({}, this.dataStore).allOrders);
|
|
|
|
} else {
|
|
|
|
console.log('No orders');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return obs;
|
|
|
|
}
|
|
|
|
|
2021-10-27 12:59:43 +00:00
|
|
|
addToOrder(lineItem: LineItem) {
|
2021-10-27 20:21:55 +00:00
|
|
|
if(this.dataStore.order._id != null) {
|
2022-05-18 20:51:39 +00:00
|
|
|
this.dataStore.order.lines.push(lineItem);
|
2023-05-08 18:33:49 +00:00
|
|
|
let obs = this.http.post(this.beUrl+'api/order', { payload: this.dataStore.order }, { headers: this.reqHeaders, params: this.reqParams });
|
2021-10-27 20:21:55 +00:00
|
|
|
obs.subscribe((orderData) => {
|
|
|
|
this.getOrder();
|
|
|
|
});
|
2021-10-27 12:59:43 +00:00
|
|
|
} else {
|
2021-10-27 20:21:55 +00:00
|
|
|
this.createOrder(lineItem);
|
2021-10-27 12:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 20:21:55 +00:00
|
|
|
createOrder(lineItem: LineItem) {
|
2021-10-27 12:59:43 +00:00
|
|
|
var order:Order = {
|
2022-05-18 20:51:39 +00:00
|
|
|
_id: '',
|
2021-10-27 12:59:43 +00:00
|
|
|
address: this.dataStore.user.address,
|
|
|
|
session: this.dataStore.user.session,
|
2021-11-22 20:37:45 +00:00
|
|
|
currency: this.dataStore.owner.currency,
|
2022-05-18 20:51:39 +00:00
|
|
|
timestamp: new Date(Date.now()).toISOString(),
|
2021-10-27 12:59:43 +00:00
|
|
|
closed: false,
|
2021-11-05 14:30:35 +00:00
|
|
|
totalZec: 0,
|
|
|
|
price: 0,
|
|
|
|
total: 0,
|
2022-05-24 18:18:46 +00:00
|
|
|
paid: false,
|
2022-08-10 19:12:28 +00:00
|
|
|
externalInvoice: '',
|
|
|
|
shortCode: '',
|
2023-06-19 15:30:41 +00:00
|
|
|
token: '',
|
2023-10-26 16:44:22 +00:00
|
|
|
taxAmount: 0,
|
|
|
|
vatAmount: 0,
|
2023-10-22 13:08:18 +00:00
|
|
|
tipAmount: 0,
|
2022-05-18 20:51:39 +00:00
|
|
|
lines: [lineItem]
|
2021-10-27 12:59:43 +00:00
|
|
|
};
|
2023-05-08 18:33:49 +00:00
|
|
|
let obs = this.http.post<{message: string, order: Order}>(this.beUrl+'api/order', {payload: order}, { headers: this.reqHeaders, params: this.reqParams });
|
2021-10-27 12:59:43 +00:00
|
|
|
obs.subscribe((orderData) => {
|
2022-05-18 20:51:39 +00:00
|
|
|
this.getOrder()
|
2021-10-27 12:59:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return obs;
|
2021-10-27 20:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cancelOrder(id: string) {
|
2023-05-08 18:33:49 +00:00
|
|
|
let obs = this.http.delete<{message: string}>(this.beUrl+'api/order/'+id, { headers: this.reqHeaders, params: this.reqParams });
|
2021-10-27 12:59:43 +00:00
|
|
|
|
2021-10-27 20:21:55 +00:00
|
|
|
obs.subscribe((OrderResponse) => {
|
|
|
|
console.log('Order deleted');
|
|
|
|
//this.getOrder();
|
|
|
|
this.dataStore.order = {
|
|
|
|
address: '',
|
|
|
|
session: '',
|
|
|
|
timestamp: '',
|
|
|
|
closed: false,
|
2021-11-22 20:37:45 +00:00
|
|
|
currency: '',
|
2021-11-05 14:30:35 +00:00
|
|
|
total: 0,
|
|
|
|
totalZec: 0,
|
|
|
|
price: 0,
|
2022-05-24 18:18:46 +00:00
|
|
|
paid: false,
|
2022-08-10 19:12:28 +00:00
|
|
|
externalInvoice: '',
|
|
|
|
shortCode: '',
|
2023-06-19 15:30:41 +00:00
|
|
|
token: '',
|
2023-10-22 13:08:18 +00:00
|
|
|
taxAmount: 0,
|
|
|
|
vatAmount: 0,
|
|
|
|
tipAmount: 0,
|
2021-10-27 20:21:55 +00:00
|
|
|
lines: [
|
|
|
|
{
|
|
|
|
qty: 1,
|
|
|
|
name: '',
|
|
|
|
cost:0
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
this._orderUpdated.next(Object.assign({}, this.dataStore).order);
|
|
|
|
});
|
|
|
|
|
|
|
|
return obs;
|
2021-10-27 12:59:43 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 16:44:22 +00:00
|
|
|
async updateTip(amt: number){
|
|
|
|
this.dataStore.order.tipAmount = amt;
|
|
|
|
let obs = this.http.post(this.beUrl+'api/order', {payload: this.dataStore.order}, {headers: this.reqHeaders, params: this.reqParams, observe: 'response'});
|
|
|
|
const res1 = await lastValueFrom(obs);
|
|
|
|
return res1;
|
|
|
|
}
|
|
|
|
|
2022-05-24 18:18:46 +00:00
|
|
|
closeOrder(paid: boolean){
|
2021-11-05 14:30:35 +00:00
|
|
|
this.fullnodeService.priceUpdate.subscribe((price) => {
|
|
|
|
this.dataStore.order.price = price;
|
2021-11-02 15:35:22 +00:00
|
|
|
});
|
2022-01-31 21:32:46 +00:00
|
|
|
this.dataStore.order.closed = true;
|
2022-05-24 18:18:46 +00:00
|
|
|
this.dataStore.order.paid = paid;
|
2023-05-15 14:48:57 +00:00
|
|
|
let obs = this.http.post(this.beUrl+'api/order', {payload: this.dataStore.order}, { headers: this.reqHeaders, params: this.reqParams });
|
2022-01-31 21:32:46 +00:00
|
|
|
obs.subscribe((orderData) => {
|
2023-10-22 13:08:18 +00:00
|
|
|
//console.log('Closed order', orderData);
|
2022-01-31 21:32:46 +00:00
|
|
|
this.dataStore.order = {
|
|
|
|
address: '',
|
|
|
|
session: '',
|
|
|
|
timestamp: '',
|
|
|
|
closed: false,
|
|
|
|
currency: '',
|
|
|
|
price: 0,
|
|
|
|
total: 0,
|
|
|
|
totalZec: 0,
|
2022-05-24 18:18:46 +00:00
|
|
|
paid: false,
|
2022-08-10 19:12:28 +00:00
|
|
|
externalInvoice: '',
|
|
|
|
shortCode: '',
|
2023-06-19 15:30:41 +00:00
|
|
|
token: '',
|
2023-10-22 13:08:18 +00:00
|
|
|
taxAmount: 0,
|
|
|
|
vatAmount: 0,
|
|
|
|
tipAmount: 0,
|
2022-01-31 21:32:46 +00:00
|
|
|
lines: [
|
|
|
|
{
|
|
|
|
qty: 1,
|
|
|
|
name: '',
|
|
|
|
cost:0
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
this._orderUpdated.next(Object.assign({}, this.dataStore).order);
|
|
|
|
});
|
|
|
|
return obs;
|
2021-11-02 15:35:22 +00:00
|
|
|
}
|
|
|
|
|
2022-07-18 00:46:56 +00:00
|
|
|
updateOrder(itemid: number, iLines : newLineItem[]) {
|
|
|
|
if(this.dataStore.order._id != null) {
|
|
|
|
|
|
|
|
var item = {} as LineItem;
|
|
|
|
this.dataStore.order.lines = [];
|
|
|
|
for ( let i = 0; i < iLines.length; i++)
|
|
|
|
{
|
|
|
|
if ( i !== itemid )
|
|
|
|
{
|
|
|
|
item = this.fillItemData(iLines[i].name, iLines[i].qty, iLines[i].cost);
|
|
|
|
// console.log("Push => ", item);
|
|
|
|
this.dataStore.order.lines.push(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let obs = this.http.post(this.beUrl+'api/order', { payload: this.dataStore.order }, { headers: this.reqHeaders });
|
|
|
|
obs.subscribe((orderData) => {
|
|
|
|
this.getOrder();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fillItemData(iname:string, iqty: number, icost: number) : LineItem {
|
|
|
|
|
|
|
|
const a = { name: iname,
|
|
|
|
qty: iqty,
|
|
|
|
cost: icost } as LineItem;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-25 17:49:50 +00:00
|
|
|
}
|