orderData.js 4.1 KB

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