transactionData.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. const Transaction = require("../models/transaction");
  2. const ObjectId = require("mongoose").Types.ObjectId;
  3. module.exports = {
  4. /*
  5. POST - retrieves a list of transactions based on the filter
  6. req.body = {
  7. from: starting date to filter on,
  8. to: ending date to filter on,
  9. recipes: list of recipes to filter on
  10. }
  11. */
  12. getTransactions: function(req, res){
  13. let from = new Date(req.body.from);
  14. let to = new Date(req.body.to);
  15. let objectifiedRecipes = [];
  16. let query = {};
  17. if(req.body.recipes.length === 0){
  18. query = {$ne: false};
  19. }else{
  20. for(let i = 0; i < req.body.recipes.length; i++){
  21. objectifiedRecipes.push(new ObjectId(req.body.recipes[i]));
  22. }
  23. query = {
  24. $elemMatch: {
  25. recipe: {
  26. $in: objectifiedRecipes
  27. }
  28. }
  29. }
  30. }
  31. Transaction.aggregate([
  32. {$match: {
  33. merchant: ObjectId(res.locals.merchant._id),
  34. date: {
  35. $gte: from,
  36. $lt: to
  37. },
  38. recipes: query
  39. }},
  40. {$sort: {date: -1}}
  41. ])
  42. .then((transactions)=>{
  43. return res.json(transactions);
  44. })
  45. .catch((err)=>{
  46. return res.json("ERROR: UNABLE TO RETRIEVE DATA");
  47. });
  48. },
  49. /*
  50. POST - create a new transaction
  51. req.body = {
  52. date: date of the transaction,
  53. recipes: [{
  54. recipe: id of the recipe to add,
  55. quantity: quantity of the recipe sold (in main unit),
  56. }]
  57. ingredientUpdates: an object that contains all of the ingredients that
  58. need to be updated as well as the amount to change.
  59. keys = id
  60. values = quantity to change in base unit
  61. }
  62. */
  63. createTransaction: function(req, res){
  64. let keys = Object.keys(req.body.ingredientUpdates);
  65. for(let i = 0; i < keys.length; i++){
  66. for(let j = 0; j < res.locals.merchant.inventory.length; j++){
  67. if(res.locals.merchant.inventory[j].ingredient._id.toString() === keys[i]){
  68. res.locals.merchant.inventory[j].quantity -= req.body.ingredientUpdates[keys[i]];
  69. break;
  70. }
  71. }
  72. }
  73. res.locals.merchant.save()
  74. .then((merchant)=>{
  75. if(req.body.date === null) throw "NEW TRANSACTIONS MUST CONTAIN A DATE";
  76. return new Transaction({
  77. merchant: res.locals.merchant._id,
  78. date: new Date(req.body.date),
  79. device: "none",
  80. recipes: req.body.recipes
  81. }).save();
  82. })
  83. .then((response)=>{
  84. return res.json(response);
  85. })
  86. .catch((err)=>{
  87. if(typeof(err) === "string"){
  88. return res.json(err);
  89. }
  90. if(err.name === "ValidationError"){
  91. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  92. }
  93. return res.json("ERROR: UNABLE TO CREATE NEW TRANSACTION");
  94. });
  95. },
  96. /*
  97. DELETE - Remove a transaction from the database
  98. */
  99. remove: function(req, res){
  100. Transaction.findOne({_id: req.params.id})
  101. .populate("recipes.recipe")
  102. .then((transaction)=>{
  103. for(let i = 0; i < transaction.recipes.length; i++){
  104. const recipe = transaction.recipes[i].recipe;
  105. for(let j = 0; j < recipe.ingredients.length; j++){
  106. const ingredient = recipe.ingredients[j].ingredient;
  107. for(let k = 0; k < res.locals.merchant.inventory.length; k++){
  108. if(ingredient.toString() === res.locals.merchant.inventory[k].ingredient.toString()){
  109. res.locals.merchant.inventory[k].quantity += recipe.ingredients[j].quantity * transaction.recipes[i].quantity;
  110. break;
  111. }
  112. }
  113. }
  114. }
  115. return Promise.all([Transaction.deleteOne({_id: req.params.id}), res.locals.merchant.save()]);
  116. })
  117. .then((response)=>{
  118. res.json({});
  119. })
  120. .catch((err)=>{
  121. if(typeof(err) === "string"){
  122. return res.json(err);
  123. }
  124. if(err.name === "ValidationError"){
  125. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  126. }
  127. return res.json("ERROR: UNABLE TO DELETE THE TRANSACTION");
  128. });
  129. },
  130. /*
  131. GET - Creates 5000 transactions for logged in merchant for testing
  132. */
  133. populate: function(req, res){
  134. function randomDate() {
  135. let now = new Date();
  136. let start = new Date();
  137. start.setFullYear(now.getFullYear() - 1);
  138. return new Date(start.getTime() + Math.random() * (now.getTime() - start.getTime()));
  139. }
  140. let newTransactions = [];
  141. for(let i = 0; i < 5000; i++){
  142. let newTransaction = new Transaction({
  143. merchant: res.locals.merchant._id,
  144. date: randomDate(),
  145. recipes: []
  146. });
  147. let numberOfRecipes = Math.floor((Math.random() * 5) + 1);
  148. for(let j = 0; j < numberOfRecipes; j++){
  149. let recipeNumber = Math.floor(Math.random() * res.locals.merchant.recipes.length);
  150. let randQuantity = Math.floor((Math.random() * 3) + 1);
  151. newTransaction.recipes.push({
  152. recipe: res.locals.merchant.recipes[recipeNumber],
  153. quantity: randQuantity
  154. });
  155. }
  156. newTransactions.push(newTransaction);
  157. }
  158. Transaction.create(newTransactions)
  159. .then((transactions)=>{
  160. return res.redirect("/dashboard");
  161. })
  162. .catch((err)=>{
  163. return;
  164. });
  165. }
  166. }