Skip to content

Commit

Permalink
Merge f09129e into 3f2223e
Browse files Browse the repository at this point in the history
  • Loading branch information
akhilome committed Oct 4, 2018
2 parents 3f2223e + f09129e commit f1683f2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions server/controllers/orderController.js
Expand Up @@ -117,6 +117,15 @@ class OrderController {
const dbQuery = 'UPDATE orders SET status=$1 WHERE id=$2';
await pool.query(dbQuery, [req.status, Number(id)]);

const orderExists = (await pool.query('SELECT * FROM orders WHERE id=$1', [Number(id)])).rowCount;

if (!orderExists) {
return res.status(404).json({
status: 'error',
message: 'no order with that id exists',
});
}

const updatedOrders = (await pool.query('SELECT orders.id, menu.food_name, users.name, orders.date, orders.status FROM orders JOIN menu ON orders.item = menu.id JOIN users ON orders.author = users.id')).rows;

const formattedOrders = orderFormatter(updatedOrders);
Expand Down
15 changes: 15 additions & 0 deletions tests/routes/orders.spec.js
Expand Up @@ -275,4 +275,19 @@ describe('PUT /orders/:id', () => {
done();
});
});

it('should not update the status of a non-existent order', (done) => {
chai.request(app)
.put('/api/v1/orders/2')
.set('x-auth', generateValidToken(users.admin))
.send({ status: 'complete' })
.end((err, res) => {
if (err) done(err);

res.status.should.be.eql(404);
res.body.should.be.an('object').which.has.all.keys(['status', 'message']);
res.body.should.not.have.keys('order');
done();
});
});
});

0 comments on commit f1683f2

Please sign in to comment.