Recipe.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 * controller.unitMultiplier(controller.getBaseUnit(this._unit), this._unit);
  14. }
  15. set quantity(quantity){
  16. this._quantity = quantity;
  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. let ingredient = parent.getIngredient(ingredients[i].ingredient);
  51. let recipeIngredient = new RecipeIngredient(
  52. ingredient.ingredient,
  53. ingredients[i].quantity,
  54. ingredients[i].unit,
  55. );
  56. this._ingredients.push(recipeIngredient);
  57. }
  58. }
  59. get id(){
  60. return this._id;
  61. }
  62. get name(){
  63. return this._name;
  64. }
  65. set name(name){
  66. this._name = name;
  67. }
  68. get category(){
  69. return this._category;
  70. }
  71. set category(category){
  72. this._category = category;
  73. }
  74. get price(){
  75. return this._price / 100;
  76. }
  77. set price(price){
  78. this._price = price;
  79. }
  80. get parent(){
  81. return this._parent;
  82. }
  83. get hidden(){
  84. return this._hidden;
  85. }
  86. set hidden(hidden){
  87. this._hidden = hidden;
  88. }
  89. get ingredients(){
  90. return this._ingredients;
  91. }
  92. clearIngredients(){
  93. this._ingredients = [];
  94. }
  95. get ingredientTotals(){
  96. return this._ingredientTotals;
  97. }
  98. //Returns the quantity of a single ingredient with the recipe.
  99. //Returns the quantity converted to the base unit of the ingredient
  100. getIngredientTotal(id){
  101. for(let i = 0; i < this._ingredients.length; i++){
  102. if(this._ingredients[i].ingredient.id === id){
  103. return (this._ingredientTotals[id] === undefined) ? 0 : this._ingredientTotals[id] * controller.unitMultiplier(controller.toBase(this._ingredients[i].ingredient.unit), this._ingredients[i].ingredient.unit);
  104. }
  105. }
  106. return 0;
  107. }
  108. addIngredient(ingredient, quantity, unit, baseUnitMultiplier){
  109. let recipeIngredient = new RecipeIngredient(ingredient, quantity, unit, baseUnitMultiplier);
  110. this._ingredients.push(recipeIngredient);
  111. recipeBook.isPopulated = false;
  112. analytics.isPopulated = false;
  113. }
  114. removeIngredients(){
  115. this._ingredients = [];
  116. }
  117. calculateIngredientTotals(){
  118. this._ingredientTotals = {};
  119. let traverseIngredient = (ingredient, multiplier)=>{
  120. for(let i = 0; i < ingredient.subIngredients.length; i++){
  121. traverseIngredient(ingredient.subIngredients[i].ingredient, multiplier * ingredient.subIngredients[i]._quantity);
  122. }
  123. if(this._ingredientTotals[ingredient.id] === undefined){
  124. this._ingredientTotals[ingredient.id] = multiplier;
  125. }else{
  126. this._ingredientTotals[ingredient.id] += multiplier;
  127. }
  128. }
  129. for(let i = 0; i < this._ingredients.length; i++){
  130. traverseIngredient(this._ingredients[i]._ingredient, this._ingredients[i]._quantity);
  131. }
  132. }
  133. }
  134. module.exports = Recipe;