| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- const Order = require("../models/order.js");
- const ObjectId = require("mongoose").Types.ObjectId;
- module.exports = {
-
- /*
- GET: gets orders based on queries
- req.body = {
- from: Date (starting date/time)
- to: Date (ending date/time)
- ingredients: [id] (list of transactions to search for)
- empty list gets all
- }
- */
- getOrders: function(req, res){
- let from = new Date(req.body.from);
- let to = new Date(req.body.to);
- let match = {};
- let objectifiedIngredients = [];
- if(req.body.ingredients.length === 0){
- match = {$ne: false};
- }else{
- for(let i = 0; i < req.body.ingredients.length; i++){
- objectifiedIngredients.push(new ObjectId(req.body.ingredients[i]));
- }
- match = {
- $elemMatch: {
- ingredient: {
- $in: objectifiedIngredients
- }
- }
- }
- }
- Order.aggregate([
- {$match:{
- merchant: new ObjectId(res.locals.merchant._id),
- date: {
- $gte: from,
- $lt: to
- },
- ingredients: match
- }},
- {$sort: {date: -1}}
- ])
- .then((orders)=>{
- return res.json(orders);
- })
- .catch((err)=>{
- return res.json("ERROR: UNABLE TO RETRIEVE DATA");
- });
- },
- /*
- POST - Creates a new order from the site
- req.body = {
- name: user created order id
- date: creation date
- ingredients: [{
- ingredient: id of the ingredient
- quantity: amount of the ingredient purchased
- pricePerUnit: price per gram
- }]
- }
- */
- createOrder: function(req, res){
- let newOrder = new Order(req.body);
- newOrder.merchant = res.locals.merchant._id;
- newOrder.save()
- .then((response)=>{
- res.json(response);
- })
- .catch((err)=>{
- console.error(err);
- if(typeof(err) === "string"){
- return res.json(err);
- }
- if(err.name === "ValidationError"){
- return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
- }
- return res.json("ERROR: UNABLE TO SAVE ORDER");
- });
-
- for(let i = 0; i < req.body.ingredients.length; i++){
- for(let j = 0; j < res.locals.merchant.inventory.length; j++){
- if(req.body.ingredients[i].ingredient === res.locals.merchant.inventory[j].ingredient.toString()){
- res.locals.merchant.inventory[j].quantity += parseFloat(req.body.ingredients[i].quantity);
- }
- }
- }
- res.locals.merchant.save().catch((err)=>{});
- },
- /*
- DELETE - Remove an order from the database
- */
- removeOrder: function(req, res){
- Order.findOne({_id: req.params.id})
- .then((order)=>{
- for(let i = 0; i < order.ingredients.length; i++){
- for(let j = 0; j < res.locals.merchant.inventory.length; j++){
- if(order.ingredients[i].ingredient.toString() === res.locals.merchant.inventory[j].ingredient.toString()){
- res.locals.merchant.inventory[j].quantity -= order.ingredients[i].quantity;
- break;
- }
- }
- }
- return Promise.all([Order.deleteOne({_id: req.params.id}), res.locals.merchant.save()]);
- })
- .then((response)=>{
- res.json({});
- })
- .catch((err)=>{
- if(typeof(err) === "string"){
- return res.json(err);
- }
- if(err.name === "ValidationError"){
- return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
- }
- return res.json("ERROR: UNABLE TO REMOVE ORDER");
- });
- }
- }
|