Recipe.js 4.9 KB

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