admin.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const Merchant = require("../models/merchant.js");
  2. const Ingredient = require("../models/ingredient.js");
  3. const Recipe = require("../models/recipe.js");
  4. const helper = require("./helper.js");
  5. const fs = require("fs");
  6. module.exports = {
  7. /*
  8. POST: Adding data for admins
  9. req.body = {
  10. password: String
  11. id: String <merchant id>
  12. }
  13. req.files = {
  14. ingredients: txt
  15. recipes: txt
  16. }
  17. */
  18. addData: function(req, res){
  19. if(req.body.password !== process.env.ADMIN_PASS) return res.json("bad password");
  20. Merchant.findOne({_id: req.body.id})
  21. .populate("inventory.ingredient")
  22. .then((merchant)=>{
  23. //Ingredients
  24. let newIngredients = [];
  25. if(req.files.ingredients !== undefined){
  26. let ingredientData = fs.readFileSync(req.files.ingredients.tempFilePath).toString();
  27. fs.unlink(req.files.ingredients.tempFilePath, ()=>{});
  28. ingredientData = ingredientData.split("\n");
  29. merchant.inventory = [];
  30. for(let i = 0; i < ingredientData.length; i++){
  31. if(ingredientData[i] === "") continue;
  32. let data = ingredientData[i].split(",");
  33. let ingredient = new Ingredient({
  34. name: data[0],
  35. category: data[1],
  36. unitType: helper.getUnitType(data[3]),
  37. ingredients: []
  38. });
  39. let merchIngredient = {
  40. ingredient: ingredient._id,
  41. quantity: helper.convertQuantityToBaseUnit(data[2], data[3]),
  42. defaultUnit: data[3]
  43. };
  44. if(data[3].toLowerCase() === "bottle"){
  45. ingredient.unitType = data[5];
  46. ingredient.unitSize = helper.convertQuantityToBaseUnit(data[4], data[5])
  47. merchIngredient.defaultUnit = "bottle";
  48. }
  49. newIngredients.push(ingredient);
  50. merchant.inventory.push(merchIngredient);
  51. }
  52. }
  53. let indexIngredients = ()=>{
  54. let ingredients = {};
  55. for(let i = 0; i < merchant.inventory.length; i++){
  56. ingredients[merchant.inventory[i].ingredient.name.toLowerCase()] = merchant.inventory[i].ingredient;
  57. }
  58. return ingredients;
  59. }
  60. //Recipes
  61. let newRecipes = [];
  62. if(req.files.recipes !== undefined){
  63. let recipeData = fs.readFileSync(req.files.recipes.tempFilePath).toString();
  64. fs.unlink(req.files.recipes.tempFilePath, ()=>{});
  65. recipeData = recipeData.split("\n");
  66. let ingredientIndices = indexIngredients();
  67. for(let i = 0; i < recipeData.length; i++){
  68. if(recipeData[i] === "") continue;
  69. let data = recipeData[i].split(",");
  70. let recipe = new Recipe({
  71. merchant: merchant._id,
  72. name: data[0],
  73. price: parseInt(parseFloat(data[1]) * 100),
  74. category: data[2],
  75. ingredients: []
  76. });
  77. for(let j = 3; j < data.length; j+=3){
  78. if(data[j] === "") break;
  79. recipe.ingredients.push({
  80. ingredient: ingredientIndices[data[j].toLowerCase()],
  81. quantity: helper.convertQuantityToBaseUnit(parseFloat(data[j+1]), data[j+2])
  82. });
  83. }
  84. merchant.recipes.push(recipe);
  85. newRecipes.push(recipe);
  86. }
  87. }
  88. return Promise.all([
  89. merchant.save(),
  90. Ingredient.create(newIngredients),
  91. Recipe.create(newRecipes)
  92. ]);
  93. })
  94. .then((response)=>{
  95. return res.redirect("/dashboard");
  96. })
  97. .catch((err)=>{
  98. // console.log(err);
  99. return res.json("ERROR: A whoopsie has been made");
  100. });
  101. }
  102. }