2021-10-25 17:49:50 +00:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Subject, BehaviorSubject, Observable } from 'rxjs';
|
|
|
|
import { HttpClient, HttpParams } from '@angular/common/http';
|
2021-10-25 20:01:06 +00:00
|
|
|
import { Order } from './order.model';
|
|
|
|
import { UserService } from '../user.service';
|
|
|
|
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-10-27 12:59:43 +00:00
|
|
|
private dataStore: { user: User, order: Order } = {
|
|
|
|
user:{
|
|
|
|
address: '',
|
|
|
|
session: '',
|
|
|
|
blocktime: 0
|
|
|
|
},
|
|
|
|
order: {
|
|
|
|
address: '',
|
|
|
|
session: '',
|
|
|
|
timestamp: '',
|
|
|
|
closed: false,
|
|
|
|
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();
|
|
|
|
public userUpdate: Observable<User>;
|
2021-10-25 17:49:50 +00:00
|
|
|
|
2021-10-25 20:01:06 +00:00
|
|
|
constructor(
|
|
|
|
private http: HttpClient,
|
|
|
|
public userService: UserService
|
|
|
|
) {
|
|
|
|
this.userUpdate = userService.userUpdate;
|
|
|
|
this.userUpdate.subscribe((user) => {
|
|
|
|
this.dataStore.user = user;
|
|
|
|
//console.log('OS: const', user);
|
|
|
|
this.getOrder(this.dataStore.user.session);
|
|
|
|
});
|
2021-10-25 17:49:50 +00:00
|
|
|
}
|
2021-10-25 20:01:06 +00:00
|
|
|
|
|
|
|
getOrder(session: string) {
|
|
|
|
const params = new HttpParams().append('session', session);
|
|
|
|
let obs = this.http.get<{message: string, order: any}>('http://localhost:3000/api/order', { headers:{}, 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);
|
|
|
|
} else {
|
|
|
|
console.log('No order found');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return obs;
|
|
|
|
}
|
|
|
|
|
2021-10-27 12:59:43 +00:00
|
|
|
addToOrder(lineItem: LineItem) {
|
|
|
|
if (this.dataStore.order.address.length == 0) {
|
|
|
|
console.log('No open order, creating...', lineItem);
|
|
|
|
this.createOrder();
|
|
|
|
} else {
|
|
|
|
console.log('Open order, adding...', lineItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
createOrder() {
|
|
|
|
var order:Order = {
|
|
|
|
address: this.dataStore.user.address,
|
|
|
|
session: this.dataStore.user.session,
|
|
|
|
closed: false,
|
|
|
|
lines: []
|
|
|
|
};
|
|
|
|
let obs = this.http.post<{message: string}>('http://localhost:3000/api/order', {order: order});
|
|
|
|
|
|
|
|
obs.subscribe((orderData) => {
|
|
|
|
console.log('Create order', orderData);
|
|
|
|
this.getOrder(this.dataStore.user.session);
|
|
|
|
});
|
|
|
|
|
|
|
|
return obs;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-25 17:49:50 +00:00
|
|
|
}
|