transactionData.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 grams
  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){
  76. throw "NEW TRANSACTIONS MUST CONTAIN A DATE";
  77. }
  78. return new Transaction({
  79. merchant: res.locals.merchant._id,
  80. date: new Date(req.body.date),
  81. device: "none",
  82. recipes: req.body.recipes
  83. }).save();
  84. })
  85. .then((response)=>{
  86. return res.json(response);
  87. })
  88. .catch((err)=>{
  89. if(typeof(err) === "string"){
  90. return res.json(err);
  91. }
  92. if(err.name === "ValidationError"){
  93. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  94. }
  95. return res.json("ERROR: UNABLE TO CREATE NEW TRANSACTION");
  96. });
  97. },
  98. /*
  99. DELETE - Remove a transaction from the database
  100. */
  101. remove: function(req, res){
  102. Transaction.findOne({_id: req.params.id})
  103. .populate("recipes.recipe")
  104. .then((transaction)=>{
  105. for(let i = 0; i < transaction.recipes.length; i++){
  106. const recipe = transaction.recipes[i].recipe;
  107. for(let j = 0; j < recipe.ingredients.length; j++){
  108. const ingredient = recipe.ingredients[j].ingredient;
  109. for(let k = 0; k < res.locals.merchant.inventory.length; k++){
  110. if(ingredient.toString() === res.locals.merchant.inventory[k].ingredient.toString()){
  111. res.locals.merchant.inventory[k].quantity += recipe.ingredients[j].quantity * transaction.recipes[i].quantity;
  112. break;
  113. }
  114. }
  115. }
  116. }
  117. return Promise.all([Transaction.deleteOne({_id: req.params.id}), res.locals.merchant.save()]);
  118. })
  119. .then((response)=>{
  120. res.json({});
  121. })
  122. .catch((err)=>{
  123. if(typeof(err) === "string"){
  124. return res.json(err);
  125. }
  126. if(err.name === "ValidationError"){
  127. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  128. }
  129. return res.json("ERROR: UNABLE TO DELETE THE TRANSACTION");
  130. });
  131. },
  132. /*
  133. GET - Creates 5000 transactions for logged in merchant for testing
  134. */
  135. populate: function(req, res){
  136. function randomDate() {
  137. let now = new Date();
  138. let start = new Date();
  139. start.setFullYear(now.getFullYear() - 1);
  140. return new Date(start.getTime() + Math.random() * (now.getTime() - start.getTime()));
  141. }
  142. let newTransactions = [];
  143. for(let i = 0; i < 5000; i++){
  144. let newTransaction = new Transaction({
  145. merchant: res.locals.merchant._id,
  146. date: randomDate(),
  147. recipes: []
  148. });
  149. let numberOfRecipes = Math.floor((Math.random() * 5) + 1);
  150. for(let j = 0; j < numberOfRecipes; j++){
  151. let recipeNumber = Math.floor(Math.random() * res.locals.merchant.recipes.length);
  152. let randQuantity = Math.floor((Math.random() * 3) + 1);
  153. newTransaction.recipes.push({
  154. recipe: res.locals.merchant.recipes[recipeNumber],
  155. quantity: randQuantity
  156. });
  157. }
  158. newTransactions.push(newTransaction);
  159. }
  160. Transaction.create(newTransactions)
  161. .then((transactions)=>{
  162. return res.redirect("/dashboard");
  163. })
  164. .catch((err)=>{
  165. return;
  166. });
  167. }
  168. }