orderData.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. console.log(req.body);
  64. let newOrder = new Order(req.body);
  65. newOrder.merchant = res.locals.merchant._id;
  66. newOrder.save()
  67. .then((response)=>{
  68. res.json(response);
  69. })
  70. .catch((err)=>{
  71. console.error(err);
  72. if(typeof(err) === "string"){
  73. return res.json(err);
  74. }
  75. if(err.name === "ValidationError"){
  76. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  77. }
  78. return res.json("ERROR: UNABLE TO SAVE ORDER");
  79. });
  80. for(let i = 0; i < req.body.ingredients.length; i++){
  81. for(let j = 0; j < res.locals.merchant.inventory.length; j++){
  82. if(req.body.ingredients[i].ingredient === res.locals.merchant.inventory[j].ingredient.toString()){
  83. res.locals.merchant.inventory[j].quantity += parseFloat(req.body.ingredients[i].quantity);
  84. }
  85. }
  86. }
  87. res.locals.merchant.save().catch((err)=>{});
  88. },
  89. /*
  90. DELETE - Remove an order from the database
  91. */
  92. removeOrder: function(req, res){
  93. Order.findOne({_id: req.params.id})
  94. .then((order)=>{
  95. for(let i = 0; i < order.ingredients.length; i++){
  96. for(let j = 0; j < res.locals.merchant.inventory.length; j++){
  97. if(order.ingredients[i].ingredient.toString() === res.locals.merchant.inventory[j].ingredient.toString()){
  98. res.locals.merchant.inventory[j].quantity -= order.ingredients[i].quantity;
  99. break;
  100. }
  101. }
  102. }
  103. return Promise.all([Order.deleteOne({_id: req.params.id}), res.locals.merchant.save()]);
  104. })
  105. .then((response)=>{
  106. res.json({});
  107. })
  108. .catch((err)=>{
  109. if(typeof(err) === "string"){
  110. return res.json(err);
  111. }
  112. if(err.name === "ValidationError"){
  113. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  114. }
  115. return res.json("ERROR: UNABLE TO REMOVE ORDER");
  116. });
  117. }
  118. }