Recipe.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. const recipeBook = require("../strands/recipeBook.js");
  2. const analytics = require("../strands/analytics.js");
  3. class RecipeIngredient{
  4. constructor(ingredient, quantity, unit){
  5. this._ingredient = ingredient;
  6. this._quantity = quantity;
  7. this._unit = unit;
  8. }
  9. get ingredient(){
  10. return this._ingredient;
  11. }
  12. get quantity(){
  13. return this._quantity;
  14. }
  15. set quantity(quantity){
  16. this_quantity = controller.baseUnit(quantity, this._ingredient.unit);
  17. }
  18. get unit(){
  19. return this._unit;
  20. }
  21. getQuantityDisplay(){
  22. return `${this._quantity.toFixed(2)} ${this._unit.toUpperCase()}`;
  23. }
  24. // getQuantityAsBase(){
  25. // }
  26. }
  27. /*
  28. Recipe Object
  29. id = database id of recipe
  30. name = name of recipe
  31. price = price of recipe in cents
  32. ingredients = [{
  33. ingredient: Ingredient Object,
  34. quantity: quantity of the ingredient within the recipe (stored as base unit, i.e grams)
  35. unit: String
  36. baseUnitmultiplier: Number
  37. }]
  38. parent = merchant that it belongs to
  39. */
  40. class Recipe{
  41. constructor(id, name, category, price, ingredients, parent, hidden){
  42. this._id = id;
  43. this._name = name;
  44. this._category = category;
  45. this._price = price;
  46. this._parent = parent;
  47. this._hidden = hidden;
  48. this._ingredients = [];
  49. //Ingredient totals is the total amount of each ingredient within the recipe, converted to ingredient base unit
  50. this._ingredientTotals = {};
  51. for(let i = 0; i < ingredients.length; i++){
  52. const ingredient = parent.getIngredient(ingredients[i].ingredient);
  53. const recipeIngredient = new RecipeIngredient(
  54. ingredient.ingredient,
  55. ingredients[i].quantity,
  56. ingredients[i].unit,
  57. ingredients[i].baseUnitMultiplier
  58. );
  59. this._ingredients.push(recipeIngredient);
  60. }
  61. }
  62. get id(){
  63. return this._id;
  64. }
  65. get name(){
  66. return this._name;
  67. }
  68. set name(name){
  69. this._name = name;
  70. }
  71. get category(){
  72. return this._category;
  73. }
  74. set category(category){
  75. this._category = category;
  76. }
  77. get price(){
  78. return this._price / 100;
  79. }
  80. set price(price){
  81. this._price = price;
  82. }
  83. get parent(){
  84. return this._parent;
  85. }
  86. get hidden(){
  87. return this._hidden;
  88. }
  89. set hidden(hidden){
  90. this._hidden = hidden;
  91. }
  92. get ingredients(){
  93. return this._ingredients;
  94. }
  95. clearIngredients(){
  96. this._ingredients = [];
  97. }
  98. get ingredientTotals(){
  99. return this._ingredientTotals;
  100. }
  101. //Returns the quantity of a single ingredient with the recipe.
  102. //Returns the quantity converted to the base unit of the ingredient
  103. getIngredientTotal(id, isDisplay = false){
  104. if(isDisplay === true){
  105. for(let i = 0; i < this._ingredients.length; i++){
  106. if(this._ingredients[i].ingredient.id === id){
  107. return (this._ingredientTotals[id] === undefined) ? 0 : controller.displayUnit(this._ingredientTotals[id], this._ingredients[i].ingredient.unit);
  108. }
  109. break;
  110. }
  111. }
  112. return (this._ingredientTotals[id] === undefined) ? 0 : this._ingredientTotals[id];
  113. }
  114. addIngredient(ingredient, quantity, unit, baseUnitMultiplier){
  115. let recipeIngredient = new RecipeIngredient(ingredient, quantity, unit, baseUnitMultiplier);
  116. this._ingredients.push(recipeIngredient);
  117. recipeBook.isPopulated = false;
  118. analytics.isPopulated = false;
  119. }
  120. removeIngredients(){
  121. this._ingredients = [];
  122. }
  123. calculateIngredientTotals(){
  124. this._ingredientTotals = {};
  125. let traverseIngredient = (recipeIngredient, multiplier)=>{
  126. let ingredient = recipeIngredient._ingredient;
  127. for(let i = 0; i < ingredient.subIngredients.length; i++){
  128. traverseIngredient(ingredient.subIngredients[i].ingredient, multiplier * ingredient.subIngredients[i].quantity);
  129. }
  130. if(this._ingredientTotals[ingredient.id] === undefined){
  131. this._ingredientTotals[ingredient.id] = multiplier * recipeIngredient.baseUnitMultiplier;
  132. }else{
  133. this._ingredientTotals[ingredient.id] += multiplier * recipeIngredient.baseUnitMultiplier;
  134. }
  135. }
  136. for(let i = 0; i < this._ingredients.length; i++){
  137. traverseIngredient(this._ingredients[i], this._ingredients[i].quantity);
  138. }
  139. }
  140. }
  141. module.exports = Recipe;