Recipe.js 4.9 KB

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