orderData.js 4.2 KB

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