newTransaction.js 3.6 KB

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