zgo/src/app/order/order.service.ts
2022-01-19 10:26:25 -06:00

224 lines
6 KiB
TypeScript

import { Injectable } from '@angular/core';
import { Subject, BehaviorSubject, Observable } from 'rxjs';
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { Order } from './order.model';
import { UserService } from '../user.service';
import { FullnodeService } from '../fullnode.service';
import { User } from '../user.model';
import { Owner } from '../owner.model';
import { LineItem} from '../items/lineitem.model';
@Injectable({providedIn: 'root'})
export class OrderService {
beUrl = 'http://localhost:3000/';
private dataStore: {allOrders: Order[], user: User, order: Order, owner: Owner } = {
allOrders: [],
user:{
address: '',
session: '',
blocktime: 0,
expiration: 0,
pin: '',
validated: false
},
owner: {
_id: '',
name: '',
address: '',
currency: 'usd',
tax: false,
taxValue: 0,
vat: false,
vatValue: 0,
email: '',
street: '',
city: '',
state: '',
postal: '',
phone: '',
paid: false,
website: '',
country: ''
},
order: {
address: '',
session: '',
timestamp: '',
closed: false,
currency: '',
price: 0,
total: 0,
totalZec: 0,
lines: [
{
qty: 1,
name: '',
cost:0
}
]
}
};
private _orderUpdated: BehaviorSubject<Order> = new BehaviorSubject(this.dataStore.order);
public readonly orderUpdate: Observable<Order> = this._orderUpdated.asObservable();
private _totalUpdated: BehaviorSubject<number> = new BehaviorSubject(this.dataStore.order.total);
public readonly totalUpdate: Observable<number> = this._totalUpdated.asObservable();
private _allOrdersUpdated: BehaviorSubject<Order[]> = new BehaviorSubject(this.dataStore.allOrders);
public readonly allOrdersUpdate: Observable<Order[]> = this._allOrdersUpdated.asObservable();
public userUpdate: Observable<User>;
public ownerUpdate: Observable<Owner>;
private apiKey = 'Le2adeic8Thah4Aeng4daem6i';
private reqHeaders: HttpHeaders;
constructor(
private http: HttpClient,
public fullnodeService: FullnodeService,
public userService: UserService
) {
this.reqHeaders = new HttpHeaders().set('Authorization', this.apiKey);
this.userUpdate = userService.userUpdate;
this.ownerUpdate = userService.ownerUpdate;
this.userUpdate.subscribe((user) => {
this.dataStore.user = user;
//console.log('OS: const', user);
this.getOrder();
});
this.ownerUpdate.subscribe((owner) => {
this.dataStore.owner = owner;
});
}
getOrder() {
var session = this.dataStore.user.session;
const params = new HttpParams().append('session', session);
let obs = this.http.get<{message: string, order: any}>(this.beUrl+'api/order', { headers:this.reqHeaders, params:params, observe: 'response'});
obs.subscribe((OrderDataResponse) => {
if (OrderDataResponse.status == 200) {
this.dataStore.order = OrderDataResponse.body!.order;
this._orderUpdated.next(Object.assign({}, this.dataStore).order);
this.dataStore.order.total = 0;
for(var line of this.dataStore.order.lines) {
this.dataStore.order.total += line.qty * line.cost;
}
this._totalUpdated.next(Object.assign({}, this.dataStore).order.total);
} else {
console.log('No order found');
}
});
return obs;
}
getAllOrders(){
var address = this.dataStore.user.address;
const params = new HttpParams().append('address', address);
let obs = this.http.get<{message: string, orders: any}>(this.beUrl+'api/allorders', { headers:this.reqHeaders, params:params, observe: 'response'});
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;
}
addToOrder(lineItem: LineItem) {
if(this.dataStore.order._id != null) {
let obs = this.http.post<{message: string}>(this.beUrl+'api/lineitem', { order_id: this.dataStore.order._id, line: lineItem }, { headers: this.reqHeaders });
obs.subscribe((orderData) => {
this.getOrder();
});
} else {
this.createOrder(lineItem);
}
}
createOrder(lineItem: LineItem) {
var order:Order = {
address: this.dataStore.user.address,
session: this.dataStore.user.session,
currency: this.dataStore.owner.currency,
closed: false,
totalZec: 0,
price: 0,
total: 0,
lines: []
};
let obs = this.http.post<{message: string, order: Order}>(this.beUrl+'api/order', {order: order}, { headers: this.reqHeaders });
obs.subscribe((orderData) => {
console.log('Create order', orderData);
this.dataStore.order = orderData.order;
this._orderUpdated.next(Object.assign({}, this.dataStore).order);
this.addToOrder(lineItem);
});
return obs;
}
cancelOrder(id: string) {
let obs = this.http.delete<{message: string}>(this.beUrl+'api/order/'+id, { headers: this.reqHeaders });
obs.subscribe((OrderResponse) => {
console.log('Order deleted');
//this.getOrder();
this.dataStore.order = {
address: '',
session: '',
timestamp: '',
closed: false,
currency: '',
total: 0,
totalZec: 0,
price: 0,
lines: [
{
qty: 1,
name: '',
cost:0
}
]
};
this._orderUpdated.next(Object.assign({}, this.dataStore).order);
});
return obs;
}
closeOrder(){
this.fullnodeService.priceUpdate.subscribe((price) => {
console.log('Price:', price);
this.dataStore.order.closed = true;
this.dataStore.order.price = price;
let obs = this.http.post<{message: string, order: Order}>(this.beUrl+'api/order', {order: this.dataStore.order}, { headers: this.reqHeaders });
obs.subscribe((orderData) => {
console.log('Closed order', orderData);
this.dataStore.order = {
address: '',
session: '',
timestamp: '',
closed: false,
currency: '',
price: 0,
total: 0,
totalZec: 0,
lines: [
{
qty: 1,
name: '',
cost:0
}
]
};
this._orderUpdated.next(Object.assign({}, this.dataStore).order);
});
return obs;
});
}
}