From 0f5a731d08a74c76aafcd468fe91c987de2ab8f3 Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Tue, 2 Nov 2021 10:35:22 -0500 Subject: [PATCH] Implement order closing --- backend/app.js | 28 ++++++++++++++++++++++------ src/app/order/order.component.ts | 1 + src/app/order/order.service.ts | 24 ++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/backend/app.js b/backend/app.js index c4e83bb..1d812ec 100644 --- a/backend/app.js +++ b/backend/app.js @@ -288,12 +288,28 @@ app.get('/api/order', (req, res, next) => { app.post('/api/order', (req, res, next) => { console.log('Post /api/order', req.body); - const order = new ordermodel(req.body.order); - order.save(); - res.status(200).json({ - message: 'Order added', - order: order - }); + if(req.body.order._id == null) { + const order = new ordermodel(req.body.order); + order.save(); + res.status(200).json({ + message: 'Order added', + 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) => { diff --git a/src/app/order/order.component.ts b/src/app/order/order.component.ts index 5638d99..abfb70f 100644 --- a/src/app/order/order.component.ts +++ b/src/app/order/order.component.ts @@ -79,6 +79,7 @@ export class OrderComponent implements OnInit{ dialogRef.afterClosed().subscribe((val) => { if (val) { console.log('Payment confirmed!'); + this.orderService.closeOrder(); } else { console.log('Returning to order'); } diff --git a/src/app/order/order.service.ts b/src/app/order/order.service.ts index dbf868e..9aa40cc 100644 --- a/src/app/order/order.service.ts +++ b/src/app/order/order.service.ts @@ -124,4 +124,28 @@ export class OrderService { 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; + } + }