|
|
@@ -1,15 +1,30 @@
|
|
|
const Order = require("../models/order.js");
|
|
|
const Merchant = require("../models/merchant.js");
|
|
|
+const ObjectId = require("mongoose").Types.ObjectId;
|
|
|
|
|
|
module.exports = {
|
|
|
- getOrders: function(){
|
|
|
+ /*
|
|
|
+ GET - get the 25 most recent orders
|
|
|
+ return = [
|
|
|
+ _id: id of order,
|
|
|
+ orderId: user created id for order,
|
|
|
+ date: date order was created,
|
|
|
+ ingredients: [{
|
|
|
+ _id: unused id of this object,
|
|
|
+ ingredient: id of the ingredient,
|
|
|
+ price: price per unit of the ingredient,
|
|
|
+ quantity: quantity of ingredient in this order
|
|
|
+ }]
|
|
|
+ ]
|
|
|
+ */
|
|
|
+ getOrders: function(req, res){
|
|
|
if(!req.session.user){
|
|
|
req.session.error = "Must be logged in to do that";
|
|
|
return res.redirect("/");
|
|
|
}
|
|
|
|
|
|
Order.aggregate([
|
|
|
- {$match: {merchant: req.session.user}},
|
|
|
+ {$match: {merchant: ObjectId(req.session.user)}},
|
|
|
{$sort: {date: -1}},
|
|
|
{$limit: 25},
|
|
|
{$project: {
|
|
|
@@ -17,9 +32,9 @@ module.exports = {
|
|
|
date: 1,
|
|
|
ingredients: 1
|
|
|
}}
|
|
|
- ]).toArray()
|
|
|
+ ])
|
|
|
.then((orders)=>{
|
|
|
- return res.json({orders});
|
|
|
+ return res.json(orders);
|
|
|
})
|
|
|
.catch((err)=>{
|
|
|
return res.json("Error: unable to retrieve your orders");
|