Order.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. class OrderIngredient{
  2. constructor(ingredient, quantity){
  3. if(quantity < 0){
  4. banner.createError = "QUANTITY CANNOT BE A NEGATIVE NUBMER";
  5. return false;
  6. }
  7. this._ingredient = ingredient;
  8. this.quantity = this.convertToBase(quantity);
  9. }
  10. get ingredient(){
  11. return this._ingredient;
  12. }
  13. get quantity(){
  14. switch(unit){
  15. case "g":return this._quantity;
  16. case "kg": return this._quantity * 1000;
  17. case "oz": return this._quantity * 28.3495;
  18. case "lb": return this._quantity * 453.5924;
  19. case "ml": return this._quantity / 1000;
  20. case "l": return this._quantity;
  21. case "tsp": return this._quantity / 202.8842;
  22. case "tbsp": return this._quantity / 67.6278;
  23. case "ozfl": return this._quantity / 33.8141;
  24. case "cup": return this._quantity / 4.1667;
  25. case "pt": return this._quantity / 2.1134;
  26. case "qt": return this._quantity / 1.0567;
  27. case "gal": return this._quantity * 3.7854;
  28. case "mm": return this._quantity / 1000;
  29. case "cm": return this._quantity / 100;
  30. case "m": return this._quantity;
  31. case "in": return this._quantity / 39.3701;
  32. case "ft": return this._quantity / 3.2808;
  33. default: return this._quantity;
  34. }
  35. }
  36. convertToBase(quantity){
  37. switch(this._ingredient.unit){
  38. case "g": return quantity;
  39. case "kg": return quantity / 1000;
  40. case "oz": return quantity / 28.3495;
  41. case "lb": return quantity / 453.5924;
  42. case "ml": return quantity *= 1000;
  43. case "l": return quantity;
  44. case "tsp": return quantity * 202.8842;
  45. case "tbsp": return quantity * 67.6278;
  46. case "ozfl": return quantity * 33.8141;
  47. case "cup": return quantity * 4.1667;
  48. case "pt": return quantity * 2.1134;
  49. case "qt": return quantity * 1.0567;
  50. case "gal": return quantity / 3.7854;
  51. case "mm": return quantity * 1000;
  52. case "cm": return quantity * 100;
  53. case "m": return quantity;
  54. case "in": return quantity * 39.3701;
  55. case "ft": return quantity * 3.2808;
  56. default: return quantity;
  57. }
  58. }
  59. }
  60. class Order{
  61. constructor(id, name, date, taxes, fees, ingredients, parent){
  62. if(!this.isSanitaryString(name)){
  63. banner.createError("NAME CONTAINS ILLEGAL CHARACTERS");
  64. return false;
  65. }
  66. if(taxes < 0){
  67. banner.createError("TAXES CANNOT BE A NEGATIVE NUMBER");
  68. }
  69. this._id = id;
  70. this._name = name;
  71. this._date = new Date(date);
  72. this._taxes = taxes;
  73. this._fees = fees;
  74. this._ingredients = [];
  75. this._parent = parent;
  76. if(date > new Date()){
  77. banner.createError("CANNOT SET A DATE IN THE FUTURE");
  78. return false;
  79. }
  80. for(let i = 0; i < ingredients.length; i++){
  81. const orderIngredient = new OrderIngredient(
  82. ingredients[i].ingredient,
  83. ingredients[i].quantity
  84. )
  85. this._ingredients.push(orderIngredient);
  86. }
  87. this._parent.modules.ingredients.isPopulated = false;
  88. }
  89. get id(){
  90. return this._id;
  91. }
  92. get name(){
  93. return this._name;
  94. }
  95. get date(){
  96. return this._date;
  97. }
  98. get taxes(){
  99. return this._taxes;
  100. }
  101. get fees(){
  102. return this._fees;
  103. }
  104. get parent(){
  105. return this._parent;
  106. }
  107. get ingredients(){
  108. return this._ingredients;
  109. }
  110. isSanitaryString(str){
  111. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  112. for(let i = 0; i < disallowed.length; i++){
  113. if(str.includes(disallowed[i])){
  114. return false;
  115. }
  116. }
  117. return true;
  118. }
  119. }
  120. module.exports = Order;