admin.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. let inventory = (req.files.ingredients === undefined) ? merchant.inventory : newIngredients;
  59. for(let i = 0; i < inventory.length; i++){
  60. if(req.files.ingredients === undefined){
  61. ingredients[inventory[i].ingredient.name.toLowerCase()] = inventory[i].ingredient;
  62. }else{
  63. ingredients[inventory[i].name.toLowerCase()] = inventory[i];
  64. }
  65. }
  66. return ingredients;
  67. }
  68. let ingredientIndices = indexIngredients();
  69. //Recipes
  70. let newRecipes = [];
  71. if(req.files.recipes !== undefined){
  72. let recipeData = fs.readFileSync(req.files.recipes.tempFilePath).toString();
  73. fs.unlink(req.files.recipes.tempFilePath, ()=>{});
  74. recipeData = recipeData.split("\n");
  75. merchant.recipes = [];
  76. for(let i = 0; i < recipeData.length; i++){
  77. if(recipeData[i] === "") continue;
  78. let data = recipeData[i].split(",");
  79. let recipe = new Recipe({
  80. merchant: merchant._id,
  81. name: data[0],
  82. price: parseInt(parseFloat(data[1]) * 100),
  83. category: data[2],
  84. ingredients: []
  85. });
  86. for(let j = 3; j < data.length; j+=3){
  87. if(data[j] === "") break;
  88. recipe.ingredients.push({
  89. ingredient: ingredientIndices[data[j].toLowerCase()],
  90. quantity: helper.convertQuantityToBaseUnit(parseFloat(data[j+1]), data[j+2]),
  91. unit: data[j+2]
  92. });
  93. }
  94. merchant.recipes.push(recipe);
  95. newRecipes.push(recipe);
  96. }
  97. }
  98. let indexRecipes = ()=>{
  99. let recipes = {};
  100. for(let i = 0; i < merchant.recipes.length; i++){
  101. recipes[merchant.recipes[i].name.toLowerCase()] = merchant.recipes[i];
  102. }
  103. return recipes;
  104. }
  105. let recipeIndices = indexRecipes();
  106. //Orders
  107. let newOrders = [];
  108. if(req.files.orders !== undefined){
  109. let orderData = fs.readFileSync(req.files.orders.tempFilePath).toString();
  110. fs.unlink(req.files.orders.tempFilePath, ()=>{});
  111. orderData = orderData.split("\n");
  112. for(let i = 0; i < orderData.length; i++){
  113. if(orderData[i] === "") continue;
  114. let data = orderData[i].split(",");
  115. let order = new Order({
  116. merchant: merchant._id,
  117. name: data[0],
  118. date: new Date(data[1]),
  119. taxes: parseFloat(data[2]),
  120. fees: parseFloat(data[3]),
  121. ingredients: []
  122. });
  123. for(let j = 4; j < data.length; j+=3){
  124. if(data[j] === "") break;
  125. order.ingredients.push({
  126. ingredient: ingredientIndices[data[j].toLowerCase()],
  127. quantity: parseFloat(data[j+1]),
  128. pricePerUnit: parseInt(parseFloat(data[j+2]) * 100)
  129. });
  130. }
  131. newOrders.push(order);
  132. }
  133. }
  134. //Transactions
  135. let newTransactions = [];
  136. if(req.files.transactions !== undefined){
  137. let transactionData = fs.readFileSync(req.files.transactions.tempFilePath).toString()
  138. fs.unlink(req.files.transactions.tempFilePath, ()=>{});
  139. transactionData = transactionData.split("\n");
  140. for(let i = 0; i < transactionData.length; i++){
  141. if(transactionData[i] === "") continue;
  142. let data = transactionData[i].split(",");
  143. let transaction = new Transaction({
  144. merchant: merchant._id,
  145. date: new Date(data[0]),
  146. recipes: []
  147. });
  148. for(let j = 1; j < data.length; j+=2){
  149. if(data[j] === "") break;
  150. transaction.recipes.push({
  151. recipe: recipeIndices[data[j].toLowerCase()],
  152. quantity: parseInt(data[j+1])
  153. });
  154. }
  155. newTransactions.push(transaction);
  156. }
  157. }
  158. return Promise.all([
  159. merchant.save(),
  160. Ingredient.create(newIngredients),
  161. Recipe.create(newRecipes),
  162. Order.create(newOrders),
  163. Transaction.create(newTransactions)
  164. ]);
  165. })
  166. .then((response)=>{
  167. return res.redirect("/dashboard");
  168. })
  169. .catch((err)=>{
  170. console.log(err);
  171. return res.json("ERROR: A whoopsie has been made");
  172. });
  173. }
  174. }