Add totals and price to order storage

This commit is contained in:
Rene Vergara 2021-11-05 09:30:35 -05:00
parent 7e5210723e
commit 4c5d4c311f
6 changed files with 53 additions and 29 deletions

View file

@ -390,6 +390,9 @@ app.post('/api/order', (req, res, next) => {
ordermodel.findByIdAndUpdate(req.body.order._id, { ordermodel.findByIdAndUpdate(req.body.order._id, {
address: req.body.order.address, address: req.body.order.address,
session: req.body.order.session, 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 closed: req.body.order.closed
}, function(err, docs) { }, function(err, docs) {
if(err) { if(err) {

View file

@ -6,6 +6,8 @@ const orderSchema = mongoose.Schema({
timestamp: {type: Date, required: true, default: Date.now}, timestamp: {type: Date, required: true, default: Date.now},
closed: { type: Boolean, required: true, default:false }, closed: { type: Boolean, required: true, default:false },
price: { type: Number, required: true}, price: { type: Number, required: true},
total: { type: Number},
totalZec: {type: Number},
lines: [{ lines: [{
qty: {type: Number, required: true, default: 1}, qty: {type: Number, required: true, default: 1},
name: { type: String, required: true}, name: { type: String, required: true},

View file

@ -15,7 +15,7 @@ import { CheckoutComponent} from '../checkout/checkout.component';
}) })
export class OrderComponent implements OnInit{ 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 price: number = 1;
public total: number = 0; public total: number = 0;
public orderUpdate: Observable<Order>; public orderUpdate: Observable<Order>;
@ -65,12 +65,14 @@ export class OrderComponent implements OnInit{
} }
checkout() { checkout() {
var zec = this.total/this.price;
this.order.totalZec = parseFloat(zec.toFixed(6));
const dialogConfig = new MatDialogConfig(); const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true; dialogConfig.disableClose = true;
dialogConfig.autoFocus = true; dialogConfig.autoFocus = true;
dialogConfig.data = { dialogConfig.data = {
totalZec: this.total/this.price, totalZec: this.order.totalZec,
addr: this.order.address, addr: this.order.address,
orderId: this.order._id orderId: this.order._id
}; };

View file

@ -7,5 +7,7 @@ export interface Order {
timestamp?: string, timestamp?: string,
closed: boolean, closed: boolean,
price?: number, price?: number,
total: number,
totalZec: number,
lines: LineItem[] lines: LineItem[]
} }

View file

@ -3,14 +3,14 @@ import { Subject, BehaviorSubject, Observable } from 'rxjs';
import { HttpClient, HttpParams } from '@angular/common/http'; import { HttpClient, HttpParams } from '@angular/common/http';
import { Order } from './order.model'; import { Order } from './order.model';
import { UserService } from '../user.service'; import { UserService } from '../user.service';
import { FullnodeService } from '../fullnode.service';
import { User } from '../user.model'; import { User } from '../user.model';
import { LineItem} from '../items/lineitem.model'; import { LineItem} from '../items/lineitem.model';
@Injectable({providedIn: 'root'}) @Injectable({providedIn: 'root'})
export class OrderService { export class OrderService {
private dataStore: { total:number, user: User, order: Order } = { private dataStore: { user: User, order: Order } = {
total: 0,
user:{ user:{
address: '', address: '',
session: '', session: '',
@ -21,6 +21,9 @@ export class OrderService {
session: '', session: '',
timestamp: '', timestamp: '',
closed: false, closed: false,
price: 0,
total: 0,
totalZec: 0,
lines: [ lines: [
{ {
qty: 1, qty: 1,
@ -32,12 +35,13 @@ export class OrderService {
}; };
private _orderUpdated: BehaviorSubject<Order> = new BehaviorSubject(this.dataStore.order); private _orderUpdated: BehaviorSubject<Order> = new BehaviorSubject(this.dataStore.order);
public readonly orderUpdate: Observable<Order> = this._orderUpdated.asObservable(); public readonly orderUpdate: Observable<Order> = this._orderUpdated.asObservable();
private _totalUpdated: BehaviorSubject<number> = new BehaviorSubject(this.dataStore.total); private _totalUpdated: BehaviorSubject<number> = new BehaviorSubject(this.dataStore.order.total);
public readonly totalUpdate: Observable<number> = this._totalUpdated.asObservable(); public readonly totalUpdate: Observable<number> = this._totalUpdated.asObservable();
public userUpdate: Observable<User>; public userUpdate: Observable<User>;
constructor( constructor(
private http: HttpClient, private http: HttpClient,
public fullnodeService: FullnodeService,
public userService: UserService public userService: UserService
) { ) {
this.userUpdate = userService.userUpdate; this.userUpdate = userService.userUpdate;
@ -57,11 +61,11 @@ export class OrderService {
if (OrderDataResponse.status == 200) { if (OrderDataResponse.status == 200) {
this.dataStore.order = OrderDataResponse.body!.order; this.dataStore.order = OrderDataResponse.body!.order;
this._orderUpdated.next(Object.assign({}, this.dataStore).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) { 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 { } else {
console.log('No order found'); console.log('No order found');
} }
@ -86,6 +90,9 @@ export class OrderService {
address: this.dataStore.user.address, address: this.dataStore.user.address,
session: this.dataStore.user.session, session: this.dataStore.user.session,
closed: false, closed: false,
totalZec: 0,
price: 0,
total: 0,
lines: [] lines: []
}; };
let obs = this.http.post<{message: string, order: Order}>('http://localhost:3000/api/order', {order: order}); let obs = this.http.post<{message: string, order: Order}>('http://localhost:3000/api/order', {order: order});
@ -110,6 +117,9 @@ export class OrderService {
session: '', session: '',
timestamp: '', timestamp: '',
closed: false, closed: false,
total: 0,
totalZec: 0,
price: 0,
lines: [ lines: [
{ {
qty: 1, qty: 1,
@ -125,7 +135,10 @@ export class OrderService {
} }
closeOrder(){ closeOrder(){
this.fullnodeService.priceUpdate.subscribe((price) => {
console.log('Price:', price);
this.dataStore.order.closed = true; 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}); let obs = this.http.post<{message: string, order: Order}>('http://localhost:3000/api/order', {order: this.dataStore.order});
obs.subscribe((orderData) => { obs.subscribe((orderData) => {
console.log('Closed order', orderData); console.log('Closed order', orderData);
@ -134,6 +147,9 @@ export class OrderService {
session: '', session: '',
timestamp: '', timestamp: '',
closed: false, closed: false,
price: 0,
total: 0,
totalZec: 0,
lines: [ lines: [
{ {
qty: 1, qty: 1,
@ -144,8 +160,8 @@ export class OrderService {
}; };
this._orderUpdated.next(Object.assign({}, this.dataStore).order); this._orderUpdated.next(Object.assign({}, this.dataStore).order);
}); });
return obs; return obs;
});
} }
} }

View file

@ -37,7 +37,6 @@ export class ViewerComponent implements OnInit {
} }
ngOnDestroy(){ ngOnDestroy(){
this.ownerUpdate.unsubscribe();
} }
} }