From 4c5d4c311f9952b550ba5e163f0124354e2337f9 Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Fri, 5 Nov 2021 09:30:35 -0500 Subject: [PATCH] Add totals and price to order storage --- backend/app.js | 3 ++ backend/models/order.js | 2 + src/app/order/order.component.ts | 6 ++- src/app/order/order.model.ts | 2 + src/app/order/order.service.ts | 68 ++++++++++++++++++------------ src/app/viewer/viewer.component.ts | 1 - 6 files changed, 53 insertions(+), 29 deletions(-) diff --git a/backend/app.js b/backend/app.js index 2592846..e58d7ca 100644 --- a/backend/app.js +++ b/backend/app.js @@ -390,6 +390,9 @@ app.post('/api/order', (req, res, next) => { ordermodel.findByIdAndUpdate(req.body.order._id, { address: req.body.order.address, session: req.body.order.session, + price: req.body.order.price, + total: req.body.order.total, + totalZec: req.body.order.totalZec, closed: req.body.order.closed }, function(err, docs) { if(err) { diff --git a/backend/models/order.js b/backend/models/order.js index 8abf13f..64110f9 100644 --- a/backend/models/order.js +++ b/backend/models/order.js @@ -6,6 +6,8 @@ const orderSchema = mongoose.Schema({ timestamp: {type: Date, required: true, default: Date.now}, closed: { type: Boolean, required: true, default:false }, price: { type: Number, required: true}, + total: { type: Number}, + totalZec: {type: Number}, lines: [{ qty: {type: Number, required: true, default: 1}, name: { type: String, required: true}, diff --git a/src/app/order/order.component.ts b/src/app/order/order.component.ts index abfb70f..2f9987e 100644 --- a/src/app/order/order.component.ts +++ b/src/app/order/order.component.ts @@ -15,7 +15,7 @@ import { CheckoutComponent} from '../checkout/checkout.component'; }) export class OrderComponent implements OnInit{ - public order: Order = {address: '', session: '', timestamp: '', closed: false, lines: [{qty: 1, name: '', cost: 0}]}; + public order: Order = {address: '', session: '', timestamp: '', closed: false, price:0, total:0, totalZec: 0, lines: [{qty: 1, name: '', cost: 0}]}; public price: number = 1; public total: number = 0; public orderUpdate: Observable; @@ -65,12 +65,14 @@ export class OrderComponent implements OnInit{ } checkout() { + var zec = this.total/this.price; + this.order.totalZec = parseFloat(zec.toFixed(6)); const dialogConfig = new MatDialogConfig(); dialogConfig.disableClose = true; dialogConfig.autoFocus = true; dialogConfig.data = { - totalZec: this.total/this.price, + totalZec: this.order.totalZec, addr: this.order.address, orderId: this.order._id }; diff --git a/src/app/order/order.model.ts b/src/app/order/order.model.ts index efbd4df..5c5a1f2 100644 --- a/src/app/order/order.model.ts +++ b/src/app/order/order.model.ts @@ -7,5 +7,7 @@ export interface Order { timestamp?: string, closed: boolean, price?: number, + total: number, + totalZec: number, lines: LineItem[] } diff --git a/src/app/order/order.service.ts b/src/app/order/order.service.ts index 9aa40cc..b40321a 100644 --- a/src/app/order/order.service.ts +++ b/src/app/order/order.service.ts @@ -3,14 +3,14 @@ import { Subject, BehaviorSubject, Observable } from 'rxjs'; import { HttpClient, HttpParams } 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 { LineItem} from '../items/lineitem.model'; @Injectable({providedIn: 'root'}) export class OrderService { - private dataStore: { total:number, user: User, order: Order } = { - total: 0, + private dataStore: { user: User, order: Order } = { user:{ address: '', session: '', @@ -21,6 +21,9 @@ export class OrderService { session: '', timestamp: '', closed: false, + price: 0, + total: 0, + totalZec: 0, lines: [ { qty: 1, @@ -32,12 +35,13 @@ export class OrderService { }; private _orderUpdated: BehaviorSubject = new BehaviorSubject(this.dataStore.order); public readonly orderUpdate: Observable = this._orderUpdated.asObservable(); - private _totalUpdated: BehaviorSubject = new BehaviorSubject(this.dataStore.total); + private _totalUpdated: BehaviorSubject = new BehaviorSubject(this.dataStore.order.total); public readonly totalUpdate: Observable = this._totalUpdated.asObservable(); public userUpdate: Observable; constructor( private http: HttpClient, + public fullnodeService: FullnodeService, public userService: UserService ) { this.userUpdate = userService.userUpdate; @@ -57,11 +61,11 @@ export class OrderService { if (OrderDataResponse.status == 200) { this.dataStore.order = OrderDataResponse.body!.order; this._orderUpdated.next(Object.assign({}, this.dataStore).order); - this.dataStore.total = 0; + this.dataStore.order.total = 0; for(var line of this.dataStore.order.lines) { - this.dataStore.total += line.qty * line.cost; + this.dataStore.order.total += line.qty * line.cost; } - this._totalUpdated.next(Object.assign({}, this.dataStore).total); + this._totalUpdated.next(Object.assign({}, this.dataStore).order.total); } else { console.log('No order found'); } @@ -86,6 +90,9 @@ export class OrderService { address: this.dataStore.user.address, session: this.dataStore.user.session, closed: false, + totalZec: 0, + price: 0, + total: 0, lines: [] }; let obs = this.http.post<{message: string, order: Order}>('http://localhost:3000/api/order', {order: order}); @@ -110,6 +117,9 @@ export class OrderService { session: '', timestamp: '', closed: false, + total: 0, + totalZec: 0, + price: 0, lines: [ { qty: 1, @@ -125,27 +135,33 @@ export class OrderService { } 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); + 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}>('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, + price: 0, + total: 0, + totalZec: 0, + lines: [ + { + qty: 1, + name: '', + cost:0 + } + ] + }; + this._orderUpdated.next(Object.assign({}, this.dataStore).order); + }); + return obs; }); - - return obs; } } diff --git a/src/app/viewer/viewer.component.ts b/src/app/viewer/viewer.component.ts index 746ea94..fbbb81c 100644 --- a/src/app/viewer/viewer.component.ts +++ b/src/app/viewer/viewer.component.ts @@ -37,7 +37,6 @@ export class ViewerComponent implements OnInit { } ngOnDestroy(){ - this.ownerUpdate.unsubscribe(); } }