Implement order closing

This commit is contained in:
Rene Vergara 2021-11-02 10:35:22 -05:00
parent 33326febaf
commit 0f5a731d08
3 changed files with 47 additions and 6 deletions

View file

@ -288,12 +288,28 @@ app.get('/api/order', (req, res, next) => {
app.post('/api/order', (req, res, next) => { app.post('/api/order', (req, res, next) => {
console.log('Post /api/order', req.body); console.log('Post /api/order', req.body);
if(req.body.order._id == null) {
const order = new ordermodel(req.body.order); const order = new ordermodel(req.body.order);
order.save(); order.save();
res.status(200).json({ res.status(200).json({
message: 'Order added', message: 'Order added',
order: order order: order
}); });
} else {
ordermodel.findByIdAndUpdate(req.body.order._id, {
address: req.body.order.address,
session: req.body.order.session,
closed: req.body.order.closed
}, function(err, docs) {
if(err) {
console.log(err);
} else {
res.status(200).json({
message: 'Order updated'
});
}
});
}
}); });
app.post('/api/lineitem', (req, res, next) => { app.post('/api/lineitem', (req, res, next) => {

View file

@ -79,6 +79,7 @@ export class OrderComponent implements OnInit{
dialogRef.afterClosed().subscribe((val) => { dialogRef.afterClosed().subscribe((val) => {
if (val) { if (val) {
console.log('Payment confirmed!'); console.log('Payment confirmed!');
this.orderService.closeOrder();
} else { } else {
console.log('Returning to order'); console.log('Returning to order');
} }

View file

@ -124,4 +124,28 @@ export class OrderService {
return obs; return obs;
} }
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);
});
return obs;
}
} }