Order.js 4.1 KB

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