transactionData.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. const Transaction = require("../models/transaction");
  2. const Merchant = require("../models/merchant");
  3. const helper = require("./helper.js");
  4. const ObjectId = require("mongoose").Types.ObjectId;
  5. module.exports = {
  6. /*
  7. POST - retrieves a list of transactions based on the filter
  8. req.body = {
  9. startDate: starting date to filter on,
  10. endDate: ending date to filter on,
  11. recipes: list of recipes to filter on
  12. }
  13. NOTE: May be a good idea to search recipes with for looping rather than query
  14. Needs some testing and playing with if so
  15. */
  16. getTransactions: function(req, res){
  17. if(!req.session.user){
  18. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  19. return res.redirect("/");
  20. }
  21. let objectifiedRecipes = [];
  22. for(let i = 0; i < req.body.recipes.length; i++){
  23. objectifiedRecipes.push(new ObjectId(req.body.recipes[i]));
  24. }
  25. let startDate = new Date(req.body.startDate);
  26. let endDate = new Date(req.body.endDate);
  27. endDate = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate() + 1);
  28. Transaction.aggregate([
  29. {$match: {
  30. merchant: new ObjectId(req.session.user),
  31. date: {
  32. $gte: startDate,
  33. $lt: endDate
  34. },
  35. recipes: {
  36. $elemMatch: {
  37. recipe: {
  38. $in: objectifiedRecipes
  39. }
  40. }
  41. }
  42. }},
  43. {$sort: {date: -1}}
  44. ])
  45. .then((transactions)=>{
  46. return res.json(transactions);
  47. })
  48. .catch((err)=>{
  49. return res.json("ERROR: UNABLE TO RETRIEVE YOUR TRANSACTIONS");
  50. });
  51. },
  52. /*
  53. POST - create a new transaction
  54. req.body = {
  55. date: date of the transaction,
  56. recipes: [{
  57. recipe: id of the recipe to add,
  58. quantity: quantity of the recipe sold (in main unit),
  59. }]
  60. ingredientUpdates: an object that contains all of the ingredients that
  61. need to be updated as well as the amount to change.
  62. keys = id
  63. values = quantity to change in grams
  64. }
  65. */
  66. createTransaction: function(req, res){
  67. if(!req.session.user){
  68. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  69. return res.redirect("/");
  70. }
  71. let newTransaction = new Transaction({
  72. merchant: req.session.user,
  73. date: new Date(req.body.date),
  74. device: "none",
  75. recipes: req.body.recipes
  76. });
  77. helper.updateIngredientQuantities(req.body.ingredientUpdates, req.session.user);
  78. newTransaction.save()
  79. .then((response)=>{
  80. return res.json(response);
  81. })
  82. .catch((err)=>{
  83. return res.json("ERROR: UNABLE TO CREATE NEW TRANSACTION");
  84. });
  85. },
  86. /*
  87. DELETE - Remove a transaction from the database
  88. */
  89. remove: function(req, res){
  90. if(!req.session.user){
  91. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  92. return res.redirect("/");
  93. }
  94. let merchant = {};
  95. let transaction = {};
  96. Merchant.findOne({_id: req.session.user})
  97. .then((response)=>{
  98. merchant = response;
  99. return Transaction.findOne({_id: req.params.id}).populate("recipes.recipe");
  100. })
  101. .then((response)=>{
  102. transaction = response;
  103. return Transaction.deleteOne({_id: req.params.id});
  104. })
  105. .then((response)=>{
  106. res.json({});
  107. for(let i = 0; i < transaction.recipes.length; i++){
  108. const recipe = transaction.recipes[i].recipe;
  109. for(let j = 0; j < recipe.ingredients.length; j++){
  110. const ingredient = recipe.ingredients[i].ingredient;
  111. for(let k = 0; k < merchant.inventory.length; k++){
  112. if(ingredient.toString() == merchant.inventory[i].ingredient.toString()){
  113. merchant.inventory[i].quantity += recipe.ingredients[i].quantity * transaction.recipes[i].quantity;
  114. break;
  115. }
  116. }
  117. }
  118. }
  119. return merchant.save();
  120. })
  121. .catch((err)=>{
  122. return res.json("ERROR: UNABLE TO DELETE THE TRANSACTION");
  123. })
  124. // Transaction.deleteOne({_id: req.params.id})
  125. // .then((response)=>{
  126. // return res.json({});
  127. // })
  128. // .catch((err)=>{
  129. // return res.json("ERROR: UNABLE TO DELETE TRANSACTION");
  130. // });
  131. },
  132. getTransactionsByDate: function(req, res){
  133. if(!req.session.user){
  134. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  135. return res.redirect("/");
  136. }
  137. const from = new Date(req.body.from);
  138. let to = new Date(req.body.to);
  139. to.setDate(to.getDate() + 1);
  140. Transaction.aggregate([
  141. {$match: {
  142. merchant: ObjectId(req.session.user),
  143. date: {
  144. $gte: from,
  145. $lt: to
  146. }
  147. }},
  148. {$sort: {date: 1}}
  149. ])
  150. .then((transactions)=>{
  151. return res.json(transactions);
  152. })
  153. .catch((err)=>{
  154. return res.json("ERROR: UNABLE TO RETRIEVE YOUR DATA");
  155. });
  156. },
  157. /*
  158. GET - Creates 5000 transactions for logged in merchant for testing
  159. */
  160. populate: function(req, res){
  161. if(!req.session.user){
  162. res.session.error = "Must be logged in to do that";
  163. return res.redirect("/");
  164. }
  165. function randomDate() {
  166. let now = new Date();
  167. let start = new Date();
  168. start.setFullYear(now.getFullYear() - 1);
  169. return new Date(start.getTime() + Math.random() * (now.getTime() - start.getTime()));
  170. }
  171. Merchant.findOne({_id: req.session.user})
  172. .then((merchant)=>{
  173. let newTransactions = [];
  174. for(let i = 0; i < 5000; i++){
  175. let newTransaction = new Transaction({
  176. merchant: merchant._id,
  177. date: randomDate(),
  178. recipes: []
  179. });
  180. let numberOfRecipes = Math.floor((Math.random() * 5) + 1);
  181. for(let j = 0; j < numberOfRecipes; j++){
  182. let recipeNumber = Math.floor(Math.random() * merchant.recipes.length);
  183. let randQuantity = Math.floor((Math.random() * 3) + 1);
  184. newTransaction.recipes.push({
  185. recipe: merchant.recipes[recipeNumber],
  186. quantity: randQuantity
  187. });
  188. }
  189. newTransactions.push(newTransaction);
  190. }
  191. Transaction.create(newTransactions)
  192. .then((transactions)=>{
  193. return res.redirect("/dashboard");
  194. })
  195. .catch((err)=>{
  196. return;
  197. });
  198. })
  199. .catch((err)=>{
  200. return;
  201. });
  202. }
  203. }