From 38109000360fb285948c3140648a7edf944579b6 Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Mon, 8 May 2023 13:33:49 -0500 Subject: [PATCH] Enhance order service for new API --- src/app/order/order.component.ts | 2 +- src/app/order/order.service.ts | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/app/order/order.component.ts b/src/app/order/order.component.ts index c5ffc7e..dca1283 100644 --- a/src/app/order/order.component.ts +++ b/src/app/order/order.component.ts @@ -106,7 +106,7 @@ export class OrderComponent implements OnInit{ this.orderUpdate.subscribe((order) => { this.order = order; - console.log('this.order > ' + JSON.stringify(this.order)); + //console.log('this.order > ' + JSON.stringify(this.order)); // ------------------------------------------------ this.oLines = []; this.myLines = this.order.lines; diff --git a/src/app/order/order.service.ts b/src/app/order/order.service.ts index d52f419..fa2b663 100644 --- a/src/app/order/order.service.ts +++ b/src/app/order/order.service.ts @@ -1,5 +1,5 @@ import { Injectable } from '@angular/core'; -import { Subject, BehaviorSubject, Observable } from 'rxjs'; +import { BehaviorSubject, Observable } from 'rxjs'; import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http'; import { Order } from './order.model'; import { UserService } from '../user.service'; @@ -83,6 +83,8 @@ export class OrderService { public userUpdate: Observable; public ownerUpdate: Observable; private reqHeaders: HttpHeaders; + private reqParams: HttpParams; + private session: null|string; constructor( private http: HttpClient, @@ -90,12 +92,13 @@ export class OrderService { public userService: UserService ) { var auth = 'Basic ' + Buffer.from(ConfigData.UsrPwd).toString('base64'); + this.session = localStorage.getItem('s4z_token'); this.reqHeaders = new HttpHeaders().set('Authorization', auth); + this.reqParams = new HttpParams().append('session', this.session!); 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) => { @@ -104,9 +107,7 @@ export class OrderService { } 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'}); + let obs = this.http.get<{message: string, order: any}>(this.beUrl+'api/order', { headers:this.reqHeaders, params: this.reqParams, observe: 'response'}); obs.subscribe((OrderDataResponse) => { if (OrderDataResponse.status == 200) { @@ -127,7 +128,7 @@ export class OrderService { getAllOrders(){ var address = this.dataStore.user.address; - const params = new HttpParams().append('address', address); + const params = this.reqParams.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 ){ @@ -145,7 +146,7 @@ export class OrderService { addToOrder(lineItem: LineItem) { if(this.dataStore.order._id != null) { this.dataStore.order.lines.push(lineItem); - let obs = this.http.post(this.beUrl+'api/order', { payload: this.dataStore.order }, { headers: this.reqHeaders }); + let obs = this.http.post(this.beUrl+'api/order', { payload: this.dataStore.order }, { headers: this.reqHeaders, params: this.reqParams }); obs.subscribe((orderData) => { this.getOrder(); }); @@ -170,7 +171,7 @@ export class OrderService { shortCode: '', lines: [lineItem] }; - let obs = this.http.post<{message: string, order: Order}>(this.beUrl+'api/order', {payload: order}, { headers: this.reqHeaders }); + let obs = this.http.post<{message: string, order: Order}>(this.beUrl+'api/order', {payload: order}, { headers: this.reqHeaders, params: this.reqParams }); obs.subscribe((orderData) => { console.log('Created order'); this.getOrder() @@ -180,7 +181,7 @@ export class OrderService { } cancelOrder(id: string) { - let obs = this.http.delete<{message: string}>(this.beUrl+'api/order/'+id, { headers: this.reqHeaders }); + let obs = this.http.delete<{message: string}>(this.beUrl+'api/order/'+id, { headers: this.reqHeaders, params: this.reqParams }); obs.subscribe((OrderResponse) => { console.log('Order deleted');