Recipe.js 4.8 KB

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