Recipe.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 quantity(){
  10. return this._quantity * controller.unitMultiplier(controller.getBaseUnit(this.unit), this.unit);
  11. }
  12. set quantity(quantity){
  13. this._quantity = quantity;
  14. }
  15. getQuantityDisplay(){
  16. return `${this.quantity.toFixed(2)} ${this.unit.toUpperCase()}`;
  17. }
  18. }
  19. /*
  20. Recipe Object
  21. id = database id of recipe
  22. name = name of recipe
  23. price = price of recipe in cents
  24. ingredients = [{
  25. ingredient: Ingredient Object,
  26. quantity: quantity of the ingredient within the recipe (stored as base unit, i.e grams)
  27. unit: String
  28. baseUnitmultiplier: Number
  29. }]
  30. parent = merchant that it belongs to
  31. */
  32. class Recipe{
  33. constructor(id, name, category, price, ingredients, parent, hidden){
  34. this.id = id;
  35. this.name = name;
  36. this.category = category;
  37. this._price = price;
  38. this.parent = parent;
  39. this.hidden = hidden;
  40. this.ingredients = [];
  41. //Ingredient totals is the total amount of each ingredient within the recipe, converted to ingredient base unit
  42. this.ingredientTotals = {};
  43. for(let i = 0; i < ingredients.length; i++){
  44. let ingredient = parent.getIngredient(ingredients[i].ingredient);
  45. let recipeIngredient = new RecipeIngredient(
  46. ingredient.ingredient,
  47. ingredients[i].quantity,
  48. ingredients[i].unit,
  49. );
  50. this.ingredients.push(recipeIngredient);
  51. }
  52. }
  53. get price(){
  54. return this._price / 100;
  55. }
  56. set price(price){
  57. this._price = price;
  58. }
  59. clearIngredients(){
  60. this.ingredients = [];
  61. }
  62. //Returns the quantity of a single ingredient with the recipe.
  63. //Returns the quantity converted to the base unit of the ingredient
  64. getIngredientTotal(id){
  65. if(this.ingredientTotals[id] === undefined) return 0;
  66. let ingredient = merchant.getIngredient(id).ingredient;
  67. let ingredientTotal = this.ingredientTotals[id];
  68. let toIngredientBase = 0;
  69. switch(controller.getBaseUnit(controller.getBaseUnit(ingredient.unit))){
  70. case "g": toIngredientBase = ingredient.convert.toMass; break;
  71. case "l": toIngredientBase = ingredient.convert.toVolume; break;
  72. case "m": toIngredientBase = ingredient.convert.toLength; break;
  73. }
  74. return ingredientTotal / toIngredientBase;
  75. }
  76. addIngredient(ingredient, quantity, unit, baseUnitMultiplier){
  77. let recipeIngredient = new RecipeIngredient(ingredient, quantity, unit, baseUnitMultiplier);
  78. this.ingredients.push(recipeIngredient);
  79. recipeBook.isPopulated = false;
  80. analytics.isPopulated = false;
  81. }
  82. //Ingredient totals are stored as base of the recipeIngredient unit
  83. calculateIngredientTotals(){
  84. this.ingredientTotals = {};
  85. let traverseIngredient = (ingredient, multiplier)=>{
  86. for(let i = 0; i < ingredient.subIngredients.length; i++){
  87. traverseIngredient(ingredient.subIngredients[i].ingredient, multiplier * ingredient.subIngredients[i]._quantity);
  88. }
  89. if(this.ingredientTotals[ingredient.id] === undefined){
  90. this.ingredientTotals[ingredient.id] = multiplier;
  91. }else{
  92. this.ingredientTotals[ingredient.id] += multiplier;
  93. }
  94. }
  95. for(let i = 0; i < this.ingredients.length; i++){
  96. traverseIngredient(this.ingredients[i].ingredient, this.ingredients[i]._quantity);
  97. }
  98. }
  99. }
  100. module.exports = Recipe;