newTransaction.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. let newTransaction = {
  2. display: function(Transaction){
  3. let recipeList = document.getElementById("newTransactionRecipes");
  4. let template = document.getElementById("createTransaction").content.children[0];
  5. document.getElementById("transactionFileUpload").addEventListener("click", ()=>{controller.openModal("transactionSpreadsheet")});
  6. while(recipeList.children.length > 0){
  7. recipeList.removeChild(recipeList.firstChild);
  8. }
  9. for(let i = 0; i < merchant.recipes.length; i++){
  10. let recipeDiv = template.cloneNode(true);
  11. recipeDiv.recipe = merchant.recipes[i];
  12. recipeList.appendChild(recipeDiv);
  13. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  14. }
  15. document.getElementById("submitNewTransaction").onclick = ()=>{this.submit(Transaction)};
  16. },
  17. submit: function(Transaction){
  18. let recipeDivs = document.getElementById("newTransactionRecipes");
  19. let date = document.getElementById("newTransactionDate").valueAsDate;
  20. if(date > new Date()){
  21. banner.createError("CANNOT HAVE A DATE IN THE FUTURE");
  22. return;
  23. }
  24. let data = {
  25. date: date,
  26. recipes: [],
  27. ingredientUpdates: {}
  28. };
  29. for(let i = 0; i < recipeDivs.children.length; i++){
  30. let quantity = recipeDivs.children[i].children[1].value;
  31. const recipe = recipeDivs.children[i].recipe;
  32. if(quantity !== "" && quantity > 0){
  33. data.recipes.push({
  34. recipe: recipe.id,
  35. quantity: quantity
  36. });
  37. for(let j = 0; j < recipe.ingredients.length; j++){
  38. let ingredient = recipe.ingredients[j];
  39. if(data.ingredientUpdates[ingredient.ingredient.id]){
  40. data.ingredientUpdates[ingredient.ingredient.id] += ingredient.convertToBase(ingredient.quantity) * quantity;
  41. }else{
  42. data.ingredientUpdates[ingredient.ingredient.id] = ingredient.convertToBase(ingredient.quantity) * quantity;
  43. }
  44. }
  45. }else if(quantity < 0){
  46. banner.createError("CANNOT HAVE NEGATIVE VALUES");
  47. return;
  48. }
  49. }
  50. if(data.recipes.length > 0){
  51. let loader = document.getElementById("loaderContainer");
  52. loader.style.display = "flex";
  53. fetch("/transaction/create", {
  54. method: "post",
  55. headers: {
  56. "Content-Type": "application/json;charset=utf-8"
  57. },
  58. body: JSON.stringify(data)
  59. })
  60. .then(response => response.json())
  61. .then((response)=>{
  62. if(typeof(response) === "string"){
  63. banner.createError(response);
  64. }else{
  65. merchant.addTransaction(response);
  66. controller.openStrand("transactions", merchant.getTransactions());
  67. banner.createNotification("TRANSACTION CREATED");
  68. }
  69. })
  70. .catch((err)=>{
  71. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  72. })
  73. .finally(()=>{
  74. loader.style.display = "none";
  75. });
  76. }
  77. },
  78. submitSpreadsheet: function(){
  79. event.preventDefault();
  80. controller.closeModal();
  81. const file = document.getElementById("spreadsheetInput").files[0];
  82. let data = new FormData();
  83. data.append("transactions", file);
  84. let loader = document.getElementById("loaderContainer");
  85. loader.style.display = "flex";
  86. fetch("/transactions/create/spreadsheet", {
  87. method: "post",
  88. body: data
  89. })
  90. .then(response => response.json())
  91. .then((response)=>{
  92. if(typeof(response) === "string"){
  93. banner.createError(response);
  94. }else{
  95. for(let i = 0; i < response.recipes.length; i++){
  96. response.recipes[i].recipe = response.recipes[i].recipe._id;
  97. }
  98. merchant.addTransaction(response);
  99. controller.openStrand("transactions", merchant.transactions);
  100. banner.createNotification("TRANSACTION SUCCESSFULLY CREATED. INGREDIENTS UPDATED");
  101. }
  102. })
  103. .catch((err)=>{
  104. banner.createError("UNABLE TO DISPLAY NEW TRANSACTIONS. PLEASE REFRESH THE PAGE");
  105. })
  106. .finally(()=>{
  107. loader.style.display = "none";
  108. });
  109. }
  110. }
  111. module.exports = newTransaction;