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, {
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) {

View file

@ -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},

View file

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

View file

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

View file

@ -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<Order> = new BehaviorSubject(this.dataStore.order);
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 userUpdate: Observable<User>;
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;
}
}

View file

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