admin.js 8.4 KB

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