Recipe.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. for(let i = 0; i < this.ingredients.length; i++){
  66. if(this.ingredients[i].ingredient.id === id){
  67. return (this.ingredientTotals[id] === undefined) ? 0 : this.ingredientTotals[id] * controller.unitMultiplier(controller.toBase(this.ingredients[i].ingredient.unit), this.ingredients[i].ingredient.unit);
  68. }
  69. }
  70. return 0;
  71. }
  72. addIngredient(ingredient, quantity, unit, baseUnitMultiplier){
  73. let recipeIngredient = new RecipeIngredient(ingredient, quantity, unit, baseUnitMultiplier);
  74. this.ingredients.push(recipeIngredient);
  75. recipeBook.isPopulated = false;
  76. analytics.isPopulated = false;
  77. }
  78. calculateIngredientTotals(){
  79. this.ingredientTotals = {};
  80. let traverseIngredient = (ingredient, multiplier)=>{
  81. for(let i = 0; i < ingredient.subIngredients.length; i++){
  82. traverseIngredient(ingredient.subIngredients[i].ingredient, multiplier * ingredient.subIngredients[i]._quantity);
  83. }
  84. if(this.ingredientTotals[ingredient.id] === undefined){
  85. this.ingredientTotals[ingredient.id] = multiplier;
  86. }else{
  87. this.ingredientTotals[ingredient.id] += multiplier;
  88. }
  89. }
  90. for(let i = 0; i < this.ingredients.length; i++){
  91. traverseIngredient(this.ingredients[i]._ingredient, this.ingredients[i]._quantity);
  92. }
  93. }
  94. }
  95. module.exports = Recipe;