transactionData.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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[j].ingredient;
  111. for(let k = 0; k < merchant.inventory.length; k++){
  112. if(ingredient.toString() === merchant.inventory[k].ingredient.toString()){
  113. merchant.inventory[k].quantity += recipe.ingredients[j].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. },
  125. getTransactionsByDate: function(req, res){
  126. if(!req.session.user){
  127. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  128. return res.redirect("/");
  129. }
  130. const from = new Date(req.body.from);
  131. let to = new Date(req.body.to);
  132. to.setDate(to.getDate() + 1);
  133. Transaction.aggregate([
  134. {$match: {
  135. merchant: ObjectId(req.session.user),
  136. date: {
  137. $gte: from,
  138. $lt: to
  139. }
  140. }},
  141. {$sort: {date: 1}}
  142. ])
  143. .then((transactions)=>{
  144. return res.json(transactions);
  145. })
  146. .catch((err)=>{
  147. return res.json("ERROR: UNABLE TO RETRIEVE YOUR DATA");
  148. });
  149. },
  150. /*
  151. GET - Creates 5000 transactions for logged in merchant for testing
  152. */
  153. populate: function(req, res){
  154. if(!req.session.user){
  155. res.session.error = "Must be logged in to do that";
  156. return res.redirect("/");
  157. }
  158. function randomDate() {
  159. let now = new Date();
  160. let start = new Date();
  161. start.setFullYear(now.getFullYear() - 1);
  162. return new Date(start.getTime() + Math.random() * (now.getTime() - start.getTime()));
  163. }
  164. Merchant.findOne({_id: req.session.user})
  165. .then((merchant)=>{
  166. let newTransactions = [];
  167. for(let i = 0; i < 5000; i++){
  168. let newTransaction = new Transaction({
  169. merchant: merchant._id,
  170. date: randomDate(),
  171. recipes: []
  172. });
  173. let numberOfRecipes = Math.floor((Math.random() * 5) + 1);
  174. for(let j = 0; j < numberOfRecipes; j++){
  175. let recipeNumber = Math.floor(Math.random() * merchant.recipes.length);
  176. let randQuantity = Math.floor((Math.random() * 3) + 1);
  177. newTransaction.recipes.push({
  178. recipe: merchant.recipes[recipeNumber],
  179. quantity: randQuantity
  180. });
  181. }
  182. newTransactions.push(newTransaction);
  183. }
  184. Transaction.create(newTransactions)
  185. .then((transactions)=>{
  186. return res.redirect("/dashboard");
  187. })
  188. .catch((err)=>{
  189. return;
  190. });
  191. })
  192. .catch((err)=>{
  193. return;
  194. });
  195. }
  196. }