admin.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. orders: txt
  19. transactions: txt
  20. }
  21. */
  22. addData: function(req, res){
  23. if(req.body.password !== process.env.ADMIN_PASS) return res.json("bad password");
  24. Merchant.findOne({_id: req.body.id})
  25. .populate("inventory.ingredient")
  26. .populate("recipes")
  27. .then((merchant)=>{
  28. //Ingredients
  29. let newIngredients = [];
  30. let ingredientData = {};
  31. if(req.files.ingredients !== undefined){
  32. ingredientData = fs.readFileSync(req.files.ingredients.tempFilePath).toString();
  33. fs.unlink(req.files.ingredients.tempFilePath, ()=>{});
  34. ingredientData = ingredientData.split("\n");
  35. merchant.inventory = [];
  36. for(let i = 0; i < ingredientData.length; i++){
  37. if(ingredientData[i] === "") continue;
  38. let data = ingredientData[i].split(",");
  39. let ingredient = new Ingredient({
  40. name: data[0],
  41. category: data[1],
  42. unit: data[3],
  43. ingredients: [],
  44. convert: {
  45. toMass: data[4] || 0,
  46. toVolume: data[5] || 0,
  47. toLength: data[6] || 0
  48. }
  49. });
  50. let toMass = ingredient.convert.toMass;
  51. let toVolume = ingredient.convert.toVolume;
  52. let toLength = ingredient.convert.toLength;
  53. switch(data[3]){
  54. case "g": toMass = 1; break;
  55. case "kg": toMass = 1; break;
  56. case "oz": toMass = 1; break;
  57. case "lb": toMass = 1; break;
  58. case "ml": toVolume = 1; break;
  59. case "l": toVolume = 1; break;
  60. case "tsp": toVolume = 1; break;
  61. case "tbsp": toVolume = 1; break;
  62. case "ozfl": toVolume = 1; break;
  63. case "cup": toVolume = 1; break;
  64. case "pt": toVolume = 1; break;
  65. case "qt": toVolume = 1; break;
  66. case "gal": toVolume = 1; break;
  67. case "mm": toLength = 1; break;
  68. case "cm": toLength = 1; break;
  69. case "m": toLength = 1; break;
  70. case "in": toLength = 1; break;
  71. case "ft": toLength = 1; break;
  72. }
  73. let merchIngredient = {
  74. ingredient: ingredient._id,
  75. quantity: helper.convertQuantityToBaseUnit(data[2], data[3]),
  76. };
  77. newIngredients.push(ingredient);
  78. merchant.inventory.push(merchIngredient);
  79. }
  80. }
  81. let indexIngredients = ()=>{
  82. let ingredients = {};
  83. let inventory = (req.files.ingredients === undefined) ? merchant.inventory : newIngredients;
  84. for(let i = 0; i < inventory.length; i++){
  85. if(req.files.ingredients === undefined){
  86. ingredients[inventory[i].ingredient.name.toLowerCase()] = inventory[i].ingredient;
  87. }else{
  88. ingredients[inventory[i].name.toLowerCase()] = inventory[i];
  89. }
  90. }
  91. return ingredients;
  92. }
  93. let ingredientIndices = indexIngredients();
  94. //Sub-ingredients
  95. if(req.files.ingredients !== undefined){
  96. for(let i = 0; i < ingredientData.length; i++){
  97. if(ingredientData[i] === "") continue;
  98. let data = ingredientData[i].split(",");
  99. for(let j = 7; j < data.length; j+=3){
  100. if(data[j] === "") break;
  101. ingredientIndices[data[0].toLowerCase()].ingredients.push({
  102. ingredient: ingredientIndices[data[j].toLowerCase()]._id,
  103. quantity: (data[j+1] * helper.convertQuantityToBaseUnit(1, data[j+2])) / helper.convertQuantityToBaseUnit(1, data[3]),
  104. unit: data[j+2]
  105. })
  106. }
  107. }
  108. }
  109. //Recipes
  110. let newRecipes = [];
  111. if(req.files.recipes !== undefined){
  112. let recipeData = fs.readFileSync(req.files.recipes.tempFilePath).toString();
  113. fs.unlink(req.files.recipes.tempFilePath, ()=>{});
  114. recipeData = recipeData.split("\n");
  115. merchant.recipes = [];
  116. for(let i = 0; i < recipeData.length; i++){
  117. if(recipeData[i] === "") continue;
  118. let data = recipeData[i].split(",");
  119. let recipe = new Recipe({
  120. merchant: merchant._id,
  121. name: data[0],
  122. price: parseInt(parseFloat(data[1]) * 100),
  123. category: data[2],
  124. ingredients: []
  125. });
  126. for(let j = 3; j < data.length; j+=3){
  127. if(data[j] === "") break;
  128. recipe.ingredients.push({
  129. ingredient: ingredientIndices[data[j].toLowerCase()],
  130. quantity: helper.convertQuantityToBaseUnit(parseFloat(data[j+1]), data[j+2]),
  131. unit: data[j+2]
  132. });
  133. }
  134. merchant.recipes.push(recipe);
  135. newRecipes.push(recipe);
  136. }
  137. }
  138. let indexRecipes = ()=>{
  139. let recipes = {};
  140. for(let i = 0; i < merchant.recipes.length; i++){
  141. recipes[merchant.recipes[i].name.toLowerCase()] = merchant.recipes[i];
  142. }
  143. return recipes;
  144. }
  145. let recipeIndices = indexRecipes();
  146. //Orders
  147. let newOrders = [];
  148. if(req.files.orders !== undefined){
  149. Order.deleteMany({merchant: req.body.id}).catch((err)=>{});
  150. let orderData = fs.readFileSync(req.files.orders.tempFilePath).toString();
  151. fs.unlink(req.files.orders.tempFilePath, ()=>{});
  152. orderData = orderData.split("\n");
  153. for(let i = 0; i < orderData.length; i++){
  154. if(orderData[i] === "") continue;
  155. let data = orderData[i].split(",");
  156. let order = new Order({
  157. merchant: merchant._id,
  158. name: data[0],
  159. date: new Date(data[1]),
  160. taxes: parseInt(parseFloat(data[2]) * 100),
  161. fees: parseInt(parseFloat(data[3]) * 100),
  162. ingredients: []
  163. });
  164. for(let j = 4; j < data.length; j+=3){
  165. if(data[j] === "") break;
  166. order.ingredients.push({
  167. ingredient: ingredientIndices[data[j].toLowerCase()],
  168. quantity: parseFloat(data[j+1]),
  169. pricePerUnit: parseInt(parseFloat(data[j+2]) * 100)
  170. });
  171. }
  172. newOrders.push(order);
  173. }
  174. }
  175. //Transactions
  176. let newTransactions = [];
  177. if(req.files.transactions !== undefined){
  178. Transaction.deleteMany({merchant: req.body.id}).catch((err)=>{});
  179. let transactionData = fs.readFileSync(req.files.transactions.tempFilePath).toString()
  180. fs.unlink(req.files.transactions.tempFilePath, ()=>{});
  181. transactionData = transactionData.split("\n");
  182. for(let i = 0; i < transactionData.length; i++){
  183. if(transactionData[i] === "") continue;
  184. let data = transactionData[i].split(",");
  185. let transaction = new Transaction({
  186. merchant: merchant._id,
  187. date: new Date(data[0]),
  188. recipes: []
  189. });
  190. for(let j = 1; j < data.length; j+=2){
  191. if(data[j] === "") break;
  192. transaction.recipes.push({
  193. recipe: recipeIndices[data[j].toLowerCase()],
  194. quantity: parseInt(data[j+1])
  195. });
  196. }
  197. newTransactions.push(transaction);
  198. }
  199. }
  200. return Promise.all([
  201. merchant.save(),
  202. Ingredient.create(newIngredients),
  203. Recipe.create(newRecipes),
  204. Order.create(newOrders),
  205. Transaction.create(newTransactions)
  206. ]);
  207. })
  208. .then((response)=>{
  209. return res.redirect("/dashboard");
  210. })
  211. .catch((err)=>{
  212. return res.json("ERROR: A whoopsie has been made");
  213. });
  214. }
  215. }