admin.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. const Merchant = require("../models/merchant.js");
  2. const Ingredient = require("../models/ingredient.js");
  3. const Recipe = require("../models/recipe.js");
  4. const Order = require("../models/order.js");
  5. const Transaction = require("../models/transaction.js");
  6. const helper = require("./helper.js");
  7. const fs = require("fs");
  8. module.exports = {
  9. /*
  10. POST: Adding data for admins
  11. req.body = {
  12. password: String
  13. id: String <merchant id>
  14. }
  15. req.files = {
  16. ingredients: txt
  17. recipes: txt
  18. }
  19. */
  20. addData: function(req, res){
  21. if(req.body.password !== process.env.ADMIN_PASS) return res.json("bad password");
  22. Merchant.findOne({_id: req.body.id})
  23. .populate("inventory.ingredient")
  24. .populate("recipes")
  25. .then((merchant)=>{
  26. //Ingredients
  27. let newIngredients = [];
  28. if(req.files.ingredients !== undefined){
  29. let ingredientData = fs.readFileSync(req.files.ingredients.tempFilePath).toString();
  30. fs.unlink(req.files.ingredients.tempFilePath, ()=>{});
  31. ingredientData = ingredientData.split("\n");
  32. merchant.inventory = [];
  33. for(let i = 0; i < ingredientData.length; i++){
  34. if(ingredientData[i] === "") continue;
  35. let data = ingredientData[i].split(",");
  36. let ingredient = new Ingredient({
  37. name: data[0],
  38. category: data[1],
  39. unitType: helper.getUnitType(data[3]),
  40. ingredients: []
  41. });
  42. let merchIngredient = {
  43. ingredient: ingredient._id,
  44. quantity: helper.convertQuantityToBaseUnit(data[2], data[3]),
  45. defaultUnit: data[3]
  46. };
  47. if(data[3].toLowerCase() === "bottle"){
  48. ingredient.unitType = data[5];
  49. ingredient.unitSize = helper.convertQuantityToBaseUnit(data[4], data[5])
  50. merchIngredient.defaultUnit = "bottle";
  51. }
  52. newIngredients.push(ingredient);
  53. merchant.inventory.push(merchIngredient);
  54. }
  55. }
  56. let indexIngredients = ()=>{
  57. let ingredients = {};
  58. for(let i = 0; i < merchant.inventory.length; i++){
  59. ingredients[merchant.inventory[i].ingredient.name.toLowerCase()] = merchant.inventory[i].ingredient;
  60. }
  61. return ingredients;
  62. }
  63. let ingredientIndices = indexIngredients();
  64. //Recipes
  65. let newRecipes = [];
  66. if(req.files.recipes !== undefined){
  67. let recipeData = fs.readFileSync(req.files.recipes.tempFilePath).toString();
  68. fs.unlink(req.files.recipes.tempFilePath, ()=>{});
  69. recipeData = recipeData.split("\n");
  70. merchant.recipes = [];
  71. for(let i = 0; i < recipeData.length; i++){
  72. if(recipeData[i] === "") continue;
  73. let data = recipeData[i].split(",");
  74. let recipe = new Recipe({
  75. merchant: merchant._id,
  76. name: data[0],
  77. price: parseInt(parseFloat(data[1]) * 100),
  78. category: data[2],
  79. ingredients: []
  80. });
  81. for(let j = 3; j < data.length; j+=3){
  82. if(data[j] === "") break;
  83. recipe.ingredients.push({
  84. ingredient: ingredientIndices[data[j].toLowerCase()],
  85. quantity: helper.convertQuantityToBaseUnit(parseFloat(data[j+1]), data[j+2])
  86. });
  87. }
  88. merchant.recipes.push(recipe);
  89. newRecipes.push(recipe);
  90. }
  91. }
  92. let indexRecipes = ()=>{
  93. let recipes = {};
  94. for(let i = 0; i < merchant.recipes.length; i++){
  95. recipes[merchant.recipes[i].name.toLowerCase()] = merchant.recipes[i];
  96. }
  97. return recipes;
  98. }
  99. let recipeIndices = indexRecipes();
  100. //Orders
  101. let newOrders = [];
  102. if(req.files.orders !== undefined){
  103. let orderData = fs.readFileSync(req.files.orders.tempFilePath).toString();
  104. fs.unlink(req.files.orders.tempFilePath, ()=>{});
  105. orderData = orderData.split("\n");
  106. for(let i = 0; i < orderData.length; i++){
  107. if(orderData[i] === "") continue;
  108. let data = orderData[i].split(",");
  109. let order = new Order({
  110. merchant: merchant._id,
  111. name: data[0],
  112. date: new Date(data[1]),
  113. taxes: parseFloat(data[2]),
  114. fees: parseFloat(data[3]),
  115. ingredients: []
  116. });
  117. for(let j = 4; j < data.length; j+=3){
  118. if(data[j] === "") break;
  119. order.ingredients.push({
  120. ingredient: ingredientIndices[data[j].toLowerCase()],
  121. quantity: parseFloat(data[j+1]),
  122. pricePerUnit: parseInt(parseFloat(data[j+2]) * 100)
  123. });
  124. }
  125. newOrders.push(order);
  126. }
  127. }
  128. //Transactions
  129. let newTransactions = [];
  130. if(req.files.transactions !== undefined){
  131. let transactionData = fs.readFileSync(req.files.transactions.tempFilePath).toString()
  132. fs.unlink(req.files.transactions.tempFilePath, ()=>{});
  133. transactionData = transactionData.split("\n");
  134. for(let i = 0; i < transactionData.length; i++){
  135. if(transactionData[i] === "") continue;
  136. let data = transactionData[i].split(",");
  137. let transaction = new Transaction({
  138. merchant: merchant._id,
  139. date: new Date(data[0]),
  140. recipes: []
  141. });
  142. for(let j = 1; j < data.length; j+=2){
  143. if(data[j] === "") break;
  144. transaction.recipes.push({
  145. recipe: recipeIndices[data[j].toLowerCase()],
  146. quantity: parseInt(data[j+1])
  147. });
  148. }
  149. newTransactions.push(transaction);
  150. }
  151. }
  152. return Promise.all([
  153. merchant.save(),
  154. Ingredient.create(newIngredients),
  155. Recipe.create(newRecipes),
  156. Order.create(newOrders),
  157. Transaction.create(newTransactions)
  158. ]);
  159. })
  160. .then((response)=>{
  161. return res.redirect("/dashboard");
  162. })
  163. .catch((err)=>{
  164. console.log(err);
  165. return res.json("ERROR: A whoopsie has been made");
  166. });
  167. }
  168. }