Order.js 4.1 KB

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