admin.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. let convertMass = {toMass: 1};
  34. let convertVolume = {toVolume: 1};
  35. let convertLength = {toLength: 1};
  36. merchant.inventory = [];
  37. for(let i = 0; i < ingredientData.length; i++){
  38. if(ingredientData[i] === "") continue;
  39. let data = ingredientData[i].split(",");
  40. let unitType = helper.getUnitType(data[3]);
  41. let ingredient = new Ingredient({
  42. name: data[0],
  43. category: data[1],
  44. unitType: unitType,
  45. ingredients: [],
  46. });
  47. switch(unitType){
  48. case "mass": ingredient.convert = convertMass; break;
  49. case "volume": ingredient.convert = convertVolume; break;
  50. case "length": ingredient.convert = convertLength; break;
  51. default: ingredient.convert = convertMass; break;
  52. }
  53. let merchIngredient = {
  54. ingredient: ingredient._id,
  55. quantity: helper.convertQuantityToBaseUnit(data[2], data[3]),
  56. defaultUnit: data[3]
  57. };
  58. if(data[3].toLowerCase() === "bottle"){
  59. ingredient.unitType = data[5];
  60. ingredient.unitSize = helper.convertQuantityToBaseUnit(data[4], data[5])
  61. merchIngredient.defaultUnit = "bottle";
  62. }
  63. newIngredients.push(ingredient);
  64. merchant.inventory.push(merchIngredient);
  65. }
  66. }
  67. let indexIngredients = ()=>{
  68. let ingredients = {};
  69. let inventory = (req.files.ingredients === undefined) ? merchant.inventory : newIngredients;
  70. for(let i = 0; i < inventory.length; i++){
  71. if(req.files.ingredients === undefined){
  72. ingredients[inventory[i].ingredient.name.toLowerCase()] = inventory[i].ingredient;
  73. }else{
  74. ingredients[inventory[i].name.toLowerCase()] = inventory[i];
  75. }
  76. }
  77. return ingredients;
  78. }
  79. let ingredientIndices = indexIngredients();
  80. //Recipes
  81. let newRecipes = [];
  82. if(req.files.recipes !== undefined){
  83. let recipeData = fs.readFileSync(req.files.recipes.tempFilePath).toString();
  84. fs.unlink(req.files.recipes.tempFilePath, ()=>{});
  85. recipeData = recipeData.split("\n");
  86. merchant.recipes = [];
  87. for(let i = 0; i < recipeData.length; i++){
  88. if(recipeData[i] === "") continue;
  89. let data = recipeData[i].split(",");
  90. let recipe = new Recipe({
  91. merchant: merchant._id,
  92. name: data[0],
  93. price: parseInt(parseFloat(data[1]) * 100),
  94. category: data[2],
  95. ingredients: []
  96. });
  97. for(let j = 3; j < data.length; j+=3){
  98. if(data[j] === "") break;
  99. recipe.ingredients.push({
  100. ingredient: ingredientIndices[data[j].toLowerCase()],
  101. quantity: helper.convertQuantityToBaseUnit(parseFloat(data[j+1]), data[j+2]),
  102. unit: data[j+2]
  103. });
  104. }
  105. merchant.recipes.push(recipe);
  106. newRecipes.push(recipe);
  107. }
  108. }
  109. let indexRecipes = ()=>{
  110. let recipes = {};
  111. for(let i = 0; i < merchant.recipes.length; i++){
  112. recipes[merchant.recipes[i].name.toLowerCase()] = merchant.recipes[i];
  113. }
  114. return recipes;
  115. }
  116. let recipeIndices = indexRecipes();
  117. //Orders
  118. let newOrders = [];
  119. if(req.files.orders !== undefined){
  120. Order.deleteMany({merchant: req.body.id}).catch((err)=>{});
  121. let orderData = fs.readFileSync(req.files.orders.tempFilePath).toString();
  122. fs.unlink(req.files.orders.tempFilePath, ()=>{});
  123. orderData = orderData.split("\n");
  124. for(let i = 0; i < orderData.length; i++){
  125. if(orderData[i] === "") continue;
  126. let data = orderData[i].split(",");
  127. let order = new Order({
  128. merchant: merchant._id,
  129. name: data[0],
  130. date: new Date(data[1]),
  131. taxes: parseInt(parseFloat(data[2]) * 100),
  132. fees: parseInt(parseFloat(data[3]) * 100),
  133. ingredients: []
  134. });
  135. for(let j = 4; j < data.length; j+=3){
  136. if(data[j] === "") break;
  137. order.ingredients.push({
  138. ingredient: ingredientIndices[data[j].toLowerCase()],
  139. quantity: parseFloat(data[j+1]),
  140. pricePerUnit: parseInt(parseFloat(data[j+2]) * 100)
  141. });
  142. }
  143. newOrders.push(order);
  144. }
  145. }
  146. //Transactions
  147. let newTransactions = [];
  148. if(req.files.transactions !== undefined){
  149. Transaction.deleteMany({merchant: req.body.id}).catch((err)=>{});
  150. let transactionData = fs.readFileSync(req.files.transactions.tempFilePath).toString()
  151. fs.unlink(req.files.transactions.tempFilePath, ()=>{});
  152. transactionData = transactionData.split("\n");
  153. for(let i = 0; i < transactionData.length; i++){
  154. if(transactionData[i] === "") continue;
  155. let data = transactionData[i].split(",");
  156. let transaction = new Transaction({
  157. merchant: merchant._id,
  158. date: new Date(data[0]),
  159. recipes: []
  160. });
  161. for(let j = 1; j < data.length; j+=2){
  162. if(data[j] === "") break;
  163. transaction.recipes.push({
  164. recipe: recipeIndices[data[j].toLowerCase()],
  165. quantity: parseInt(data[j+1])
  166. });
  167. }
  168. newTransactions.push(transaction);
  169. }
  170. }
  171. return Promise.all([
  172. merchant.save(),
  173. Ingredient.create(newIngredients),
  174. Recipe.create(newRecipes),
  175. Order.create(newOrders),
  176. Transaction.create(newTransactions)
  177. ]);
  178. })
  179. .then((response)=>{
  180. return res.redirect("/dashboard");
  181. })
  182. .catch((err)=>{
  183. return res.json("ERROR: A whoopsie has been made");
  184. });
  185. }
  186. }