import { Injectable } from '@angular/core'; import { Subject, BehaviorSubject, Observable } from 'rxjs'; import { HttpClient, HttpParams } from '@angular/common/http'; import { Order } from './order.model'; import { UserService } from '../user.service'; import { User } from '../user.model'; import { LineItem} from '../items/lineitem.model'; @Injectable({providedIn: 'root'}) export class OrderService { private dataStore: { total:number, user: User, order: Order } = { total: 0, user:{ address: '', session: '', blocktime: 0 }, order: { address: '', session: '', timestamp: '', closed: false, lines: [ { qty: 1, name: '', cost:0 } ] } }; private _orderUpdated: BehaviorSubject = new BehaviorSubject(this.dataStore.order); public readonly orderUpdate: Observable = this._orderUpdated.asObservable(); private _totalUpdated: BehaviorSubject = new BehaviorSubject(this.dataStore.total); public readonly totalUpdate: Observable = this._totalUpdated.asObservable(); public userUpdate: Observable; 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(); }); } getOrder() { var session = this.dataStore.user.session; 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); this.dataStore.total = 0; for(var line of this.dataStore.order.lines) { this.dataStore.total += line.qty * line.cost; } this._totalUpdated.next(Object.assign({}, this.dataStore).total); } else { console.log('No order found'); } }); return obs; } addToOrder(lineItem: LineItem) { if(this.dataStore.order._id != null) { let obs = this.http.post<{message: string}>('http://localhost:3000/api/lineitem', { order_id: this.dataStore.order._id, line: lineItem }); 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, closed: false, lines: [] }; let obs = this.http.post<{message: string, order: Order}>('http://localhost:3000/api/order', {order: order}); 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}>('http://localhost:3000/api/order/'+id); obs.subscribe((OrderResponse) => { console.log('Order deleted'); //this.getOrder(); this.dataStore.order = { address: '', session: '', timestamp: '', closed: false, lines: [ { qty: 1, name: '', cost:0 } ] }; this._orderUpdated.next(Object.assign({}, this.dataStore).order); }); return obs; } closeOrder(){ this.dataStore.order.closed = true; let obs = this.http.post<{message: string, order: Order}>('http://localhost:3000/api/order', {order: this.dataStore.order}); obs.subscribe((orderData) => { console.log('Closed order', orderData); this.dataStore.order = { address: '', session: '', timestamp: '', closed: false, lines: [ { qty: 1, name: '', cost:0 } ] }; this._orderUpdated.next(Object.assign({}, this.dataStore).order); }); return obs; } }