transactionData.js 6.3 KB

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