Enhance order service for new API

This commit is contained in:
Rene Vergara 2023-05-08 13:33:49 -05:00
parent 96eb3eb7f6
commit 3810900036
Signed by: pitmutt
GPG key ID: 65122AD495A7F5B2
2 changed files with 11 additions and 10 deletions

View file

@ -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;

View file

@ -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<User>;
public ownerUpdate: Observable<Owner>;
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');