2021-10-25 17:49:50 +00:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Subject, BehaviorSubject, Observable } 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-10-27 12:59:43 +00:00
|
|
|
import { LineItem} from '../items/lineitem.model';
|
2021-10-25 17:49:50 +00:00
|
|
|
|
|
|
|
@Injectable({providedIn: 'root'})
|
|
|
|
|
|
|
|
export class OrderService {
|
2021-11-08 20:37:26 +00:00
|
|
|
beUrl = 'http://localhost:3000/';
|
2021-11-05 21:32:01 +00:00
|
|
|
private dataStore: {allOrders: Order[], user: User, order: Order } = {
|
|
|
|
allOrders: [],
|
2021-10-27 12:59:43 +00:00
|
|
|
user:{
|
|
|
|
address: '',
|
|
|
|
session: '',
|
|
|
|
blocktime: 0
|
|
|
|
},
|
|
|
|
order: {
|
|
|
|
address: '',
|
|
|
|
session: '',
|
|
|
|
timestamp: '',
|
|
|
|
closed: false,
|
2021-11-05 14:30:35 +00:00
|
|
|
price: 0,
|
|
|
|
total: 0,
|
|
|
|
totalZec: 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();
|
2021-10-25 20:01:06 +00:00
|
|
|
public userUpdate: Observable<User>;
|
2021-11-09 18:39:16 +00:00
|
|
|
private apiKey = 'Le2adeic8Thah4Aeng4daem6i';
|
|
|
|
private reqHeaders: HttpHeaders;
|
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
|
|
|
|
) {
|
2021-11-09 18:39:16 +00:00
|
|
|
this.reqHeaders = new HttpHeaders().set('Authorization', this.apiKey);
|
2021-10-25 20:01:06 +00:00
|
|
|
this.userUpdate = userService.userUpdate;
|
|
|
|
this.userUpdate.subscribe((user) => {
|
|
|
|
this.dataStore.user = user;
|
|
|
|
//console.log('OS: const', user);
|
2021-10-27 20:21:55 +00:00
|
|
|
this.getOrder();
|
2021-10-25 20:01:06 +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() {
|
|
|
|
var session = this.dataStore.user.session;
|
2021-10-25 20:01:06 +00:00
|
|
|
const params = new HttpParams().append('session', session);
|
2021-11-09 18:39:16 +00:00
|
|
|
let obs = this.http.get<{message: string, order: any}>(this.beUrl+'api/order', { headers:this.reqHeaders, params:params, 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.dataStore.order.total = 0;
|
2021-10-27 20:21:55 +00:00
|
|
|
for(var line of this.dataStore.order.lines) {
|
2021-11-05 14:30:35 +00:00
|
|
|
this.dataStore.order.total += line.qty * line.cost;
|
2021-10-27 20:21:55 +00:00
|
|
|
}
|
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;
|
|
|
|
const params = new HttpParams().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) {
|
2021-11-09 18:39:16 +00:00
|
|
|
let obs = this.http.post<{message: string}>(this.beUrl+'api/lineitem', { order_id: this.dataStore.order._id, line: lineItem }, { headers: this.reqHeaders });
|
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 = {
|
|
|
|
address: this.dataStore.user.address,
|
|
|
|
session: this.dataStore.user.session,
|
|
|
|
closed: false,
|
2021-11-05 14:30:35 +00:00
|
|
|
totalZec: 0,
|
|
|
|
price: 0,
|
|
|
|
total: 0,
|
2021-10-27 12:59:43 +00:00
|
|
|
lines: []
|
|
|
|
};
|
2021-11-09 18:39:16 +00:00
|
|
|
let obs = this.http.post<{message: string, order: Order}>(this.beUrl+'api/order', {order: order}, { headers: this.reqHeaders });
|
2021-10-27 12:59:43 +00:00
|
|
|
obs.subscribe((orderData) => {
|
|
|
|
console.log('Create order', orderData);
|
2021-10-27 20:21:55 +00:00
|
|
|
this.dataStore.order = orderData.order;
|
|
|
|
this._orderUpdated.next(Object.assign({}, this.dataStore).order);
|
|
|
|
this.addToOrder(lineItem);
|
2021-10-27 12:59:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return obs;
|
2021-10-27 20:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cancelOrder(id: string) {
|
2021-11-09 18:39:16 +00:00
|
|
|
let obs = this.http.delete<{message: string}>(this.beUrl+'api/order/'+id, { headers: this.reqHeaders });
|
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-05 14:30:35 +00:00
|
|
|
total: 0,
|
|
|
|
totalZec: 0,
|
|
|
|
price: 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
|
|
|
}
|
|
|
|
|
2021-11-02 15:35:22 +00:00
|
|
|
closeOrder(){
|
2021-11-05 14:30:35 +00:00
|
|
|
this.fullnodeService.priceUpdate.subscribe((price) => {
|
|
|
|
console.log('Price:', price);
|
|
|
|
this.dataStore.order.closed = true;
|
|
|
|
this.dataStore.order.price = price;
|
2021-11-09 18:39:16 +00:00
|
|
|
let obs = this.http.post<{message: string, order: Order}>(this.beUrl+'api/order', {order: this.dataStore.order}, { headers: this.reqHeaders });
|
2021-11-05 14:30:35 +00:00
|
|
|
obs.subscribe((orderData) => {
|
|
|
|
console.log('Closed order', orderData);
|
|
|
|
this.dataStore.order = {
|
|
|
|
address: '',
|
|
|
|
session: '',
|
|
|
|
timestamp: '',
|
|
|
|
closed: false,
|
|
|
|
price: 0,
|
|
|
|
total: 0,
|
|
|
|
totalZec: 0,
|
|
|
|
lines: [
|
|
|
|
{
|
|
|
|
qty: 1,
|
|
|
|
name: '',
|
|
|
|
cost:0
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
this._orderUpdated.next(Object.assign({}, this.dataStore).order);
|
|
|
|
});
|
|
|
|
return obs;
|
2021-11-02 15:35:22 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-25 17:49:50 +00:00
|
|
|
}
|