orderData.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. const Order = require("../models/order.js");
  2. const ObjectId = require("mongoose").Types.ObjectId;
  3. module.exports = {
  4. /*
  5. GET: gets orders based on queries
  6. req.body = {
  7. from: Date (starting date/time)
  8. to: Date (ending date/time)
  9. ingredients: [id] (list of transactions to search for)
  10. empty list gets all
  11. }
  12. */
  13. getOrders: function(req, res){
  14. let from = new Date(req.body.from);
  15. let to = new Date(req.body.to);
  16. let match = {};
  17. let objectifiedIngredients = [];
  18. if(req.body.ingredients.length === 0){
  19. match = {$ne: false};
  20. }else{
  21. for(let i = 0; i < req.body.ingredients.length; i++){
  22. objectifiedIngredients.push(new ObjectId(req.body.ingredients[i]));
  23. }
  24. match = {
  25. $elemMatch: {
  26. ingredient: {
  27. $in: objectifiedIngredients
  28. }
  29. }
  30. }
  31. }
  32. Order.aggregate([
  33. {$match:{
  34. merchant: new ObjectId(res.locals.merchant._id),
  35. date: {
  36. $gte: from,
  37. $lt: to
  38. },
  39. ingredients: match
  40. }},
  41. {$sort: {date: -1}}
  42. ])
  43. .then((orders)=>{
  44. return res.json(orders);
  45. })
  46. .catch((err)=>{
  47. return res.json("ERROR: UNABLE TO RETRIEVE DATA");
  48. });
  49. },
  50. /*
  51. POST - Creates a new order from the site
  52. req.body = {
  53. name: user created order id
  54. date: creation date
  55. ingredients: [{
  56. ingredient: id of the ingredient
  57. quantity: amount of the ingredient purchased
  58. pricePerUnit: price per gram
  59. }]
  60. }
  61. */
  62. createOrder: function(req, res){
  63. let newOrder = new Order(req.body);
  64. newOrder.merchant = res.locals.merchant._id;
  65. newOrder.save()
  66. .then((response)=>{
  67. res.json(response);
  68. })
  69. .catch((err)=>{
  70. console.error(err);
  71. if(typeof(err) === "string"){
  72. return res.json(err);
  73. }
  74. if(err.name === "ValidationError"){
  75. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  76. }
  77. return res.json("ERROR: UNABLE TO SAVE ORDER");
  78. });
  79. for(let i = 0; i < req.body.ingredients.length; i++){
  80. for(let j = 0; j < res.locals.merchant.inventory.length; j++){
  81. if(req.body.ingredients[i].ingredient === res.locals.merchant.inventory[j].ingredient.toString()){
  82. res.locals.merchant.inventory[j].quantity += parseFloat(req.body.ingredients[i].quantity);
  83. }
  84. }
  85. }
  86. res.locals.merchant.save().catch((err)=>{});
  87. },
  88. /*
  89. DELETE - Remove an order from the database
  90. */
  91. removeOrder: function(req, res){
  92. Order.findOne({_id: req.params.id})
  93. .then((order)=>{
  94. for(let i = 0; i < order.ingredients.length; i++){
  95. for(let j = 0; j < res.locals.merchant.inventory.length; j++){
  96. if(order.ingredients[i].ingredient.toString() === res.locals.merchant.inventory[j].ingredient.toString()){
  97. res.locals.merchant.inventory[j].quantity -= order.ingredients[i].quantity;
  98. break;
  99. }
  100. }
  101. }
  102. return Promise.all([Order.deleteOne({_id: req.params.id}), res.locals.merchant.save()]);
  103. })
  104. .then((response)=>{
  105. res.json({});
  106. })
  107. .catch((err)=>{
  108. if(typeof(err) === "string"){
  109. return res.json(err);
  110. }
  111. if(err.name === "ValidationError"){
  112. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  113. }
  114. return res.json("ERROR: UNABLE TO REMOVE ORDER");
  115. });
  116. }
  117. }