Recipe.js 4.4 KB

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